diff --git a/Graph Theory/BFS/BFS_traversal.cpp b/Graph Theory/BFS/BFS_traversal.cpp new file mode 100644 index 0000000..074deed --- /dev/null +++ b/Graph Theory/BFS/BFS_traversal.cpp @@ -0,0 +1,65 @@ +//Code for BFS traversal of graph +#include +using namespace std; + + + +/* Function to do BFS of graph +* adj[]: array of vectors +* vis[]: array to keep track of visited nodes +*/ +void bfs(int s, vector adj[], bool vis[], int N) +{ + queue q; + q.push(s); + vis[s]=true; + + while(!q.empty()) + { + int u = q.front(); + cout<>T; + while(T--) + { + //N is number of nodes + //E is number of edges + int N, E; + cin>>N>>E; + vector adj[N]; + bool vis[N] = {false}; //initialize all values of vis array to false + for(int i=0;i>u>>v; + adj[u].push_back(v); + + // for undirected graph add this line + // adj[v].push_back(u); + } + + bfs(0, adj, vis, N); + + cout<