Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package javaapplication1;
- import java.util.Scanner;
- final class IDDFS
- {
- int g[][] = {
- {0,1,1,0,0}, //0
- {1,0,0,1,0}, //1
- {1,0,0,0,1}, //2
- {0,1,0,0,0}, //3
- {0,0,1,0,0}, //4
- };
- int vis[];
- int par[];
- int N;
- IDDFS()
- {
- Scanner sc = new Scanner(System.in);
- N = sc.nextInt();
- par = new int [N];
- vis = new int [N];
- for(int i=0; i <N ; i++ )
- {
- myIDDFS(0,0,i);
- for (int j=0; j<N; j++)
- {
- vis[j]=0;
- }
- System.out.println("");
- }
- print();
- }
- void myIDDFS(int s, int dept, int limit)
- {
- if(dept>limit)
- return;
- System.out.print(s + " ");
- vis[s] = 1;
- for(int v=0; v<N;v++)
- {
- if(g[s][v]==1 && vis[v]==0)
- {
- par[v] = s;
- myIDDFS(v,dept+1,limit);
- }
- }
- }
- void print()
- {
- System.out.println("Node\tParent");
- for(int i=0;i<N;i++)
- {
- System.out.println(""+i+"\t"+par[i]);
- }
- }
- }
- public class JavaApplication1 {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- IDDFS d = new IDDFS();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement