subgraph.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. #include <set>
  6. #include <boost/test/minimal.hpp>
  7. #include <boost/graph/subgraph.hpp>
  8. #include <boost/graph/adjacency_list.hpp>
  9. #include <boost/graph/random.hpp>
  10. #include "graph_test.hpp"
  11. #include <boost/graph/iteration_macros.hpp>
  12. #include <boost/random/mersenne_twister.hpp>
  13. #include "test_graph.hpp"
  14. // UNDER CONSTRUCTION
  15. // This is a helper function to recusively compare two subgraphs,
  16. // including the index for every local edges and their children.
  17. template<typename subgraph_t>
  18. void sub_cmp(subgraph_t const &g1, subgraph_t const &g2)
  19. {
  20. BOOST_CHECK(g1.is_root() == g2.is_root());
  21. BOOST_CHECK(num_vertices(g1) == num_vertices(g2));
  22. BOOST_CHECK(num_edges(g1) == num_edges(g2));
  23. typename subgraph_t::edge_iterator e1_i, e1_i_end, e2_i, e2_i_end;
  24. boost::tie(e1_i, e1_i_end) = edges(g1);
  25. boost::tie(e2_i, e2_i_end) = edges(g2);
  26. for(; e1_i != e1_i_end; ++e1_i, ++e2_i)
  27. {
  28. BOOST_CHECK(get(boost::edge_index, g1, *e1_i)
  29. == get(boost::edge_index, g2, *e2_i));
  30. }
  31. typename subgraph_t::const_children_iterator g1_i, g1_i_end, g2_i, g2_i_end;
  32. boost::tie(g1_i, g1_i_end) = g1.children();
  33. boost::tie(g2_i, g2_i_end) = g2.children();
  34. for(; g1_i != g1_i_end && g2_i != g2_i_end; ++g1_i, ++g2_i)
  35. {
  36. sub_cmp(*g1_i, *g2_i);
  37. }
  38. BOOST_CHECK(g1_i == g1_i_end && g2_i == g2_i_end);
  39. }
  40. int test_main(int, char*[])
  41. {
  42. using namespace boost;
  43. typedef adjacency_list<vecS, vecS, bidirectionalS,
  44. property<vertex_color_t, int>,
  45. property<edge_index_t, std::size_t, property<edge_weight_t, int> >
  46. > graph_t;
  47. typedef subgraph<graph_t> subgraph_t;
  48. typedef graph_traits<subgraph_t>::vertex_descriptor vertex_t;
  49. mt19937 gen;
  50. for (int t = 0; t < 100; t += 5) {
  51. subgraph_t g;
  52. int N = t + 2;
  53. std::vector<vertex_t> vertex_set;
  54. std::vector< std::pair<vertex_t, vertex_t> > edge_set;
  55. generate_random_graph(g, N, N * 2, gen,
  56. std::back_inserter(vertex_set),
  57. std::back_inserter(edge_set));
  58. graph_test< subgraph_t > gt;
  59. gt.test_incidence_graph(vertex_set, edge_set, g);
  60. gt.test_bidirectional_graph(vertex_set, edge_set, g);
  61. gt.test_adjacency_graph(vertex_set, edge_set, g);
  62. gt.test_vertex_list_graph(vertex_set, g);
  63. gt.test_edge_list_graph(vertex_set, edge_set, g);
  64. gt.test_adjacency_matrix(vertex_set, edge_set, g);
  65. std::vector<vertex_t> sub_vertex_set;
  66. std::vector<vertex_t> sub_global_map;
  67. std::vector<vertex_t> global_sub_map(num_vertices(g));
  68. std::vector< std::pair<vertex_t, vertex_t> > sub_edge_set;
  69. subgraph_t& g_s = g.create_subgraph();
  70. const std::set<vertex_t>::size_type Nsub = N/2;
  71. // Collect a set of random vertices to put in the subgraph
  72. std::set<vertex_t> verts;
  73. while (verts.size() < Nsub)
  74. verts.insert(random_vertex(g, gen));
  75. for (std::set<vertex_t>::iterator it = verts.begin();
  76. it != verts.end(); ++it) {
  77. vertex_t v_global = *it;
  78. vertex_t v = add_vertex(v_global, g_s);
  79. sub_vertex_set.push_back(v);
  80. sub_global_map.push_back(v_global);
  81. global_sub_map[v_global] = v;
  82. }
  83. // compute induced edges
  84. BGL_FORALL_EDGES(e, g, subgraph_t)
  85. if (container_contains(sub_global_map, source(e, g))
  86. && container_contains(sub_global_map, target(e, g)))
  87. sub_edge_set.push_back(std::make_pair(global_sub_map[source(e, g)],
  88. global_sub_map[target(e, g)]));
  89. gt.test_incidence_graph(sub_vertex_set, sub_edge_set, g_s);
  90. gt.test_bidirectional_graph(sub_vertex_set, sub_edge_set, g_s);
  91. gt.test_adjacency_graph(sub_vertex_set, sub_edge_set, g_s);
  92. gt.test_vertex_list_graph(sub_vertex_set, g_s);
  93. gt.test_edge_list_graph(sub_vertex_set, sub_edge_set, g_s);
  94. gt.test_adjacency_matrix(sub_vertex_set, sub_edge_set, g_s);
  95. if (num_vertices(g_s) == 0)
  96. return 0;
  97. std::vector<int> weights;
  98. for (unsigned i = 0; i < num_vertices(g_s); ++i)
  99. weights.push_back(i*2);
  100. gt.test_vertex_property_graph(weights, vertex_color_t(), g_s);
  101. // A regression test: the copy constructor of subgraph did not
  102. // copy one of the members, so local_edge->global_edge mapping
  103. // was broken.
  104. {
  105. subgraph_t g;
  106. graph_t::vertex_descriptor v1, v2;
  107. v1 = add_vertex(g);
  108. v2 = add_vertex(g);
  109. add_edge(v1, v2, g);
  110. subgraph_t sub = g.create_subgraph(vertices(g).first, vertices(g).second);
  111. graph_t::edge_iterator ei, ee;
  112. for (boost::tie(ei, ee) = edges(sub); ei != ee; ++ei) {
  113. // This used to segfault.
  114. get(edge_weight, sub, *ei);
  115. }
  116. }
  117. // This block generates a complete graph with 8 vertices,
  118. // and puts the first and last four of the vertices into two children.
  119. // Do these again to the children, so there are 4 grandchildren with 2 vertices for each.
  120. // Use the copy constructor to generate a copy and compare with the original one.
  121. {
  122. subgraph_t g1;
  123. for(size_t i = 0; i < 8; i ++)
  124. {
  125. add_vertex(g1);
  126. }
  127. subgraph_t::vertex_iterator vi_start, vi, vi_end, vj_start, vj, vj_end;
  128. for(tie(vi, vi_end) = vertices(g1); vi != vi_end; ++vi)
  129. {
  130. for(tie(vj, vj_end) = vertices(g1); vj != vj_end; ++vj)
  131. {
  132. if(*vi != *vj)
  133. {
  134. add_edge(*vi, *vj, g1);
  135. }
  136. }
  137. }
  138. tie(vi_start, vi_end) = vertices(g1);
  139. vi = vi_start;
  140. for(size_t i = 0; i < 4; i++)
  141. {
  142. ++vi;
  143. }
  144. g1.create_subgraph(vi_start, vi);
  145. g1.create_subgraph(++vi, vi_end);
  146. subgraph_t::children_iterator gi1, gi2;
  147. gi2 = g1.children().first;
  148. gi1 = gi2++;
  149. tie(vi_start, vi_end) = vertices(*gi1);
  150. vi = vi_start;
  151. tie(vj_start, vj_end) = vertices(*gi2);
  152. vj = vj_start;
  153. for(size_t i = 0; i < 2; i++)
  154. {
  155. ++vi;
  156. ++vj;
  157. }
  158. (*gi1).create_subgraph(vi_start, vi);
  159. (*gi1).create_subgraph(++vi, vi_end);
  160. (*gi2).create_subgraph(vj_start, vj);
  161. (*gi2).create_subgraph(++vj, vj_end);
  162. subgraph_t g2(g1);
  163. sub_cmp(g1, g2);
  164. }
  165. // Bootstrap the test_graph framework.
  166. // TODO: Subgraph is fundamentally broken for property types.
  167. // TODO: Under construction.
  168. {
  169. using namespace boost;
  170. typedef property<edge_index_t, size_t, EdgeBundle> EdgeProp;
  171. typedef adjacency_list<vecS, vecS, directedS, VertexBundle, EdgeProp> BaseGraph;
  172. typedef subgraph<BaseGraph> Graph;
  173. typedef graph_traits<Graph>::vertex_descriptor Vertex;
  174. Graph g;
  175. Vertex v = add_vertex(g);
  176. typedef property_map<Graph, int VertexBundle::*>::type BundleMap;
  177. BundleMap map = get(&VertexBundle::value, g);
  178. get(map, v);
  179. // put(map, v, 5);
  180. // BOOST_ASSERT(get(map, v) == 5);
  181. // test_graph(g);
  182. return 0;
  183. }
  184. }
  185. return 0;
  186. }