public class DFSExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] conn = { { 0, 1, 0, 1, 0, 0, 0, 0, 1 }, // 0
{ 0, 0, 0, 0, 0, 0, 0, 1, 0 }, // 1
{ 0, 0, 0, 0, 0, 1, 0, 0, 0 }, // 2
{ 0, 0, 1, 0, 1, 0, 0, 0, 0 }, // 3
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 4
{ 0, 0, 0, 0, 0, 0, 1, 0, 0 }, // 5
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 6
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 7
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 } };// 8
boolean[] visited = new boolean[conn.length];
int v = 0;
DFS(conn, visited, v);
}
public static void DFS(int[][] conn, boolean[] visited, int v){
visited[v] = true;
System.out.println("nextNode : "+v);
for(int i = 0; i < visited.length; i++){
if(conn[v][i] > 0 && !visited[i]){
DFS(conn, visited, i);
}
}
}
}
No comments:
Post a Comment