From 2a87bae8cc60255dcad08a8d187c88c5140cab54 Mon Sep 17 00:00:00 2001 From: ayuagar23 <65036486+ayuagar23@users.noreply.github.com> Date: Mon, 11 Oct 2021 11:23:40 +0530 Subject: [PATCH] Issue - #1 : Added implementation of BFS --- C++/Data Structure/bfs_queue.cpp | 41 ++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 C++/Data Structure/bfs_queue.cpp diff --git a/C++/Data Structure/bfs_queue.cpp b/C++/Data Structure/bfs_queue.cpp new file mode 100644 index 000000000..d1ab4d494 --- /dev/null +++ b/C++/Data Structure/bfs_queue.cpp @@ -0,0 +1,41 @@ +#include +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: "<>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<