-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathBFS.cpp
More file actions
62 lines (56 loc) · 1.26 KB
/
BFS.cpp
File metadata and controls
62 lines (56 loc) · 1.26 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
# include <iostream>
# include <algorithm>
# include <vector>
# include <list>
using namespace std;
class Graph
{
vector< vector<int> > Edge;
vector<unsigned char> visited; //We can use vector<bool>, but it's slow and broken :( , CPP's byte array http://stackoverflow.com/questions/10077771/what-is-the-correct-way-to-deal-with-medium-sized-byte-arrays-in-modern-c
public:
Graph(int V)
{
Edge.resize(V);
visited.resize(V);
}
void add_edge(int v,int w)
{
Edge[v].push_back(w);
}
void BFS(int s)
{
for(auto i : visited) i = false;
list<int> q;
visited[s] = true;
q.push_back(s);
while (!q.empty())
{
s = q.front();
cout<<s<<" ";
q.pop_front();
for(auto i : Edge[s])
{
if(!visited[i])
{
visited[i] = true;
q.push_back(i);
}
}
}
}
};
int main()
{
Graph g(6);
g.add_edge(0,1);
g.add_edge(0,2);
g.add_edge(1,2);
g.add_edge(2,0);
g.add_edge(2,3);
g.add_edge(3,3);
int v;
cout<<"Enter vertex from where to perform BFS"<<endl;
cin>>v;
g.BFS(2);
cout<< (char)true;
}