diff --git a/Answer56.cpp b/Answer56.cpp new file mode 100644 index 0000000..eb37337 --- /dev/null +++ b/Answer56.cpp @@ -0,0 +1,26 @@ +vector bfsOfGraph(int V, vector adj[]) +{ + vector bfs_traversal; + vector vis(V, false); + for (int i = 0; i < V; ++i) { + if (!vis[i]) { + queue q; + vis[i] = true; + q.push(i); + while (!q.empty()) { + int g_node = q.front(); + q.pop(); + bfs_traversal.push_back(g_node); + for (auto it : adj[g_node]) { + if (!vis[it]) { + vis[it] = true; + q.push(it); + } + } + } + } + } + return bfs_traversal; +} + +//returns a vector conataining the BFS Graph