depth_first_search.hpp 14 KB

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