Advertisement
pervej56

IDDFS Recursive

Mar 22nd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. package idddfs;
  2.  
  3. import java.util.Scanner;
  4.  
  5. /**
  6. *
  7. * @author Masud
  8. */
  9. public class Idddfs {
  10.  
  11. /**
  12. * @param args the command line arguments
  13. */
  14. public static int vis[] = new int[20];
  15. public static int n, search, t, find = 0, depth = 0;
  16. public static int s = 0;
  17. public static int g[][] = new int[20][20];
  18. public static boolean breakk = false;
  19.  
  20. public static void main(String[] args) {
  21. // TODO code application logic here
  22.  
  23. Scanner sn = new Scanner(System.in);
  24. System.out.println("Enter the Number of Node: ");
  25. n = sn.nextInt();
  26.  
  27. for (int i = 0; i < n; i++) {
  28. for (int j = 0; j < n; j++) { //Input
  29. g[i][j] = sn.nextInt();
  30. }
  31. }
  32. System.out.println("Enter the search Node: ");
  33. search = sn.nextInt();
  34.  
  35. System.out.println("Output");
  36. int k = 0;
  37. while (breakk == false) {
  38. DFS(s, 0, k); //Level
  39. for (int j = 0; j < n; j++) {
  40. vis[j] = 0;
  41. }
  42. k++;
  43. System.out.println("");
  44. }
  45. System.out.println("Find at= " + find);
  46.  
  47. }
  48.  
  49. static void DFS(int s, int currentlevel, int maxlevel) {
  50.  
  51. if (currentlevel > maxlevel) {
  52. return;
  53. }
  54. vis[s] = 1;
  55. System.out.print(s + " ");
  56. if (s == search) {
  57. breakk = true;
  58. find = currentlevel;
  59. }
  60. for (int v = 0; v < n; v++) {
  61. if (g[s][v] == 1 && vis[v] == 0) {
  62. DFS(v, currentlevel + 1, maxlevel);
  63. }
  64. }
  65. }
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement