-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfs.cpp
42 lines (38 loc) · 884 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>
#define ll long long
using namespace std;
queue<int> q;
map<int,vector<int>> mp;
bool visited[10001]={false};
void bfs(int rt){
q.push(rt);
while(!q.empty()){
int d=q.front();
cout<<d<<" ";
q.pop();
for(auto i:mp){
for(auto j:i.second){
if (!visited[j]) {
visited[j]=true;
q.push(j);
}
}
}
}
}
int main() {
cout << "Enter no. of edges: ";
int n, v1, v2;
cin >> n;
cout << "Enter the nodes that are adjacent: ";
for (int i = 0; i < n; i++) {
cin >> v1 >> v2;
mp[v1].push_back(v2);
mp[v2].push_back(v1);
}
cout << "Enter root to find the dfs travel: ";
int root;
cin >> root;
bfs(root);
return 0;
}