forked from sureshmangs/Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfs.cpp
42 lines (38 loc) · 796 Bytes
/
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
36
37
38
39
40
41
42
#include<bits/stdc++.h>
using namespace std;
void adjListGraph(vector<int> adj[], int s, int d){
// undirected graph
adj[s].push_back(d);
adj[d].push_back(s);
}
void bfs(vector<int> adj[], int s, int v){
queue<int> q;
bool vis[v]={false};
q.push(s);
vis[s]=true;
while(!q.empty()){
cout<<q.front()<<" ";
q.pop();
for(auto x : adj[s]){
if(!vis[x]){
q.push(x);
vis[x]=true;
}
}
s=q.front();
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int v,e;
cin>>v>>e;
vector<int> adj[v];
while(e--){
int s,d;
cin>>s>>d;
adjListGraph(adj,s,d);
}
int s=0; // source is node 0
bfs(adj, s, v);
}