-
Notifications
You must be signed in to change notification settings - Fork 497
/
breadth_first_traversal.cpp
71 lines (58 loc) · 1.28 KB
/
breadth_first_traversal.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
59
60
61
62
63
64
65
66
67
68
69
70
71
//@ raunak kumar jaiswal
#include <bits/stdc++.h>
using namespace std;
void bfstraversal(vector<vector<int>>& g, vector<bool>& visited, vector<int>& ans, int node)
{
queue<int>qq;
qq.push(node);
visited[node] = true;
while(!qq.empty())
{
auto it = qq.front();
qq.pop();
for(auto itt : g[it])
{
if(visited[itt]==false)
{
visited[itt] = true;
qq.push(itt);
}
}
ans.push_back(it);
}
}
void makegraph( vector<vector<int>>& g, int num_edge)
{
for(int i=0;i<num_edge;i++)
{
int a,b;
cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
}
}
int main()
{
vector<vector<int>>g;
vector<bool>visited;
int num_node , num_edge;
cout<<"Enter number of Node"<<endl;
cin>>num_node;
cout<<"Enter number of Edges"<<endl;
cin>>num_edge;
g.assign(num_node,vector<int>());
visited.assign(num_node, false);
cout<<"Enter the edges between vertex"<<endl;
makegraph(g,num_edge);
vector<int>ans;
for (int i=0;i<num_node;i++)
{
if(visited[i]==false)
bfstraversal(g,visited, ans, i);
}
cout<<"----- Traversal output---"<<endl;
for(auto it: ans)
{
cout<<it<<" ";
}
}