Skip to content

Commit

Permalink
Issue - abhpd#1 : Added implementation of BFS
Browse files Browse the repository at this point in the history
  • Loading branch information
ayuagar23 authored Oct 11, 2021
1 parent bca8a1f commit 2a87bae
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions C++/Data Structure/bfs_queue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include<iostream>
using namespace std;
int adj[20][20],v[20],queue[20],n,front=0,rear=0;
void BFS(int i){
v[i]=3;
for(int j=1;j<=n;j++){
if(adj[i][j]==1 && v[j]==1){
queue[++rear]=j;
v[j]=2;
}
}
if(front<=rear)
BFS(queue[front++]);
}
int main(){
int i,j,start;
cout<<"Enter the total nodes: ";
cin>>n;
cout<<"give adjacency matrix: "<<endl;
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
cin>>adj[i][j];
}
}
for(i=1;i<=n;i++){
v[i]=1;
queue[i]=0;
}
cout<<"give starting point for bfs: ";
cin>>start;
queue[++rear]=start;
front=1;
v[start]=2;
BFS(start);
for(i=1;i<=n;i++){
if(queue[i]!='0')
cout<<queue[i];
}
return 0;
}

0 comments on commit 2a87bae

Please sign in to comment.