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 using namespace std; 10 using namespace boost; 11 12 //顶点名称 13 enum { A, B, C, D, E, F }; 14 //顶点个数 15 #define N 6 16 const char *name = "ABCDEF"; 17 18 //无向图 19 void main() 20 { 21 //vecS是vector数组的意思(每一个结点都是一个vector数组) 22 adjacency_list<vecS, vecS, undirectedS> myg(N); 23 //每一个结点都是list 24 //adjacency_list<listS, listS, undirectedS> myg; 25 //每一个结点都是set(红黑树) 26 //adjacency_list<setS, setS, undirectedS> myg; 27 //每一个结点都是multiset(红黑树,每一个结点是一个链表) 28 //adjacency_list<multisetS, multisetS, undirectedS> myg(5); 29 30 add_edge(A, B, myg); 31 add_edge(A, C, myg); 32 add_edge(A, D, myg); 33 add_edge(A, E, myg); 34 add_edge(B, C, myg); 35 add_edge(F, C, myg); 36 37 //输出所有结点 38 auto it = vertices(myg); 39 for (auto ib = it.first; ib != it.second; ib++) 40 { 41 cout << *ib << endl; 42 } 43 44 //输出所有边 45 auto ite = edges(myg); 46 for (auto ib = ite.first; ib != ite.second; ib++) 47 { 48 //cout << *ib << endl; 49 //输出从哪到哪 50 cout << source(*ib, myg) << "->" << target(*ib, myg) << endl; 51 } 52 53 54 cout << "顶点" << endl; 55 print_vertices(myg, name); 56 cout << endl; 57 58 cout << "边" << endl; 59 print_edges(myg, name); 60 cout << endl; 61 62 cout << "关联" << endl; 63 print_graph(myg, name); 64 cout << endl; 65 cin.get(); 66 }
转载于:https://www.cnblogs.com/xiaochi/p/8662403.html