Advertisement
FahimFaisal

DFS

Oct 8th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.09 KB | None | 0 0
  1. import java.io.File;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4.  
  5. public class DFS {
  6.     public static int node;
  7.     public static int time;
  8.     public static int[] color;    
  9.     public static int[] f;
  10.     public static int[] d;
  11.     public static int[] d1;
  12.     public static int[] parent;
  13.     public static int start;
  14.     public static char[] nodes={'u','v','w','x','y','z'};
  15.    
  16.     public static int[] vertices=new int[] {0,1,2,3,4,5,6,7};
  17.     public static int[][] matrix;
  18.     //public static int node;
  19.    
  20.    
  21.     public static void main(String [] args) {
  22.      try{
  23.             Scanner scn=new Scanner(new File("H:\\DFS Graph.txt"));
  24.             String[] splitA;
  25.             String temp=scn.nextLine();
  26.             node=Integer.parseInt(temp);
  27.          //   out.println(v);
  28.            String temp2=scn.nextLine();
  29.           int edge=Integer.parseInt(temp2);
  30.             matrix=new int[node][node];
  31.             while(scn.hasNext())
  32.             {
  33.             String line=scn.nextLine();
  34.             splitA=line.split(",");
  35.             int x=Integer.parseInt(splitA[0]);
  36.             int y=Integer.parseInt(splitA[1]);
  37.             matrix[x][y]=1;
  38.             }
  39.            
  40.             //----------
  41.              color=new int[node];
  42.               d=new int[node];
  43.            d1=new int[node];
  44.                f=new int[node];
  45.                parent=new int[node];
  46.                start=0;
  47.             //----------------------\\\
  48.                
  49.                //d[start]=0;
  50.                //parent[start]=0;
  51.                //3 1 6 7 5 4 2 0
  52.            
  53.             for(int i=0;i<node;i++) {
  54.             color[i]=0;
  55.             d[i]=0;
  56.             f[i]=0;
  57.             }
  58.            
  59.          
  60.      
  61.             time=0;
  62.             //color[0]=1;
  63.            
  64.          //DFS_visit(3);
  65.             for(int j=0;j<node;j++) {
  66.             if(color[j]==0) {
  67.                 DFS_visit(j);
  68.             }
  69.             }
  70.             print();
  71.             printStart();
  72.          
  73.          
  74.      }
  75.      
  76.             catch(Exception e) {
  77.                e.printStackTrace();
  78.               }
  79.     }
  80.     public static void DFS_visit(int u) {
  81.         //System.out.print(u+"====>");
  82.         color[u]=1;
  83.         time++;
  84.         d[u]=time;
  85.         for(int c=0;c<node;c++) {
  86.             if(matrix[u][c]==1) {
  87.                 if(color[c]==0) {
  88.                    
  89.                     parent[c]=u;
  90.                     DFS_visit(c);
  91.                 }
  92.             }
  93.         }
  94.         color[u]=2;//black
  95.         time=time+1;
  96.         f[u]=time;
  97.         System.out.print(nodes[u]+">");
  98.     }
  99.     public static void print() {
  100.          System.out.println();
  101.          System.out.println();
  102.          for(int c=0;c<node;c++) {
  103.              if(color[c]==2)
  104.                System.out.print("Black"+",");
  105.              else if(color[c]==1) {
  106.                  System.out.print("Gray"+",");
  107.                  
  108.              }
  109.              else {
  110.                  System.out.print("White"+",");
  111.              }
  112.        }
  113.          System.out.println();
  114.         System.out.println();
  115.         System.out.println();
  116.        }
  117.     public static int findIndex(int arr[], int t)
  118.     {
  119.  
  120.         // if array is Null
  121.         if (arr == null) {
  122.             return -1;
  123.         }
  124.  
  125.         // find length of array
  126.         int len = arr.length;
  127.         int i = 0;
  128.  
  129.         // traverse in the array
  130.         while (i < len) {
  131.  
  132.             // if the i-th element is t
  133.             // then return the index
  134.             if (arr[i] == t) {
  135.                 return i;
  136.             }
  137.             else {
  138.                 i = i + 1;
  139.             }
  140.         }
  141.         return -1;
  142.     }
  143.     public static void printStart() {
  144.         System.out.println("###-------START TIME---------###");
  145.          System.out.println();
  146.          
  147.         for(int k=0;k<node;k++) {
  148.             d1[k]=d[k];
  149.             //System.out.println(d[k]);
  150.             }
  151.          
  152.                
  153.          Arrays.sort(d1);
  154.          
  155.           for(int j=0;j<node;j++) {
  156.                
  157.                     int b=(d1[j]);
  158.                     //System.out.println(b);
  159.                     int ind=findIndex(d,b);
  160.                     System.out.print(nodes[ind]+"=>");
  161.                 }
  162.        
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement