-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBFS graphs using edges
42 lines (40 loc) · 1.03 KB
/
BFS graphs using edges
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
#include <bits/stdc++.h>
void prepareAdjList(unordered_map<int, set<int>> &adj, vector<pair<int,int>> &edges){
for(int i=0;i<edges.size();i++){
int u = edges[i].first;
int v = edges[i].second;
adj[u].insert(v);
adj[v].insert(u);
}
}
void bfs(unordered_map<int, set<int>> &adj, unordered_map<int, bool> &vis, vector<int> &ans,
int node){
queue<int> q;
q.push(node);
vis[node] = 1;
while(!q.empty()){
int frontNode = q.front();
q.pop();
ans.push_back(frontNode);
for(auto it:adj[frontNode]){
if(!vis[it]){
q.push(it);
vis[it] = 1;
}
}
}
}
vector<int> BFS(int vertex, vector<pair<int, int>> edges)
{
unordered_map<int, set<int>> adj;
vector<int> ans;
unordered_map<int, bool> vis;
prepareAdjList(adj, edges);
//traverse all component of graph
for(int i=0;i<vertex;i++){
if(!vis[i]){
bfs(adj,vis,ans,i);
}
}
return ans;
}