dfs.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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/depth_first_search.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( get(m_color, target(e, g)) == Color::gray() ||
  68. get(m_color, target(e, g)) == Color::black() );
  69. }
  70. template <class Vertex, class Graph>
  71. void finish_vertex(Vertex u, Graph&) {
  72. using namespace boost;
  73. BOOST_CHECK( get(m_color, u) == Color::black() );
  74. put(m_finish_time, u, m_time++);
  75. }
  76. private:
  77. ColorMap m_color;
  78. ParentMap m_parent;
  79. DiscoverTimeMap m_discover_time;
  80. FinishTimeMap m_finish_time;
  81. typename boost::property_traits<DiscoverTimeMap>::value_type m_time;
  82. };
  83. template <typename Graph>
  84. struct dfs_test
  85. {
  86. typedef boost::graph_traits<Graph> Traits;
  87. typedef typename Traits::vertices_size_type
  88. vertices_size_type;
  89. static void go(vertices_size_type max_V) {
  90. using namespace boost;
  91. typedef typename Traits::vertex_descriptor vertex_descriptor;
  92. typedef typename boost::property_map<Graph,
  93. boost::vertex_color_t>::type ColorMap;
  94. typedef typename boost::property_traits<ColorMap>::value_type ColorValue;
  95. typedef typename boost::color_traits<ColorValue> Color;
  96. vertices_size_type i, k;
  97. typename Traits::edges_size_type j;
  98. typename Traits::vertex_iterator vi, vi_end, ui, ui_end;
  99. boost::mt19937 gen;
  100. for (i = 0; i < max_V; ++i)
  101. for (j = 0; j < i*i; ++j) {
  102. Graph g;
  103. generate_random_graph(g, i, j, gen);
  104. ColorMap color = get(boost::vertex_color, g);
  105. std::vector<vertex_descriptor> parent(num_vertices(g));
  106. for (k = 0; k < num_vertices(g); ++k)
  107. parent[k] = k;
  108. std::vector<int> discover_time(num_vertices(g)),
  109. finish_time(num_vertices(g));
  110. // Get vertex index map
  111. typedef typename boost::property_map<Graph, boost::vertex_index_t>::const_type idx_type;
  112. idx_type idx = get(boost::vertex_index, g);
  113. typedef
  114. boost::iterator_property_map<typename std::vector<vertex_descriptor>::iterator, idx_type>
  115. parent_pm_type;
  116. parent_pm_type parent_pm(parent.begin(), idx);
  117. typedef
  118. boost::iterator_property_map<std::vector<int>::iterator, idx_type>
  119. time_pm_type;
  120. time_pm_type discover_time_pm(discover_time.begin(), idx);
  121. time_pm_type finish_time_pm(finish_time.begin(), idx);
  122. dfs_test_visitor<ColorMap, parent_pm_type,
  123. time_pm_type, time_pm_type>
  124. vis(color, parent_pm,
  125. discover_time_pm, finish_time_pm);
  126. boost::depth_first_search(g, visitor(vis).color_map(color));
  127. // all vertices should be black
  128. for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
  129. BOOST_CHECK(get(color, *vi) == Color::black());
  130. // check parenthesis structure of discover/finish times
  131. // See CLR p.480
  132. for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui)
  133. for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) {
  134. vertex_descriptor u = *ui, v = *vi;
  135. if (u != v) {
  136. BOOST_CHECK( finish_time[u] < discover_time[v]
  137. || finish_time[v] < discover_time[u]
  138. || (discover_time[v] < discover_time[u]
  139. && finish_time[u] < finish_time[v]
  140. && boost::is_descendant(u, v, parent_pm))
  141. || (discover_time[u] < discover_time[v]
  142. && finish_time[v] < finish_time[u]
  143. && boost::is_descendant(v, u, parent_pm))
  144. );
  145. }
  146. }
  147. }
  148. }
  149. };
  150. // usage: dfs.exe [max-vertices=15]
  151. int test_main(int argc, char* argv[])
  152. {
  153. int max_V = 7;
  154. if (argc > 1)
  155. max_V = atoi(argv[1]);
  156. // Test directed graphs.
  157. dfs_test< boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS,
  158. boost::property<boost::vertex_color_t, boost::default_color_type> >
  159. >::go(max_V);
  160. // Test undirected graphs.
  161. dfs_test< boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS,
  162. boost::property<boost::vertex_color_t, boost::default_color_type> >
  163. >::go(max_V);
  164. return 0;
  165. }