subgraph.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //=======================================================================
  2. // Copyright 2001 University of Notre Dame.
  3. // Author: Jeremy G. Siek
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //=======================================================================
  9. /*
  10. Sample output:
  11. G0:
  12. 0 --> 1
  13. 1 --> 2 3
  14. 2 --> 5
  15. 3 -->
  16. 4 --> 1 5
  17. 5 --> 3
  18. 0(0,1) 1(1,2) 2(1,3) 6(2,5) 3(4,1) 4(4,5) 5(5,3)
  19. G1:
  20. 2 --> 5
  21. 4 --> 5
  22. 5 -->
  23. 6(2,5) 4(4,5)
  24. G2:
  25. 0 --> 1
  26. 1 -->
  27. 0(0,1)
  28. */
  29. #include <boost/config.hpp>
  30. #include <iostream>
  31. #include <boost/graph/subgraph.hpp>
  32. #include <boost/graph/adjacency_list.hpp>
  33. #include <boost/graph/graph_utility.hpp>
  34. int main(int,char*[])
  35. {
  36. using namespace boost;
  37. typedef subgraph< adjacency_list<vecS, vecS, directedS,
  38. property<vertex_color_t, int>, 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. Graph& G1 = G0.create_subgraph();
  43. Graph& G2 = G0.create_subgraph();
  44. enum { A1, B1, C1 }; // for conveniently refering to vertices in G1
  45. enum { A2, B2 }; // for conveniently refering to vertices in G2
  46. add_vertex(C, G1); // global vertex C becomes local A1 for G1
  47. add_vertex(E, G1); // global vertex E becomes local B1 for G1
  48. add_vertex(F, G1); // global vertex F becomes local C1 for G1
  49. add_vertex(A, G2); // global vertex A becomes local A1 for G2
  50. add_vertex(B, G2); // global vertex B becomes local B1 for G2
  51. add_edge(A, B, G0);
  52. add_edge(B, C, G0);
  53. add_edge(B, D, G0);
  54. add_edge(E, B, G0);
  55. add_edge(E, F, G0);
  56. add_edge(F, D, G0);
  57. add_edge(A1, C1, G1); // (A1,C1) is subgraph G1 local indices for (C,F).
  58. std::cout << "G0:" << std::endl;
  59. print_graph(G0, get(vertex_index, G0));
  60. print_edges2(G0, get(vertex_index, G0), get(edge_index, G0));
  61. std::cout << std::endl;
  62. Graph::children_iterator ci, ci_end;
  63. int num = 1;
  64. for (boost::tie(ci, ci_end) = G0.children(); ci != ci_end; ++ci) {
  65. std::cout << "G" << num++ << ":" << std::endl;
  66. print_graph(*ci, get(vertex_index, *ci));
  67. print_edges2(*ci, get(vertex_index, *ci), get(edge_index, *ci));
  68. std::cout << std::endl;
  69. }
  70. return 0;
  71. }