-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBFS.cpp
116 lines (105 loc) · 2.28 KB
/
BFS.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[100005],BFS_result;
bool visited[100005]={false};
void BFS(int source)
{
// Queue to store elements we need to explore.
queue<int> q;
// We need to explore source so we push source to the queue.
q.push(source);
visited[source] = true;
// Loop till we have no elements to explore.
while(!q.empty())
{
// Taking out first element from the queue.
int parent = q.front();
// As we are going to explore the first element so we pop it out.
q.pop();
// Push parent to results.
BFS_result.push_back(parent);
// To explore all elements of parent.
for(int i = 0; i < adj[parent].size(); i++)
{
if(!visited[adj[parent][i]])
{
// Push to queue as we need to explore it.
q.push(adj[parent][i]);
visited[adj[parent][i]] = true;
}
}
}
}
void print(vector<int> A)
{
for(int i=0; i<A.size(); i++)
cout<<A[i]<<" ";
cout<<endl;
}
void init(int vertices)
{
for(int i=0; i<vertices; i++)
visited[i]=false;
BFS_result.clear();
}
void BFS_all(int vertices)
{
for(int i = 0; i < vertices; i++)
{
if(!visited[i])
BFS(i);
}
}
int main() {
int vertices,edges;
cin >> vertices >> edges;
for(int i = 0; i < edges; i++)
{
int x,y;
cin >> x >> y;
// As it is an undirected graph we need to make an edge from x to y
// and an edge from y to x as well.
adj[x].push_back(y);
adj[y].push_back(x);
}
// To follow the convention of moving in ascending order
// we sort the list of edges from a particular node.
for(int i = 0; i <vertices; i++)
{
sort(adj[i].begin(),adj[i].end());
}
BFS(0);
cout<<"BFS Result : ";
print(BFS_result);
init(vertices);
BFS_all(vertices);
cout<<"BFS for all Result : ";
print(BFS_result);
return 0;
}
// TESTCASE 1
// 7 7
// 0 1
// 0 2
// 0 5
// 1 6
// 1 3
// 3 4
// 5 4
// TESTCASE 1 IN ADJACENCY LIST
// adj[0]=[1,2,5]
// adj[1]=[0,3,6]
// adj[2]=[0]
// adj[3]=[1,4]
// adj[4]=[3,5]
// adj[5]=[0,4]
// adj[6]=[1]
// TESTCASE 2
// 9 7
// 0 1
// 1 2
// 1 3
// 2 3
// 3 4
// 7 6
// 6 8