-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBfs.cpp
More file actions
64 lines (59 loc) · 1.18 KB
/
Copy pathBfs.cpp
File metadata and controls
64 lines (59 loc) · 1.18 KB
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
#include<bits/stdc++.h>
#define ll long long
#define vll vector<ll>
#define pb push_back
using namespace std;
vll adj[20];
bool visited[20];
void bfs(ll s)
{
queue<ll> q;
visited[s]=true;
q.push(s);
while(!q.empty())
{
s=q.front();
q.pop();
cout<<s<<' ';
for(ll i=0 ; i<adj[s].size() ; i++)
{
ll node=adj[s][i];
if(!visited[node])
{
q.push(node);
visited[node]=true;
}
}
}
}
void addEdge(ll a,ll b)
{
adj[a].pb(b);
adj[b].pb(a);
}
void initialize()
{
for(ll i=0 ; i<20 ; i++)
visited[i]=false;
}
int main()
{
ll edges[][2]={
{1,2},
{1,3},
{2,4},
{2,5},
{3,6},
{3,7},
{4,8},
{4,9},
{4,10},
{5,11},
{7,12},
{7,13}
};
for(ll i=0 ; i<12 ; i++)
addEdge(edges[i][0],edges[i][1]);
bfs(2);
return 0;
}