Advertisement
FahimFaisal

Mid_assignment_cis2_pathao

Dec 9th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.40 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. import java.io.File;
  10. import java.util.LinkedList;
  11. import java.util.Queue;
  12. import java.util.Scanner;
  13.  
  14. /**
  15.  *
  16.  * @author acer
  17.  */
  18. public class BFS {
  19.     public static int node;
  20.     public static int[] color;
  21.     //public static char [] vertices={'u','v','w','x','y','z'};\
  22.     public static int [] vertices={0,1,2,3,4,5,6,7,8,9,10,11};
  23.     public static String [] place={"Mouchak","Panthapath","Rampura","Shahahbagh","Dhanmondi","Lalmatia","Badda","Hatirjheel","Nilkhet","TSC","Dhaka University","BUET"};
  24.     //public static int [] childcolor ={0,1,2,5,6,11};
  25.     public static int [] childcolor={1,1,1,0,0,1,1,0,0,0,0,1};
  26.     public static int [] d;
  27.     public static int [] parent;
  28.     public static int [][] matrix;
  29.     private static int edge;
  30.     public static String path="";
  31.    
  32.     public static void bfs_traverse(int s){
  33.         int pizza;
  34.         int pasta;
  35.        
  36.         for(int i=0;i<node;i++){
  37.             color[i]=0;//white
  38.             d[i]=999;
  39.             parent[i]=-1;
  40.         }
  41.             color[s]=1;//gray
  42.             d[s]=0;
  43.             parent[s]=-1;
  44.             Queue<Integer> q=new LinkedList<Integer>();
  45.             q.add(s);
  46.             while(!(q.isEmpty())){
  47.                 pizza=0;
  48.                 pasta=0;
  49.                 int u=q.remove();
  50.                 for(int v=0;v<node;v++){
  51.                     if(matrix[u][v]==1){
  52.                         if(childcolor[v]==1){
  53.                                 pizza++;
  54.                                
  55.                             }else{
  56.                                 pasta++;
  57.                             }
  58.                         if(color[v]==0){
  59.                            
  60.                             color[v]=1;
  61.                             d[v]=d[u]+1;
  62.                             parent[v]=u;
  63.                             q.add(v);
  64.                             //System.out.println(vertices[v]);
  65.                         }
  66.                     }
  67.             }
  68.                 color[u]=2;//black
  69.                 if(q.peek()!=null){
  70.                 path+=(vertices[u]+"->");
  71.                 }else{
  72.                     path+=(vertices[u]);
  73.                 }
  74.                 if(pizza>0 && pasta>0){
  75.                 System.out.println(place[u]+"->"+pizza+" pizza, "+pasta+" pasta" );
  76.                 }
  77.             }
  78.             System.out.println("Path "+path);
  79.            
  80.                
  81.         }
  82.    
  83.    
  84.    public static void main (String [] args){
  85.        try{
  86.            Scanner sc=new Scanner(new File("E:\\Pathao.txt"));
  87.            String [] split;
  88.            node=Integer.parseInt(sc.nextLine());
  89.            edge=Integer.parseInt(sc.nextLine());
  90.            matrix=new int [node][node];
  91.            while(sc.hasNext()){
  92.                String line=sc.nextLine();
  93.                split=line.split(",");
  94.                int x=Integer.parseInt(split[0]);
  95.                int y=Integer.parseInt(split[1]);
  96.                matrix[x][y]=1;
  97.            }
  98.      
  99.            d=new int[node];
  100.            color=new int[node];
  101.            parent=new int[node];
  102.            
  103.            bfs_traverse(0);
  104.            
  105.        }
  106.        catch(Exception e){
  107.           e.printStackTrace();
  108.        }
  109.    }
  110.    
  111.    
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement