-
Notifications
You must be signed in to change notification settings - Fork 0
/
ollie126_BFS-Traversal.cpp
61 lines (58 loc) · 1.2 KB
/
ollie126_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
#include <bits/stdc++.h>
using namespace std;
// BFS Traversal
vector<int> bfsOfGraph(int V, vector<int> adj[])
{
vector<int> bfs;
vector<int> vis(V, 0);
queue<int> q;
q.push(0);
vis[0] = 1;
while (!q.empty())
{
int node = q.front();
q.pop();
bfs.push_back(node);
for(auto it: adj[node])
{
if(!vis[it])
{
q.push(it);
vis[it] = 1;
}
}
}
return bfs;
}
void addEdge(vector<int> adj[], int u, int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}
void printAns(vector<int> &ans)
{
for(int i = 0; i < ans.size(); i++)
{
cout << ans[i] << " ";
}
}
int main()
{
int vertices , edges;
cout << "Enter the number of vertices: ";
cin >> vertices;
cout << "Enter the number of edges: ";
cin >> edges;
vector<int> adj[vertices];
for(int i = 1 ; i <= edges ; i++)
{
int v1 , v2;
cin >> v1 >> v2;
addEdge(adj , v1 , v2);
}
vector<int> ans = bfsOfGraph(vertices, adj);
cout << "The bfs traversal of the above graph is as follows: " << endl;
printAns(ans);
cout << endl;
return 0;
}