Advertisement
mdmamunkhan

IDFS_recursive

Mar 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. //IDFS
  2.  
  3. package dfs;
  4. /*
  5. * @author mamun
  6. */
  7. public class DFS {
  8.  
  9. public static void main(String[] args) {
  10.  
  11. dfs obj = new dfs();
  12. //obj.graph_print();
  13. }
  14. }
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22. package dfs;
  23.  
  24. /*
  25. * @author mamun
  26. */
  27. public class dfs {
  28.  
  29. int g[][] = {
  30. {0, 1, 0, 0, 1, 1}, //0
  31. {1, 0, 1, 0, 0, 0}, //1
  32. {0, 1, 0, 1, 0, 0}, //2
  33. {0, 0, 1, 0, 0, 0}, //3
  34. {1, 0, 0, 0, 0, 0}, //4
  35. {1, 0, 0, 0, 0, 0} //5
  36. };
  37. int visited[] = {0, 0, 0, 0, 0, 0};
  38. int N;
  39.  
  40. dfs() {
  41. N = g.length;
  42.  
  43. for (int i = 0; i < N; i++) {
  44.  
  45. mydfs(0, 2, i); //call dfs by first node 0
  46.  
  47. for (int I = 0; I < N; I++) {
  48. visited[I] = 0;
  49. }
  50.  
  51. System.out.println("");
  52. // print();
  53. }
  54. }
  55. void mydfs(int s, int depth, int limit) {
  56. if (depth > limit) {
  57. return;
  58. }
  59. visited[s] = 1;
  60. System.out.print(" " + s);
  61. for (int v = 0; v < N; v++) // N is node number
  62. {
  63. if (g[s][v] == 1 && visited[v] == 0) {
  64.  
  65. mydfs(v, depth + 1, limit); // call dfs again by current node
  66. }
  67. }
  68. }
  69. void graph_print() {
  70. System.out.println("Graph is : ");
  71. for (int i = 0; i <= g.length - 1; i++) {
  72. for (int j = 0; j <= g.length - 1; j++) {
  73. System.out.print(" " + g[i][j]);
  74. }
  75. System.out.println();
  76. }
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement