-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBFS.cpp
More file actions
34 lines (28 loc) · 794 Bytes
/
BFS.cpp
File metadata and controls
34 lines (28 loc) · 794 Bytes
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
vector<int> bfsOfGraph(int V, vector<int> adj[]) {
// adjecent list is already given no need of taking node
vector<vector<int>>adj(V);
for(auto&x : edges){
int u = x.first;
int v = x.second;
adj[u].push_back(v);
adj[v].push_back(u);
}
// BFS
vector<int>vis(V,0);
vis[0] = 1;
queue<int>q;
q.push(0);
vector<int>bfs;
while(!q.empty()){
int node = q.front();
q.pop();
bfs.push_back(node);
for(auto&x : adj[node]){
if(!vis[x]) {
vis[x] = 1;
q.push(x);
}
}
}
return bfs;
}