-
Notifications
You must be signed in to change notification settings - Fork 1
/
BFS traversal.cpp
85 lines (69 loc) · 1.91 KB
/
BFS traversal.cpp
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
74
75
76
77
78
79
80
81
82
83
84
85
/*
BFS Traversal
Given an undirected and disconnected graph G(V, E), print its BFS traversal.
Note:
1. Here you need to consider that you need to print BFS path starting from vertex 0 only.
2. V is the number of vertices present in graph G and vertices are numbered from 0 to V-1.
3. E is the number of edges present in graph G.
4. Take graph input in the adjacency matrix.
5. Handle for Disconnected Graphs as well
Input Format :
The first line of input contains two integers, that denote the value of V and E.
Each of the following E lines contains space separated two integers, that denote that there exists an edge between vertex a and b.
Output Format :
Print the BFS Traversal, as described in the task.
Constraints :
0 <= V <= 1000
0 <= E <= (V * (V - 1)) / 2
0 <= a <= V - 1
0 <= b <= V - 1
Time Limit: 1 second
Sample Input 1:
4 4
0 1
0 3
1 2
2 3
Sample Output 1:
0 1 3 2
*/
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int main() {
// Write your code here
int v,e;
cin >> v >> e;
vector<vector<bool>> edges(v, vector<bool>(v, false));
for(int i = 0; i < e; ++i) {
int first;
int second;
cin >> first >> second;
edges[first][second] = true;
edges[second][first] = true;
}
vector<bool> visited(v, false);
queue<int> q;
// If the graph is a connected component graph
// We Call BFs for each Component
for(int i = 0; i < v ; i++) {
if(!visited[i]) {
q.push(i);
visited[i] = true;
while(!q.empty()) {
int front = q.front();
q.pop();
cout << front << " ";
for(int i = 0; i < v; i++) {
if(edges[front][i] and !visited[i]) {
q.push(i);
visited[i] = true;
}
}
}
}
}
}
// Time Complexity : O(V^2)
// Auxillary Space : O(V)