diff --git a/bfs.java b/bfs.java new file mode 100644 index 00000000..a44ec489 --- /dev/null +++ b/bfs.java @@ -0,0 +1,42 @@ +import java.util.*; + +public class App{ + + public static void bfs(byte [][] a,int vertices,int source){ //passing adjacency matrix and no of vertices + byte []b=new byte[vertices]; //flag container containing status of each vertices + Arrays.fill(b,(byte)-1); //status initialization + /* code status + -1 = ready + 0 = waiting + 1 = processed */ + + Queue st=new LinkedList<>(); //operational stack + st.add(source); //assigning source + while(!st.isEmpty()){ + b[st.peek()]=(byte)0; //assigning waiting status + System.out.println(st.element()); + int pop=st.element(); + b[pop]=(byte)1; //assigning processed status + st.remove(); //removing head of the queue + for(int i=0;i