From 067ba56664b9136296186d5913bc4ba3543e11f8 Mon Sep 17 00:00:00 2001 From: RaunakAgarwal18 <103554673+RaunakAgarwal18@users.noreply.github.com> Date: Thu, 27 Oct 2022 16:06:00 +0530 Subject: [PATCH] Create Answer56.cpp --- Answer56.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Answer56.cpp 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