-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBFS.cpp
36 lines (34 loc) · 1.04 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
// Q154 https://www.codingninjas.com/codestudio/problems/bfs-in-graph_973002?topList=striver-sde-sheet-problems&utm_source=striver&utm_medium=website
// Time: O(E+V) //DFS
// Space: O(E+V)+O(V)+O(V)
#include<bits/stdc++.h>
vector<int> BFS(int v, vector<pair<int, int>> edge){
int e=edge.size();
vector<int> bfs;
vector<int> vis(v,0);
vector<int> adj[v]; //vector of arrays
for(int i=0;i<e;i++){
adj[edge[i].second].push_back(edge[i].first);
adj[edge[i].first].push_back(edge[i].second);
}
for(int i=0;i<v;i++){
if(!vis[i]){
queue<int> q;
q.push(i);
vis[i]=1;
sort(adj[i].begin(),adj[i].end());
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;
}