-
Notifications
You must be signed in to change notification settings - Fork 0
/
BFSInGraph.cpp
58 lines (52 loc) · 1.36 KB
/
BFSInGraph.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
// Using Queue
// TC O(V+E) SC O(V^2)
#include<bits/stdc++.h>
#include<queue>
void BFSHelper(vector<vector<int>> &adj_list,int source,vector<bool> &vis,vector<int>& res){
queue<int> q;
vis[source]=true;
q.push(source);
while(!q.empty())
{
int front = q.front();
q.pop();
res.push_back(front);
for(int i=0;i<adj_list.size();i++){
if(adj_list[front][i]==1 && !vis[i])
{
vis[i]=true;
q.push(i);
}
}
}
}
vector<vector<int>> adjacency_list(int vertex, vector<pair<int,int>> &edges){
vector<vector<int>> adj_list(vertex);
for(int i=0;i<vertex;i++){
adj_list[i]=vector<int>(vertex);
for(int j=0;j<vertex;j++)
adj_list[i][j]=0;
}
int size = edges.size();
for(int i=0;i<size;i++){
adj_list[edges[i].first][edges[i].second]=1;
adj_list[edges[i].second][edges[i].first]=1;
}
return adj_list;
}
vector<int> BFS(int vertex, vector<pair<int, int>> edges)
{
vector<vector<int>> adj_list = adjacency_list(vertex,edges);
int adj_size = adj_list.size();
vector<bool> vis(adj_size);
for(int i=0;i<vertex;i++){
vis[i]=false;
}
vector<int> res;
for(int i=0;i<adj_size;i++){
if(!vis[i]){
BFSHelper(adj_list,i,vis,res);
}
}
return res;
}