-
Notifications
You must be signed in to change notification settings - Fork 3
/
bfsfunction.cpp
48 lines (43 loc) · 948 Bytes
/
bfsfunction.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
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
vector<ll > adj[20];
vector<bool > visited(20,false);
void bfs(int x)
{
queue<int > q;
q.push(x);
visited[x]=true ;
while(!q.empty())
{
int m=q.front();
cout<< m <<" ";
q.pop();
for(int j=0;j<adj[m].size();j++)
{
if(visited[adj[m][j]]==false)
{
q.push(adj[m][j]);
visited[adj[m][j]]=true;
}
}
}
}
int main()
{
ll n,e,x,y; //n=nodes e=edges
cin>>n>>e; //entering no. of node and edges
for(int i=1;i<=e;i++)
{
cin>>x>>y; //edges connection
adj[x].pb(y); //undirected graph
//adj[y].pb(x);
}
for(int i=0;i<n;i++)
{
if(visited[i]==false)
bfs(i);
}
return 0;
}