From 760d454d1680f944656c39180a2ad4a980a6f05d Mon Sep 17 00:00:00 2001 From: Bhavya Tyagi <55448429+bhavyatyagi@users.noreply.github.com> Date: Tue, 25 Aug 2020 20:54:05 +0530 Subject: [PATCH] Update bfs.cpp Modified the solution as per question (Added disconnected graphs)! --- Graphs1/bfs.cpp | 96 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/Graphs1/bfs.cpp b/Graphs1/bfs.cpp index e5bce72..2f4e13b 100644 --- a/Graphs1/bfs.cpp +++ b/Graphs1/bfs.cpp @@ -13,6 +13,9 @@ Note : Take graph input in adjacency matrix. Input Format : Line 1: Two Integers V and E (separated by space) Next 'E' lines, each have two space-separated integers, 'a' and 'b', denoting that there exists an edge between Vertex 'a' and Vertex 'b'. +Note : 1. Take graph input in the adjacency matrix. + 2. Handle for Disconnected Graphs as well +Input Format : Output Format : BFS Traversal (separated by space) Constraints : @@ -28,6 +31,99 @@ Sample Output 1: 0 1 3 2 */ + + +/* + Contributer: Bhavya Tyagi + Thapar Institute of Engineering & Technology, Patiala + Added The Modified code after the question includes Disconnected Graphs too +*/ + +#include +#include +using namespace std; + +void printBFS(int **edges,int n, int j,bool *visited) +{ + + queue q; + q.push(j); + visited[j]=true; + while(!q.empty()) + { + int currentEdge=q.front(); + cout<>n>>e; + + int **edges=new int*[n]; + + for(int i=0;i>f>>s; + edges[f][s]=1; + edges[s][f]=1; + } + bool *visited=new bool[n]; + + for(int i=0;i using namespace std;