-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.cpp
More file actions
64 lines (63 loc) · 1.62 KB
/
bfs.cpp
File metadata and controls
64 lines (63 loc) · 1.62 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
63
64
#include"graph.hpp"
#include<cstdlib>
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<set>
#include<queue>
typedef enum{ WHITE,GRAY,BLACK } vertex_data_type;
typedef int edge_data_type;
typedef Graph<vertex_data_type, edge_data_type> graph_type;
typedef typename graph_type::Vertex vertex_type;
int main(int argc, char const *argv[])
{
std::string fname = "data.txt";
if( argc > 1)
fname = std::string(argv[1]);
graph_type g;
if( not g.load(fname))
{
std::cout<<"load error\n";
return 1;
}
g.finalized();
std::cout<<"graph info:"<<std::endl<<g.num_vertices()<<"\t"<<g.num_edges()<<std::endl;
for(auto& ele : g.vertices)
{
ele=WHITE;
}
std::queue<vid_type> q;
size_t vid = 0;
if( argc >= 3) vid = std::atoi(argv[2]);
q.push(vid);
g.vertices[vid]=GRAY;
size_t cnt = 0;
while( not q.empty())
{
vid_type top = q.front();
vertex_type v = g.get_vertex(top);
std::cout<<v.invid<<std::endl;
++cnt;
for(auto e : g.out_edges(v.vid))
{
if( g.vertices[e.first] == WHITE)
{
q.push(e.first);
g.vertices[e.first]=GRAY;
}
}
for(auto e : g.in_edges(v.vid))
{
if( g.vertices[e.first] == WHITE)
{
q.push(e.first);
g.vertices[e.first]=GRAY;
}
}
g.vertices[top]=BLACK;
q.pop();
}
std::cout<<cnt<<"\t"<<g.num_vertices()<<std::endl;
return 0;
}