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 DFS
- {
- int g[][] = {
- {0,1,1,0,0,0}, //0
- {1,0,0,1,0,1}, //1
- {1,0,0,0,1,0}, //2
- {0,1,0,0,0,0}, //3
- {0,0,1,0,0,0}, //4
- {0,1,0,0,0,0} //5
- };
- int vis[] = {0,0,0,0,0};
- int par[];
- int N;
- DFS()
- {
- Scanner sc = new Scanner(System.in);
- N = sc.nextInt();
- par = new int [N];
- myDFS(0);
- print();
- }
- void myDFS(int s)
- {
- vis[s] = 1;
- for(int v=0; v<N;v++)
- {
- if(g[s][v]==1 && vis[v]==0)
- {
- par[v] = s;
- myDFS(v);
- }
- }
- }
- 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
- DFS d = new DFS();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement