Advertisement
rana1704

DFS Normal

Feb 15th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.31 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 DFS
  12. {
  13.     int g[][] = {
  14.         {0,1,1,0,0,0}, //0
  15.         {1,0,0,1,0,1}, //1
  16.         {1,0,0,0,1,0}, //2
  17.         {0,1,0,0,0,0}, //3
  18.         {0,0,1,0,0,0}, //4
  19.         {0,1,0,0,0,0} //5
  20.     };
  21.     int vis[] = {0,0,0,0,0};
  22.     int par[];
  23.     int N;
  24.     DFS()
  25.     {
  26.         Scanner sc = new Scanner(System.in);
  27.         N = sc.nextInt();
  28.         par = new int [N];
  29.         myDFS(0);
  30.         print();
  31.        
  32.     }
  33.     void myDFS(int s)
  34.     {
  35.         vis[s] = 1;
  36.         for(int v=0; v<N;v++)
  37.         {
  38.             if(g[s][v]==1 && vis[v]==0)
  39.             {
  40.                 par[v] = s;
  41.                 myDFS(v);
  42.             }
  43.         }
  44.     }
  45.     void print()
  46.     {
  47.       System.out.println("Node\tParent");
  48.       for(int i=0;i<N;i++)
  49.       {
  50.         System.out.println(""+i+"\t"+par[i]);
  51.       }
  52.     }
  53. }
  54.  
  55. public class JavaApplication1 {
  56.  
  57.     /**
  58.      * @param args the command line arguments
  59.      */
  60.     public static void main(String[] args) {
  61.         // TODO code application logic here
  62.         DFS d = new DFS();
  63.        
  64.     }
  65.    
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement