forked from pravalika2604/30-Days-Of-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay 20 bfs.cpp
More file actions
58 lines (57 loc) · 962 Bytes
/
Day 20 bfs.cpp
File metadata and controls
58 lines (57 loc) · 962 Bytes
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
#include<iostream>
#include<algorithm>
#include<list>
#include<queue>
using namespace std;
queue <int> q;
struct graph
{
list<int>adj;
};
list<int>::iterator it;
int main()
{
int V,e,v,u;
cout<<"enter no. of vertices in graph"<<endl;
cin>>V;
graph g[V];
cout<<"no. ofedges"<<endl;
cin>>e;
for(int i=0;i<e;i++)
{
cout<<"enter edges from parent u to v"<<endl;
cin>>u>>v;
g[u].adj.push_back(v);
}
int s,t;
cout<<"enter start vertex"<<endl;
cin>>s;
bool visited[V];
q.push(s);
visited[s]=true;
while(!q.empty())
{
t=q.front();
cout<<t<<"\t";
q.pop();
for(it=g[t].adj.begin();it!=g[t].adj.end();it++)
{
if(!visited[*it])
{
visited[*it]=true;
q.push(*it);
}
}
}
}
/*print adjacency list;
for(int i=0;i<V;i++)
{
cout<<i<<"\t";
for(it=g[i].adj.begin();it!=g[i].adj.end();it++)
{
cout<<*it<<"\t";
}
cout<<"\n";
}
*/