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 algorithm;
- import java.io.File;
- import java.util.LinkedList;
- import java.util.Queue;
- import java.util.Scanner;
- /**
- *
- * @author acer
- */
- public class BFS {
- public static int node;
- public static int[] color;
- //public static char [] vertices={'u','v','w','x','y','z'};\
- public static int [] vertices={0,1,2,3,4,5,6,7,8,9,10,11};
- public static String [] place={"Mouchak","Panthapath","Rampura","Shahahbagh","Dhanmondi","Lalmatia","Badda","Hatirjheel","Nilkhet","TSC","Dhaka University","BUET"};
- //public static int [] childcolor ={0,1,2,5,6,11};
- public static int [] childcolor={1,1,1,0,0,1,1,0,0,0,0,1};
- public static int [] d;
- public static int [] parent;
- public static int [][] matrix;
- private static int edge;
- public static String path="";
- public static void bfs_traverse(int s){
- int pizza;
- int pasta;
- for(int i=0;i<node;i++){
- color[i]=0;//white
- d[i]=999;
- parent[i]=-1;
- }
- color[s]=1;//gray
- d[s]=0;
- parent[s]=-1;
- Queue<Integer> q=new LinkedList<Integer>();
- q.add(s);
- while(!(q.isEmpty())){
- pizza=0;
- pasta=0;
- int u=q.remove();
- for(int v=0;v<node;v++){
- if(matrix[u][v]==1){
- if(childcolor[v]==1){
- pizza++;
- }else{
- pasta++;
- }
- if(color[v]==0){
- color[v]=1;
- d[v]=d[u]+1;
- parent[v]=u;
- q.add(v);
- //System.out.println(vertices[v]);
- }
- }
- }
- color[u]=2;//black
- if(q.peek()!=null){
- path+=(vertices[u]+"->");
- }else{
- path+=(vertices[u]);
- }
- if(pizza>0 && pasta>0){
- System.out.println(place[u]+"->"+pizza+" pizza, "+pasta+" pasta" );
- }
- }
- System.out.println("Path "+path);
- }
- public static void main (String [] args){
- try{
- Scanner sc=new Scanner(new File("E:\\Pathao.txt"));
- String [] split;
- node=Integer.parseInt(sc.nextLine());
- edge=Integer.parseInt(sc.nextLine());
- matrix=new int [node][node];
- while(sc.hasNext()){
- String line=sc.nextLine();
- split=line.split(",");
- int x=Integer.parseInt(split[0]);
- int y=Integer.parseInt(split[1]);
- matrix[x][y]=1;
- }
- d=new int[node];
- color=new int[node];
- parent=new int[node];
- bfs_traverse(0);
- }
- catch(Exception e){
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement