16.boost图深度优先遍历DFS
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 using namespace std; 16 using namespace boost; 17 18 //顶点名称 19 enum { A, B, C, D, E, F, G, H }; 20 //顶点个数 21 #define N 6 22 const char *name = "ABCDEF"; 23 24 //宏定义 25 typedef adjacency_list<vecS, vecS, undirectedS, no_property, property<edge_weight_t, int>> mygraph; 26 typedef property<edge_weight_t, int> EWP; 27 28 class DFSmyv :public boost::default_dfs_visitor 29 { 30 public: 31 //当前在哪个结点 32 template<typename vertex,typename Graph> 33 void discover_vertex(vertex v, Graph g) const 34 { 35 cout << "at" << v << endl; 36 } 37 38 //输出当前结点下一个要访问的结点 39 template<typename Edge,typename Graph> const 40 void examine_edge(Edge e, Graph g) 41 { 42 cout << "edges" << e << endl; 43 } 44 }; 45 46 //无向图 47 void main() 48 { 49 //图,每个结点是vec来实现,无向图,有边长与权重的属性 50 mygraph myg; 51 add_edge(A, B,13, myg); 52 add_edge(B, C,23 ,myg); 53 add_edge(A, C,1, myg); 54 add_edge(A, D,11, myg); 55 add_edge(C, D,10, myg); 56 add_edge(B, D,11, myg); 57 add_edge(B, E, 11, myg); 58 add_edge(B, F, 11, myg); 59 add_edge(B, G, 11, myg); 60 add_edge(B, H, 11, myg); 61 DFSmyv myd; 62 depth_first_search(myg, visitor(myd)); 63 64 65 cin.get(); 66 }