Advertisement
rana1704

IDDFS

Mar 22nd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.69 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package javaapplication1;
  7.  
  8. import java.util.Scanner;
  9.  
  10.  
  11. final class IDDFS
  12. {
  13.     int g[][] = {
  14.         {0,1,1,0,0}, //0
  15.         {1,0,0,1,0}, //1
  16.         {1,0,0,0,1}, //2
  17.         {0,1,0,0,0}, //3
  18.         {0,0,1,0,0}, //4
  19.     };
  20.     int vis[];
  21.     int par[];
  22.     int N;
  23.    
  24.     IDDFS()
  25.     {
  26.        
  27.         Scanner sc = new Scanner(System.in);
  28.         N = sc.nextInt();
  29.         par = new int [N];
  30.         vis = new int [N];
  31.        
  32.        
  33.         for(int i=0; i <N ; i++ )
  34.         {
  35.            myIDDFS(0,0,i);
  36.            
  37.            for (int j=0; j<N; j++)
  38.            {
  39.                vis[j]=0;
  40.            }
  41.             System.out.println("");
  42.      
  43.         }
  44.    
  45.         print();
  46.        
  47.     }
  48.     void myIDDFS(int s, int dept, int limit)
  49.     {
  50.         if(dept>limit)
  51.             return;
  52.         System.out.print(s + " ");
  53.         vis[s] = 1;
  54.         for(int v=0; v<N;v++)
  55.         {
  56.             if(g[s][v]==1 && vis[v]==0)
  57.             {
  58.                 par[v] = s;
  59.                 myIDDFS(v,dept+1,limit);
  60.                
  61.             }
  62.         }
  63.     }
  64.     void print()
  65.     {
  66.       System.out.println("Node\tParent");
  67.       for(int i=0;i<N;i++)
  68.       {
  69.         System.out.println(""+i+"\t"+par[i]);
  70.       }
  71.     }
  72. }
  73.  
  74. public class JavaApplication1 {
  75.  
  76.     /**
  77.      * @param args the command line arguments
  78.      */
  79.     public static void main(String[] args) {
  80.         // TODO code application logic here
  81.         IDDFS d = new IDDFS();
  82.        
  83.     }
  84.    
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement