Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dfs;
- /*
- * @author mamun
- */
- public class DFS {
- public static void main(String[] args) {
- dfs obj = new dfs();
- obj.print();
- }
- }
- //Call another java class
- package dfs;
- /*
- * @author mamun
- */
- public class dfs {
- int g[][] = { {0,1,0,0,1,1}, //0
- {1,0,1,0,0,0}, //1
- {0,1,0,1,0,0}, //2
- {0,0,1,0,0,0}, //3
- {1,0,0,0,0,0}, //4
- {1,0,0,0,0,0} //5
- };
- int visited[] ={0,0,0,0,0,0};
- int parent [];
- int N;
- dfs(){
- N=6;
- parent = new int [N]; // parent will an one dimensional array
- mydfs(0); //call dfs by first node 0
- }
- void mydfs(int s)
- {
- visited[s] =1;
- for (int v=0; v<N;v++) // N is node number
- {
- if (g[s][v] ==1 && visited[v]==0)
- {
- parent[v]=s;
- mydfs(v); // call dfs again by current node
- }
- }
- }
- void print()
- {
- System.out.println("Node\tParent");
- for (int i=0;i<N;i++)
- {
- System.out.println(""+i+"\t"+parent[i]);
- }
- }
- }
Add Comment
Please, Sign In to add comment