-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfs.cpp
63 lines (55 loc) · 899 Bytes
/
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
60
61
62
63
#include<iostream>
#include<list>
using namespace std;
class graph
{public:
int v;
list<int> *array;
graph(int);
void add(int src,int des);
void bfs(int);
};
graph::graph(int a)
{
v=a;
array=new list<int>[v];
}
void graph::add(int src,int des)
{
array[src].push_back(des);
}
void graph::bfs(int v)
{
bool visited[v+1]={false};
list <int> queue;
queue.push_back(v);
visited[v]=true;
list<int>::iterator i;
while(!queue.empty())
{
int now=queue.front();
cout<<now<<" ";
queue.pop_front();
for(i=array[now].begin();i!=array[now].end();i++)
{
if(visited[*i]==false)
{queue.push_back(*i);
visited[*i]=true;
}
}
}
}
int main()
{
graph g(4);
g.add(0,1);
g.add(0, 2);
g.add(1, 2);
g.add(2, 0);
g.add(2, 3);
g.add(3, 3);
cout << "Following is Breadth First Traversal "
<< "(starting from vertex 2) \n";
g.bfs(2);
return 0;
}