forked from VarunRajPanigrahy/CppCodes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFSForDisconnectedGraph.cpp
More file actions
73 lines (63 loc) · 1.4 KB
/
BFSForDisconnectedGraph.cpp
File metadata and controls
73 lines (63 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// C++ implementation of modified BFS
#include<bits/stdc++.h>
using namespace std;
// A utility function to add an edge in an
// undirected graph.
void addEdge(vector<int> adj[], int u, int v)
{
adj[u].push_back(v);
}
// A utility function to do BFS of graph
// from a given vertex u.
void BFSUtil(int u, vector<int> adj[],
vector<bool> &visited)
{
// Create a queue for BFS
list<int> q;
// Mark the current node as visited and enqueue it
visited[u] = true;
q.push_back(u);
// 'i' will be used to get all adjacent vertices 4
// of a vertex list<int>::iterator i;
while(!q.empty())
{
// Dequeue a vertex from queue and print it
u = q.front();
cout << u << " ";
q.pop_front();
// Get all adjacent vertices of the dequeued
// vertex s. If an adjacent has not been visited,
// then mark it visited and enqueue it
for (int i = 0; i != adj[u].size(); ++i)
{
if (!visited[adj[u][i]])
{
visited[adj[u][i]] = true;
q.push_back(adj[u][i]);
}
}
}
}
// This function does BFSUtil() for all
// unvisited vertices.
void BFS(vector<int> adj[], int V)
{
vector<bool> visited(V, false);
for (int u=0; u<V; u++)
if (visited[u] == false)
BFSUtil(u, adj, visited);
}
// Driver code
int main()
{
int V = 5;
vector<int> adj[V];
addEdge(adj, 0, 4);
addEdge(adj, 1, 2);
addEdge(adj, 1, 3);
addEdge(adj, 1, 4);
addEdge(adj, 2, 3);
addEdge(adj, 3, 4);
BFS(adj, V);
return 0;
}