DFS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class DFS {
  private int[][] matrix;
  private int vertexLen = 4;
  private boolean[] visted;

  public DFS() {
    matrix = new int[vertexLen][vertexLen];
    matrix[0] = new int[]{0, 1, 1, 0};
    matrix[1] = new int[]{1, 0, 0, 1};
    matrix[2] = new int[]{1, 0, 0, 0};
    matrix[3] = new int[]{0, 1, 0, 0};
    visted = new boolean[vertexLen];
  }
  public void dfs() {
    for (int i = 0; i < vertexLen; i++) {
      if (!visted[i]) {
       dfs(i);
      }
    }
  }
  public void dfs(int vertex) {
    visted[vertex] = true;
    System.out.print(vertex + "->");
    for (int j = 0; j < vertexLen; j++) {
      if (!visted[j] && matrix[vertex][j] == 1) {
        dfs(j);
      }
    }
  }

  public static void main(String[] args) {
    DFS dfs = new DFS();
    dfs.dfs();
  }
}

results matching ""

    No results matching ""