From d617c09f7dfc221f18e70a8d1e8b9da2cc4443dc Mon Sep 17 00:00:00 2001 From: Arpit Singh Date: Mon, 5 Oct 2020 11:17:19 +0530 Subject: [PATCH] Create BFS_traversal.cpp --- Graph Theory/BFS/BFS_traversal.cpp | 65 ++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Graph Theory/BFS/BFS_traversal.cpp 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<