Advertisement
FahimFaisal

Mid_assignment_cis1

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