subgraph_properties.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // (C) Copyright Jeremy Siek 2004
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. /*
  6. Sample output:
  7. After initializing properties for G1:
  8. Global and local properties for vertex G0[C]...
  9. G0[C]= A1
  10. G1[A1]= A1
  11. Global and local properties for vertex G0[E]...
  12. G0[E]= B1
  13. G1[B1]= B1
  14. Global and local properties for vertex G0[F]...
  15. G0[F]= C1
  16. G1[C1]= C1
  17. After initializing properties for G2:
  18. Global and local properties for vertex G0[A]
  19. G0[A]= A2
  20. G2[A2]= A2
  21. Global and local properties for vertex G0[C]...
  22. G0[C]= B2
  23. G1[A1]= B2
  24. G2[B2]= B2
  25. */
  26. #include <boost/config.hpp>
  27. #include <iostream>
  28. #include <boost/graph/subgraph.hpp>
  29. #include <boost/graph/adjacency_list.hpp>
  30. #include <boost/graph/graph_utility.hpp>
  31. int main(int,char*[])
  32. {
  33. using namespace boost;
  34. //typedef adjacency_list_traits<vecS, vecS, directedS> Traits;// Does nothing?
  35. typedef property< vertex_color_t, int,
  36. property< vertex_name_t, std::string > > VertexProperty;
  37. typedef subgraph< adjacency_list< vecS, vecS, directedS,
  38. VertexProperty, property<edge_index_t, int> > > Graph;
  39. const int N = 6;
  40. Graph G0(N);
  41. enum { A, B, C, D, E, F}; // for conveniently refering to vertices in G0
  42. property_map<Graph, vertex_name_t>::type name = get(vertex_name_t(), G0);
  43. name[A] = "A";
  44. name[B] = "B";
  45. name[C] = "C";
  46. name[D] = "D";
  47. name[E] = "E";
  48. name[F] = "F";
  49. Graph& G1 = G0.create_subgraph();
  50. enum { A1, B1, C1 }; // for conveniently refering to vertices in G1
  51. add_vertex(C, G1); // global vertex C becomes local A1 for G1
  52. add_vertex(E, G1); // global vertex E becomes local B1 for G1
  53. add_vertex(F, G1); // global vertex F becomes local C1 for G1
  54. property_map<Graph, vertex_name_t>::type name1 = get(vertex_name_t(), G1);
  55. name1[A1] = "A1";
  56. std::cout << std::endl << "After initializing properties for G1:" << std::endl;
  57. std::cout << " Global and local properties for vertex G0[C]..." << std::endl;
  58. std::cout << " G0[C]= " << boost::get(vertex_name, G0, vertex(C, G0)) << std::endl;// prints: "G0[C]= C"
  59. std::cout << " G1[A1]= " << boost::get(vertex_name, G1, vertex(A1, G1)) << std::endl;// prints: "G1[A1]= A1"
  60. name1[B1] = "B1";
  61. std::cout << " Global and local properties for vertex G0[E]..." << std::endl;
  62. std::cout << " G0[E]= " << boost::get(vertex_name, G0, vertex(E, G0)) << std::endl;// prints: "G0[E]= E"
  63. std::cout << " G1[B1]= " << boost::get(vertex_name, G1, vertex(B1, G1)) << std::endl;// prints: "G1[B1]= B1"
  64. name1[C1] = "C1";
  65. std::cout << " Global and local properties for vertex G0[F]..." << std::endl;
  66. std::cout << " G0[F]= " << boost::get(vertex_name, G0, vertex(F, G0)) << std::endl;// prints: "G0[F]= F"
  67. std::cout << " G1[C1]= " << boost::get(vertex_name, G1, vertex(C1, G1)) << std::endl;// prints: "G1[C1]= C1"
  68. Graph& G2 = G0.create_subgraph();
  69. enum { A2, B2 }; // for conveniently refering to vertices in G2
  70. add_vertex(A, G2); // global vertex A becomes local A2 for G2
  71. add_vertex(C, G2); // global vertex C becomes local B2 for G2
  72. property_map<Graph, vertex_name_t>::type name2 = get(vertex_name_t(), G2);
  73. name2[A2] = "A2";
  74. std::cout << std::endl << std::endl << "After initializing properties for G2:" << std::endl;
  75. std::cout << " Global and local properties for vertex G0[A]" << std::endl;
  76. std::cout << " G0[A]= " << boost::get(vertex_name, G0, vertex(A, G0)) << std::endl;// prints: "G0[A]= A"
  77. std::cout << " G2[A2]= " << boost::get(vertex_name, G2, vertex(A2, G2)) << std::endl;// prints: "G2[A2]= A2"
  78. name2[B2] = "B2";
  79. std::cout << " Global and local properties for vertex G0[C]..." << std::endl;
  80. std::cout << " G0[C]= " << boost::get(vertex_name, G0, vertex(C, G0)) << std::endl;// prints: "G0[C]= C"
  81. std::cout << " G1[A1]= " << boost::get(vertex_name, G1, vertex(A1, G1)) << std::endl;// prints: "G1[A1]= A1"
  82. std::cout << " G2[B2]= " << boost::get(vertex_name, G2, vertex(B2, G2)) << std::endl;// prints: "G2[B2]= B2"
  83. return 0;
  84. }