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<