Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //IDFS
- package dfs;
- /*
- * @author mamun
- */
- public class DFS {
- public static void main(String[] args) {
- dfs obj = new dfs();
- //obj.graph_print();
- }
- }
- 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 N;
- dfs() {
- N = g.length;
- for (int i = 0; i < N; i++) {
- mydfs(0, 2, i); //call dfs by first node 0
- for (int I = 0; I < N; I++) {
- visited[I] = 0;
- }
- System.out.println("");
- // print();
- }
- }
- void mydfs(int s, int depth, int limit) {
- if (depth > limit) {
- return;
- }
- visited[s] = 1;
- System.out.print(" " + s);
- for (int v = 0; v < N; v++) // N is node number
- {
- if (g[s][v] == 1 && visited[v] == 0) {
- mydfs(v, depth + 1, limit); // call dfs again by current node
- }
- }
- }
- void graph_print() {
- System.out.println("Graph is : ");
- for (int i = 0; i <= g.length - 1; i++) {
- for (int j = 0; j <= g.length - 1; j++) {
- System.out.print(" " + g[i][j]);
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement