From c4f4e62043d3ae97707c4b222a94ca18afc4c719 Mon Sep 17 00:00:00 2001 From: surajkumar1198 <51400331+surajkumar1198@users.noreply.github.com> Date: Wed, 2 Oct 2019 15:00:02 +0530 Subject: [PATCH] added breadth first search to the searching algorithms --- Searching/breadth_first_search.cpp | 53 ++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Searching/breadth_first_search.cpp diff --git a/Searching/breadth_first_search.cpp b/Searching/breadth_first_search.cpp new file mode 100644 index 0000000..02ae779 --- /dev/null +++ b/Searching/breadth_first_search.cpp @@ -0,0 +1,53 @@ + +#include +using namespace std; +void bfs(int s, vector adj[], bool vis[], int N) +{ + queueq; + vis[s]=1; + cout<::iterator i; + while(!q.empty()){ + int p=q.front(); + q.pop(); + + for(i=adj[p].begin();i!=adj[p].end();i++){ + if(!vis[*i]){ //if vertex is not visited + vis[*i]=1; //mark vertex visited + cout<<*i<<" "; + q.push(*i); + } + + } + + + } + +} +int main() +{ + + int N, E;//N-number of vertices and E-number of edges + cin>>N>>E; + vector adj[N+1]; + bool vis[N+1] = {false};//Mark all the vertices as not visited + for(int i=0;i>u>>v; + adj[u].push_back(v);//creating undirected graph + adj[v].push_back(u);//enter vertices from 1 to N you want to connect + + } + int startVertex;//starting vertex from where you want to do bfs + cout<<"\nEnter the starting vertex from where you want to do bfs = "; + cin>>startVertex; + cout<<"\n"; + bfs(startVertex, adj, vis, N);//printing the bfs + cout<