-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbfs.cpp
59 lines (56 loc) · 1.03 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<bits/stdc++.h>
using namespace std;
class graph
{
int V;
list<int> *adj;
public:
graph(int);
void addedge(int,int);
void bfs(int);
};
graph :: graph(int V)
{
this->V=V;
adj = new list<int>[V];
}
void graph :: addedge(int v,int w)
{
adj[v].push_back(w);
}
void graph :: bfs(int s)
{
bool *visited = new bool[V];
int i;
for(i=0;i<V;i++)
visited[i] = false;
list<int> :: iterator it;
list<int> q;
q.push_back(s);
visited[s] = true;
while(!q.empty())
{
s=q.front();
cout<<s<<' ';
q.pop_front();
for(it=adj[s].begin();it!=adj[s].end();++it)
{
if(!visited[*it])
{
visited[*it] = true;
q.push_back(*it);
}
}
}
}
int main()
{
graph g(6);
g.addedge(0,1);
g.addedge(0,3);
g.addedge(0,4);
g.addedge(0,5);
g.addedge(5,2);
g.addedge(2,1);
g.bfs(0);
}