depth_first_search.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. //=======================================================================
  2. // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
  3. // Copyright 2003 Bruce Barr
  4. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
  5. //
  6. // Distributed under the Boost Software License, Version 1.0. (See
  7. // accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //=======================================================================
  10. // Nonrecursive implementation of depth_first_visit_impl submitted by
  11. // Bruce Barr, schmoost <at> yahoo.com, May/June 2003.
  12. #ifndef BOOST_GRAPH_RECURSIVE_DFS_HPP
  13. #define BOOST_GRAPH_RECURSIVE_DFS_HPP
  14. #include <boost/config.hpp>
  15. #include <boost/graph/graph_traits.hpp>
  16. #include <boost/graph/graph_concepts.hpp>
  17. #include <boost/graph/properties.hpp>
  18. #include <boost/graph/visitors.hpp>
  19. #include <boost/graph/named_function_params.hpp>
  20. #include <boost/ref.hpp>
  21. #include <boost/implicit_cast.hpp>
  22. #include <boost/optional.hpp>
  23. #include <boost/parameter.hpp>
  24. #include <boost/concept/assert.hpp>
  25. #include <boost/tti/has_member_function.hpp>
  26. #include <vector>
  27. #include <utility>
  28. namespace boost {
  29. template <class Visitor, class Graph>
  30. class DFSVisitorConcept {
  31. public:
  32. void constraints() {
  33. BOOST_CONCEPT_ASSERT(( CopyConstructibleConcept<Visitor> ));
  34. vis.initialize_vertex(u, g);
  35. vis.start_vertex(u, g);
  36. vis.discover_vertex(u, g);
  37. vis.examine_edge(e, g);
  38. vis.tree_edge(e, g);
  39. vis.back_edge(e, g);
  40. vis.forward_or_cross_edge(e, g);
  41. // vis.finish_edge(e, g); // Optional for user
  42. vis.finish_vertex(u, g);
  43. }
  44. private:
  45. Visitor vis;
  46. Graph g;
  47. typename graph_traits<Graph>::vertex_descriptor u;
  48. typename graph_traits<Graph>::edge_descriptor e;
  49. };
  50. namespace detail {
  51. struct nontruth2 {
  52. template<class T, class T2>
  53. bool operator()(const T&, const T2&) const { return false; }
  54. };
  55. BOOST_TTI_HAS_MEMBER_FUNCTION(finish_edge)
  56. template <bool IsCallable> struct do_call_finish_edge {
  57. template <typename E, typename G, typename Vis>
  58. static void call_finish_edge(Vis& vis, const E& e, const G& g) {
  59. vis.finish_edge(e, g);
  60. }
  61. };
  62. template <> struct do_call_finish_edge<false> {
  63. template <typename E, typename G, typename Vis>
  64. static void call_finish_edge(Vis&, const E&, const G&) {}
  65. };
  66. template <typename E, typename G, typename Vis>
  67. void call_finish_edge(Vis& vis, const E& e, const G& g) { // Only call if method exists
  68. do_call_finish_edge<has_member_function_finish_edge<Vis, void>::value>::call_finish_edge(vis, e, g);
  69. }
  70. // Define BOOST_RECURSIVE_DFS to use older, recursive version.
  71. // It is retained for a while in order to perform performance
  72. // comparison.
  73. #ifndef BOOST_RECURSIVE_DFS
  74. // If the vertex u and the iterators ei and ei_end are thought of as the
  75. // context of the algorithm, each push and pop from the stack could
  76. // be thought of as a context shift.
  77. // Each pass through "while (ei != ei_end)" may refer to the out-edges of
  78. // an entirely different vertex, because the context of the algorithm
  79. // shifts every time a white adjacent vertex is discovered.
  80. // The corresponding context shift back from the adjacent vertex occurs
  81. // after all of its out-edges have been examined.
  82. //
  83. // See http://lists.boost.org/MailArchives/boost/msg48752.php for FAQ.
  84. template <class IncidenceGraph, class DFSVisitor, class ColorMap,
  85. class TerminatorFunc>
  86. void depth_first_visit_impl
  87. (const IncidenceGraph& g,
  88. typename graph_traits<IncidenceGraph>::vertex_descriptor u,
  89. DFSVisitor& vis,
  90. ColorMap color, TerminatorFunc func = TerminatorFunc())
  91. {
  92. BOOST_CONCEPT_ASSERT(( IncidenceGraphConcept<IncidenceGraph> ));
  93. BOOST_CONCEPT_ASSERT(( DFSVisitorConcept<DFSVisitor, IncidenceGraph> ));
  94. typedef typename graph_traits<IncidenceGraph>::vertex_descriptor Vertex;
  95. typedef typename graph_traits<IncidenceGraph>::edge_descriptor Edge;
  96. BOOST_CONCEPT_ASSERT(( ReadWritePropertyMapConcept<ColorMap, Vertex> ));
  97. typedef typename property_traits<ColorMap>::value_type ColorValue;
  98. BOOST_CONCEPT_ASSERT(( ColorValueConcept<ColorValue> ));
  99. typedef color_traits<ColorValue> Color;
  100. typedef typename graph_traits<IncidenceGraph>::out_edge_iterator Iter;
  101. typedef std::pair<Vertex, std::pair<boost::optional<Edge>, std::pair<Iter, Iter> > > VertexInfo;
  102. boost::optional<Edge> src_e;
  103. Iter ei, ei_end;
  104. std::vector<VertexInfo> stack;
  105. // Possible optimization for vector
  106. //stack.reserve(num_vertices(g));
  107. put(color, u, Color::gray());
  108. vis.discover_vertex(u, g);
  109. boost::tie(ei, ei_end) = out_edges(u, g);
  110. if (func(u, g)) {
  111. // If this vertex terminates the search, we push empty range
  112. stack.push_back(std::make_pair(u, std::make_pair(boost::optional<Edge>(), std::make_pair(ei_end, ei_end))));
  113. } else {
  114. stack.push_back(std::make_pair(u, std::make_pair(boost::optional<Edge>(), std::make_pair(ei, ei_end))));
  115. }
  116. while (!stack.empty()) {
  117. VertexInfo& back = stack.back();
  118. u = back.first;
  119. src_e = back.second.first;
  120. boost::tie(ei, ei_end) = back.second.second;
  121. stack.pop_back();
  122. while (ei != ei_end) {
  123. Vertex v = target(*ei, g);
  124. vis.examine_edge(*ei, g);
  125. ColorValue v_color = get(color, v);
  126. if (v_color == Color::white()) {
  127. vis.tree_edge(*ei, g);
  128. src_e = *ei;
  129. stack.push_back(std::make_pair(u, std::make_pair(src_e, std::make_pair(++ei, ei_end))));
  130. u = v;
  131. put(color, u, Color::gray());
  132. vis.discover_vertex(u, g);
  133. boost::tie(ei, ei_end) = out_edges(u, g);
  134. if (func(u, g)) {
  135. ei = ei_end;
  136. }
  137. } else {
  138. if (v_color == Color::gray()) {
  139. vis.back_edge(*ei, g);
  140. } else {
  141. vis.forward_or_cross_edge(*ei, g);
  142. }
  143. call_finish_edge(vis, *ei, g);
  144. ++ei;
  145. }
  146. }
  147. put(color, u, Color::black());
  148. vis.finish_vertex(u, g);
  149. if (src_e) call_finish_edge(vis, src_e.get(), g);
  150. }
  151. }
  152. #else // BOOST_RECURSIVE_DFS is defined
  153. template <class IncidenceGraph, class DFSVisitor, class ColorMap,
  154. class TerminatorFunc>
  155. void depth_first_visit_impl
  156. (const IncidenceGraph& g,
  157. typename graph_traits<IncidenceGraph>::vertex_descriptor u,
  158. DFSVisitor& vis, // pass-by-reference here, important!
  159. ColorMap color, TerminatorFunc func)
  160. {
  161. BOOST_CONCEPT_ASSERT(( IncidenceGraphConcept<IncidenceGraph> ));
  162. BOOST_CONCEPT_ASSERT(( DFSVisitorConcept<DFSVisitor, IncidenceGraph> ));
  163. typedef typename graph_traits<IncidenceGraph>::vertex_descriptor Vertex;
  164. BOOST_CONCEPT_ASSERT(( ReadWritePropertyMapConcept<ColorMap, Vertex> ));
  165. typedef typename property_traits<ColorMap>::value_type ColorValue;
  166. BOOST_CONCEPT_ASSERT(( ColorValueConcept<ColorValue> ));
  167. typedef color_traits<ColorValue> Color;
  168. typename graph_traits<IncidenceGraph>::out_edge_iterator ei, ei_end;
  169. put(color, u, Color::gray()); vis.discover_vertex(u, g);
  170. if (!func(u, g))
  171. for (boost::tie(ei, ei_end) = out_edges(u, g); ei != ei_end; ++ei) {
  172. Vertex v = target(*ei, g); vis.examine_edge(*ei, g);
  173. ColorValue v_color = get(color, v);
  174. if (v_color == Color::white()) { vis.tree_edge(*ei, g);
  175. depth_first_visit_impl(g, v, vis, color, func);
  176. } else if (v_color == Color::gray()) vis.back_edge(*ei, g);
  177. else vis.forward_or_cross_edge(*ei, g);
  178. call_finish_edge(vis, *ei, g);
  179. }
  180. put(color, u, Color::black()); vis.finish_vertex(u, g);
  181. }
  182. #endif
  183. } // namespace detail
  184. template <class VertexListGraph, class DFSVisitor, class ColorMap>
  185. void
  186. depth_first_search(const VertexListGraph& g, DFSVisitor vis, ColorMap color,
  187. typename graph_traits<VertexListGraph>::vertex_descriptor start_vertex)
  188. {
  189. typedef typename graph_traits<VertexListGraph>::vertex_descriptor Vertex;
  190. BOOST_CONCEPT_ASSERT(( DFSVisitorConcept<DFSVisitor, VertexListGraph> ));
  191. typedef typename property_traits<ColorMap>::value_type ColorValue;
  192. typedef color_traits<ColorValue> Color;
  193. typename graph_traits<VertexListGraph>::vertex_iterator ui, ui_end;
  194. for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui) {
  195. Vertex u = implicit_cast<Vertex>(*ui);
  196. put(color, u, Color::white()); vis.initialize_vertex(u, g);
  197. }
  198. if (start_vertex != detail::get_default_starting_vertex(g)){ vis.start_vertex(start_vertex, g);
  199. detail::depth_first_visit_impl(g, start_vertex, vis, color,
  200. detail::nontruth2());
  201. }
  202. for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui) {
  203. Vertex u = implicit_cast<Vertex>(*ui);
  204. ColorValue u_color = get(color, u);
  205. if (u_color == Color::white()) { vis.start_vertex(u, g);
  206. detail::depth_first_visit_impl(g, u, vis, color, detail::nontruth2());
  207. }
  208. }
  209. }
  210. template <class VertexListGraph, class DFSVisitor, class ColorMap>
  211. void
  212. depth_first_search(const VertexListGraph& g, DFSVisitor vis, ColorMap color)
  213. {
  214. typedef typename boost::graph_traits<VertexListGraph>::vertex_iterator vi;
  215. std::pair<vi, vi> verts = vertices(g);
  216. if (verts.first == verts.second)
  217. return;
  218. depth_first_search(g, vis, color, detail::get_default_starting_vertex(g));
  219. }
  220. template <class Visitors = null_visitor>
  221. class dfs_visitor {
  222. public:
  223. dfs_visitor() { }
  224. dfs_visitor(Visitors vis) : m_vis(vis) { }
  225. template <class Vertex, class Graph>
  226. void initialize_vertex(Vertex u, const Graph& g) {
  227. invoke_visitors(m_vis, u, g, ::boost::on_initialize_vertex());
  228. }
  229. template <class Vertex, class Graph>
  230. void start_vertex(Vertex u, const Graph& g) {
  231. invoke_visitors(m_vis, u, g, ::boost::on_start_vertex());
  232. }
  233. template <class Vertex, class Graph>
  234. void discover_vertex(Vertex u, const Graph& g) {
  235. invoke_visitors(m_vis, u, g, ::boost::on_discover_vertex());
  236. }
  237. template <class Edge, class Graph>
  238. void examine_edge(Edge u, const Graph& g) {
  239. invoke_visitors(m_vis, u, g, ::boost::on_examine_edge());
  240. }
  241. template <class Edge, class Graph>
  242. void tree_edge(Edge u, const Graph& g) {
  243. invoke_visitors(m_vis, u, g, ::boost::on_tree_edge());
  244. }
  245. template <class Edge, class Graph>
  246. void back_edge(Edge u, const Graph& g) {
  247. invoke_visitors(m_vis, u, g, ::boost::on_back_edge());
  248. }
  249. template <class Edge, class Graph>
  250. void forward_or_cross_edge(Edge u, const Graph& g) {
  251. invoke_visitors(m_vis, u, g, ::boost::on_forward_or_cross_edge());
  252. }
  253. template <class Edge, class Graph>
  254. void finish_edge(Edge u, const Graph& g) {
  255. invoke_visitors(m_vis, u, g, ::boost::on_finish_edge());
  256. }
  257. template <class Vertex, class Graph>
  258. void finish_vertex(Vertex u, const Graph& g) {
  259. invoke_visitors(m_vis, u, g, ::boost::on_finish_vertex());
  260. }
  261. BOOST_GRAPH_EVENT_STUB(on_initialize_vertex,dfs)
  262. BOOST_GRAPH_EVENT_STUB(on_start_vertex,dfs)
  263. BOOST_GRAPH_EVENT_STUB(on_discover_vertex,dfs)
  264. BOOST_GRAPH_EVENT_STUB(on_examine_edge,dfs)
  265. BOOST_GRAPH_EVENT_STUB(on_tree_edge,dfs)
  266. BOOST_GRAPH_EVENT_STUB(on_back_edge,dfs)
  267. BOOST_GRAPH_EVENT_STUB(on_forward_or_cross_edge,dfs)
  268. BOOST_GRAPH_EVENT_STUB(on_finish_edge,dfs)
  269. BOOST_GRAPH_EVENT_STUB(on_finish_vertex,dfs)
  270. protected:
  271. Visitors m_vis;
  272. };
  273. template <class Visitors>
  274. dfs_visitor<Visitors>
  275. make_dfs_visitor(Visitors vis) {
  276. return dfs_visitor<Visitors>(vis);
  277. }
  278. typedef dfs_visitor<> default_dfs_visitor;
  279. // Boost.Parameter named parameter variant
  280. namespace graph {
  281. namespace detail {
  282. template <typename Graph>
  283. struct depth_first_search_impl {
  284. typedef void result_type;
  285. template <typename ArgPack>
  286. void operator()(const Graph& g, const ArgPack& arg_pack) const {
  287. using namespace boost::graph::keywords;
  288. boost::depth_first_search(g,
  289. arg_pack[_visitor | make_dfs_visitor(null_visitor())],
  290. boost::detail::make_color_map_from_arg_pack(g, arg_pack),
  291. arg_pack[_root_vertex || boost::detail::get_default_starting_vertex_t<Graph>(g)]);
  292. }
  293. };
  294. }
  295. BOOST_GRAPH_MAKE_FORWARDING_FUNCTION(depth_first_search, 1, 4)
  296. }
  297. BOOST_GRAPH_MAKE_OLD_STYLE_PARAMETER_FUNCTION(depth_first_search, 1)
  298. template <class IncidenceGraph, class DFSVisitor, class ColorMap>
  299. void depth_first_visit
  300. (const IncidenceGraph& g,
  301. typename graph_traits<IncidenceGraph>::vertex_descriptor u,
  302. DFSVisitor vis, ColorMap color)
  303. {
  304. vis.start_vertex(u, g);
  305. detail::depth_first_visit_impl(g, u, vis, color, detail::nontruth2());
  306. }
  307. template <class IncidenceGraph, class DFSVisitor, class ColorMap,
  308. class TerminatorFunc>
  309. void depth_first_visit
  310. (const IncidenceGraph& g,
  311. typename graph_traits<IncidenceGraph>::vertex_descriptor u,
  312. DFSVisitor vis, ColorMap color, TerminatorFunc func = TerminatorFunc())
  313. {
  314. vis.start_vertex(u, g);
  315. detail::depth_first_visit_impl(g, u, vis, color, func);
  316. }
  317. } // namespace boost
  318. #ifdef BOOST_GRAPH_USE_MPI
  319. # include <boost/graph/distributed/depth_first_search.hpp>
  320. #endif
  321. #endif