undirected_dfs.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #include <boost/config.hpp>
  10. #include <boost/test/minimal.hpp>
  11. #include <stdlib.h>
  12. #include <boost/graph/undirected_dfs.hpp>
  13. #include <boost/graph/adjacency_list.hpp>
  14. #include <boost/graph/graph_archetypes.hpp>
  15. #include <boost/graph/graph_utility.hpp>
  16. #include <boost/graph/random.hpp>
  17. #include <boost/random/mersenne_twister.hpp>
  18. template <typename ColorMap, typename ParentMap,
  19. typename DiscoverTimeMap, typename FinishTimeMap>
  20. class dfs_test_visitor {
  21. typedef typename boost::property_traits<ColorMap>::value_type ColorValue;
  22. typedef typename boost::color_traits<ColorValue> Color;
  23. public:
  24. dfs_test_visitor(ColorMap color, ParentMap p, DiscoverTimeMap d,
  25. FinishTimeMap f)
  26. : m_color(color), m_parent(p),
  27. m_discover_time(d), m_finish_time(f), m_time(0) { }
  28. template <class Vertex, class Graph>
  29. void initialize_vertex(Vertex u, Graph&) {
  30. BOOST_CHECK( boost::get(m_color, u) == Color::white() );
  31. }
  32. template <class Vertex, class Graph>
  33. void start_vertex(Vertex u, Graph&) {
  34. BOOST_CHECK( boost::get(m_color, u) == Color::white() );
  35. }
  36. template <class Vertex, class Graph>
  37. void discover_vertex(Vertex u, Graph&) {
  38. using namespace boost;
  39. BOOST_CHECK( get(m_color, u) == Color::gray() );
  40. BOOST_CHECK( get(m_color, get(m_parent, u)) == Color::gray() );
  41. put(m_discover_time, u, m_time++);
  42. }
  43. template <class Edge, class Graph>
  44. void examine_edge(Edge e, Graph& g) {
  45. using namespace boost;
  46. BOOST_CHECK( get(m_color, source(e, g)) == Color::gray() );
  47. }
  48. template <class Edge, class Graph>
  49. void tree_edge(Edge e, Graph& g) {
  50. using namespace boost;
  51. BOOST_CHECK( get(m_color, target(e, g)) == Color::white() );
  52. put(m_parent, target(e, g), source(e, g));
  53. }
  54. template <class Edge, class Graph>
  55. void back_edge(Edge e, Graph& g) {
  56. using namespace boost;
  57. BOOST_CHECK( get(m_color, target(e, g)) == Color::gray() );
  58. }
  59. template <class Edge, class Graph>
  60. void forward_or_cross_edge(Edge e, Graph& g) {
  61. using namespace boost;
  62. BOOST_CHECK( get(m_color, target(e, g)) == Color::black() );
  63. }
  64. template <class Edge, class Graph>
  65. void finish_edge(Edge e, Graph& g) {
  66. using namespace boost;
  67. BOOST_CHECK(
  68. (get(m_color, target(e, g)) == Color::gray())
  69. || (get(m_color, target(e, g)) == Color::black())
  70. );
  71. }
  72. template <class Vertex, class Graph>
  73. void finish_vertex(Vertex u, Graph&) {
  74. using namespace boost;
  75. BOOST_CHECK( get(m_color, u) == Color::black() );
  76. put(m_finish_time, u, m_time++);
  77. }
  78. private:
  79. ColorMap m_color;
  80. ParentMap m_parent;
  81. DiscoverTimeMap m_discover_time;
  82. FinishTimeMap m_finish_time;
  83. typename boost::property_traits<DiscoverTimeMap>::value_type m_time;
  84. };
  85. template <typename Graph>
  86. struct dfs_test
  87. {
  88. typedef boost::graph_traits<Graph> Traits;
  89. typedef typename Traits::vertices_size_type
  90. vertices_size_type;
  91. static void go(vertices_size_type max_V) {
  92. using namespace boost;
  93. typedef typename Traits::vertex_descriptor vertex_descriptor;
  94. typedef typename boost::property_map<Graph,
  95. boost::vertex_color_t>::type ColorMap;
  96. typedef typename boost::property_traits<ColorMap>::value_type ColorValue;
  97. typedef typename boost::color_traits<ColorValue> Color;
  98. typedef typename boost::property_map<Graph,
  99. boost::edge_color_t>::type EColorMap;
  100. typedef typename boost::property_traits<EColorMap>::value_type EColorValue;
  101. typedef typename boost::color_traits<EColorValue> EColor;
  102. vertices_size_type i, k;
  103. typename Traits::edges_size_type j;
  104. typename Traits::vertex_iterator vi, vi_end, ui, ui_end;
  105. typename Traits::edge_iterator ei, ei_end;
  106. boost::mt19937 gen;
  107. for (i = 0; i < max_V; ++i)
  108. for (j = 0; j < i*i; ++j) {
  109. Graph g;
  110. generate_random_graph(g, i, j, gen);
  111. ColorMap color = get(boost::vertex_color, g);
  112. EColorMap e_color = get(boost::edge_color, g);
  113. std::vector<vertex_descriptor> parent(num_vertices(g));
  114. for (k = 0; k < num_vertices(g); ++k)
  115. parent[k] = k;
  116. std::vector<int> discover_time(num_vertices(g)),
  117. finish_time(num_vertices(g));
  118. // Get vertex index map
  119. typedef typename boost::property_map<Graph, boost::vertex_index_t>::const_type idx_type;
  120. idx_type idx = get(boost::vertex_index, g);
  121. typedef
  122. boost::iterator_property_map<typename std::vector<vertex_descriptor>::iterator, idx_type>
  123. parent_pm_type;
  124. parent_pm_type parent_pm(parent.begin(), idx);
  125. typedef
  126. boost::iterator_property_map<std::vector<int>::iterator, idx_type>
  127. time_pm_type;
  128. time_pm_type discover_time_pm(discover_time.begin(), idx);
  129. time_pm_type finish_time_pm(finish_time.begin(), idx);
  130. dfs_test_visitor<ColorMap, parent_pm_type,
  131. time_pm_type, time_pm_type>
  132. vis(color, parent_pm,
  133. discover_time_pm, finish_time_pm);
  134. boost::undirected_dfs(g, visitor(vis).color_map(color)
  135. .edge_color_map(e_color));
  136. // all vertices should be black
  137. for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
  138. BOOST_CHECK(get(color, *vi) == Color::black());
  139. // all edges should be black
  140. for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
  141. BOOST_CHECK(get(e_color, *ei) == EColor::black());
  142. // check parenthesis structure of discover/finish times
  143. // See CLR p.480
  144. for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui)
  145. for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) {
  146. vertex_descriptor u = *ui, v = *vi;
  147. if (u != v) {
  148. BOOST_CHECK( finish_time[u] < discover_time[v]
  149. || finish_time[v] < discover_time[u]
  150. || (discover_time[v] < discover_time[u]
  151. && finish_time[u] < finish_time[v]
  152. && boost::is_descendant(u, v, parent_pm))
  153. || (discover_time[u] < discover_time[v]
  154. && finish_time[v] < finish_time[u]
  155. && boost::is_descendant(v, u, parent_pm))
  156. );
  157. }
  158. }
  159. }
  160. }
  161. };
  162. // usage: undirected_dfs.exe [max-vertices=15]
  163. int test_main(int argc, char* argv[])
  164. {
  165. int max_V = 7;
  166. if (argc > 1)
  167. max_V = atoi(argv[1]);
  168. // Test undirected graphs.
  169. dfs_test< boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS,
  170. boost::property<boost::vertex_color_t, boost::default_color_type>,
  171. boost::property<boost::edge_color_t, boost::default_color_type> >
  172. >::go(max_V);
  173. return 0;
  174. }