17.广度优先遍历bfs

 1 #include <iostream>
 2 #include <boost/config.hpp>
 3 //图(矩阵实现)
 4 #include <boost/graph/adjacency_matrix.hpp>
 5 #include <boost\graph\graph_utility.hpp>
 6 #include <boost/graph/graph_traits.hpp>
 7 //图(链表实现)
 8 #include <boost/graph/adjacency_list.hpp>
 9 //求最小生成树
10 #include <boost/graph/kruskal_min_spanning_tree.hpp>
11 //prim算法求最小生成树
12 #include <boost/graph/prim_minimum_spanning_tree.hpp>
13 //深度优先遍历
14 #include<boost/graph/depth_first_search.hpp>
15 //广度优先遍历
16 #include <boost/graph/breadth_first_search.hpp>
17 using namespace std;
18 using namespace boost;
19 
20 //顶点名称
21 enum { A, B, C, D, E, F, G, H ,I};
22 //顶点个数
23 #define N 8
24 const char *name = "ABCDEFGH";
25 
26 //宏定义
27 typedef adjacency_list<vecS, vecS, undirectedS, no_property, property<edge_weight_t, int>> mygraph;
28 typedef property<edge_weight_t, int> EWP;
29 
30 class BFSmyv :public boost::default_bfs_visitor
31 {
32 public:
33     //当前在哪个结点
34     template<typename vertex,typename Graph>
35     void discover_vertex(vertex v, Graph g) const
36     {
37         cout << "at" << v << endl;
38     }
39 
40     //输出当前结点下一个要访问的结点
41     template<typename Edge,typename Graph> const
42     void examine_edge(Edge e, Graph g)
43     {
44         cout << "edges" << e << endl;
45     }
46 };
47 
48 //无向图
49 void main()
50 {
51     //图,每个结点是vec来实现,无向图,有边长与权重的属性
52     mygraph myg;
53     add_edge(A, B,13, myg);
54     add_edge(B, C,23 ,myg);
55     add_edge(A, C,1, myg);
56     add_edge(A, D,11, myg);
57     add_edge(C, D,10, myg);
58     add_edge(D, E,11, myg);
59     add_edge(E, F, 11, myg);
60     add_edge(B, E, 11, myg);
61     add_edge(B, F, 11, myg);
62     add_edge(B, G, 11, myg);
63     add_edge(B, H, 11, myg);
64     add_edge(B, I, 11, myg);
65 
66     adjacency_list<vecS, vecS, undirectedS, no_property, property<edge_weight_t, int>>test;
67     BFSmyv myd;
68 
69     //用于广度优先遍历
70     //mygraph test;
71     breadth_first_search(myg,vertex(8,test), visitor(myd));
72     
73     
74     cin.get();
75 }

 

posted @ 2018-03-29 10:03  喵小喵~  阅读(206)  评论(0编辑  收藏  举报