-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfsofgraph.cpp
More file actions
50 lines (49 loc) · 1.01 KB
/
Copy pathbfsofgraph.cpp
File metadata and controls
50 lines (49 loc) · 1.01 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
#include <bits/stdc++.h>
#define int long long int
using namespace std;
vector<int> bfsOfGraph(int V, vector<int> adj[])
{
int vis[V] = {0};
vis[0] = 1;
queue<int> q;
// push the initial starting node
q.push(0);
vector<int> bfs;
// iterate till the queue is empty
while (!q.empty())
{
int node = q.front();
q.pop();
bfs.push_back(node);
// traverse for all its neighbours
for (auto it : adj[node])
{
// if the neighbour has previously not been visited,
// store in Q and mark as visited
if (!vis[it])
{
vis[it] = 1;
q.push(it);
}
}
}
return bfs;
}
int32_t main()
{
int V,E;
cin >> V >> E;
vector<int>adj[V+1];
for(int i=0;i<E;i++)
{
int x,y;
adj[x].push_back(y);
adj[y].push_back(x);
}
vector<int>bfs=bfsOfGraph(V,adj);
for(auto it:bfs)
{
cout<<it<<" ";
}
return 0;
}