PageRenderTime 27ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/avr-cpp-libs/foreign/cpp/boost/graph/depth_first_search.hpp

https://bitbucket.org/nazemnykh_anton/avr-cpp-libs
C++ Header | 331 lines | 265 code | 36 blank | 30 comment | 33 complexity | cce9dae9e6e5a106f6ef42193ad805ed MD5 | raw file
  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 <vector>
  23. #include <utility>
  24. namespace boost {
  25. template <class Visitor, class Graph>
  26. class DFSVisitorConcept {
  27. public:
  28. void constraints() {
  29. function_requires< CopyConstructibleConcept<Visitor> >();
  30. vis.initialize_vertex(u, g);
  31. vis.start_vertex(u, g);
  32. vis.discover_vertex(u, g);
  33. vis.examine_edge(e, g);
  34. vis.tree_edge(e, g);
  35. vis.back_edge(e, g);
  36. vis.forward_or_cross_edge(e, g);
  37. vis.finish_vertex(u, g);
  38. }
  39. private:
  40. Visitor vis;
  41. Graph g;
  42. typename graph_traits<Graph>::vertex_descriptor u;
  43. typename graph_traits<Graph>::edge_descriptor e;
  44. };
  45. namespace detail {
  46. struct nontruth2 {
  47. template<class T, class T2>
  48. bool operator()(const T&, const T2&) const { return false; }
  49. };
  50. // Define BOOST_RECURSIVE_DFS to use older, recursive version.
  51. // It is retained for a while in order to perform performance
  52. // comparison.
  53. #ifndef BOOST_RECURSIVE_DFS
  54. // If the vertex u and the iterators ei and ei_end are thought of as the
  55. // context of the algorithm, each push and pop from the stack could
  56. // be thought of as a context shift.
  57. // Each pass through "while (ei != ei_end)" may refer to the out-edges of
  58. // an entirely different vertex, because the context of the algorithm
  59. // shifts every time a white adjacent vertex is discovered.
  60. // The corresponding context shift back from the adjacent vertex occurs
  61. // after all of its out-edges have been examined.
  62. //
  63. // See http://lists.boost.org/MailArchives/boost/msg48752.php for FAQ.
  64. template <class IncidenceGraph, class DFSVisitor, class ColorMap,
  65. class TerminatorFunc>
  66. void depth_first_visit_impl
  67. (const IncidenceGraph& g,
  68. typename graph_traits<IncidenceGraph>::vertex_descriptor u,
  69. DFSVisitor& vis,
  70. ColorMap color, TerminatorFunc func = TerminatorFunc())
  71. {
  72. function_requires<IncidenceGraphConcept<IncidenceGraph> >();
  73. function_requires<DFSVisitorConcept<DFSVisitor, IncidenceGraph> >();
  74. typedef typename graph_traits<IncidenceGraph>::vertex_descriptor Vertex;
  75. function_requires< ReadWritePropertyMapConcept<ColorMap, Vertex> >();
  76. typedef typename property_traits<ColorMap>::value_type ColorValue;
  77. function_requires< ColorValueConcept<ColorValue> >();
  78. typedef color_traits<ColorValue> Color;
  79. typedef typename graph_traits<IncidenceGraph>::out_edge_iterator Iter;
  80. typedef std::pair<Vertex, std::pair<Iter, Iter> > VertexInfo;
  81. Iter ei, ei_end;
  82. std::vector<VertexInfo> stack;
  83. // Possible optimization for vector
  84. //stack.reserve(num_vertices(g));
  85. typedef typename unwrap_reference<TerminatorFunc>::type TF;
  86. put(color, u, Color::gray());
  87. vis.discover_vertex(u, g);
  88. boost::tie(ei, ei_end) = out_edges(u, g);
  89. // Variable is needed to workaround a borland bug.
  90. TF& fn = static_cast<TF&>(func);
  91. if (fn(u, g)) {
  92. // If this vertex terminates the search, we push empty range
  93. stack.push_back(std::make_pair(u, std::make_pair(ei_end, ei_end)));
  94. } else {
  95. stack.push_back(std::make_pair(u, std::make_pair(ei, ei_end)));
  96. }
  97. while (!stack.empty()) {
  98. VertexInfo& back = stack.back();
  99. u = back.first;
  100. boost::tie(ei, ei_end) = back.second;
  101. stack.pop_back();
  102. while (ei != ei_end) {
  103. Vertex v = target(*ei, g);
  104. vis.examine_edge(*ei, g);
  105. ColorValue v_color = get(color, v);
  106. if (v_color == Color::white()) {
  107. vis.tree_edge(*ei, g);
  108. stack.push_back(std::make_pair(u, std::make_pair(++ei, ei_end)));
  109. u = v;
  110. put(color, u, Color::gray());
  111. vis.discover_vertex(u, g);
  112. boost::tie(ei, ei_end) = out_edges(u, g);
  113. if (fn(u, g)) {
  114. ei = ei_end;
  115. }
  116. } else if (v_color == Color::gray()) {
  117. vis.back_edge(*ei, g);
  118. ++ei;
  119. } else {
  120. vis.forward_or_cross_edge(*ei, g);
  121. ++ei;
  122. }
  123. }
  124. put(color, u, Color::black());
  125. vis.finish_vertex(u, g);
  126. }
  127. }
  128. #else // BOOST_RECURSIVE_DFS is defined
  129. template <class IncidenceGraph, class DFSVisitor, class ColorMap,
  130. class TerminatorFunc>
  131. void depth_first_visit_impl
  132. (const IncidenceGraph& g,
  133. typename graph_traits<IncidenceGraph>::vertex_descriptor u,
  134. DFSVisitor& vis, // pass-by-reference here, important!
  135. ColorMap color, TerminatorFunc func)
  136. {
  137. function_requires<IncidenceGraphConcept<IncidenceGraph> >();
  138. function_requires<DFSVisitorConcept<DFSVisitor, IncidenceGraph> >();
  139. typedef typename graph_traits<IncidenceGraph>::vertex_descriptor Vertex;
  140. function_requires< ReadWritePropertyMapConcept<ColorMap, Vertex> >();
  141. typedef typename property_traits<ColorMap>::value_type ColorValue;
  142. function_requires< ColorValueConcept<ColorValue> >();
  143. typedef color_traits<ColorValue> Color;
  144. typename graph_traits<IncidenceGraph>::out_edge_iterator ei, ei_end;
  145. put(color, u, Color::gray()); vis.discover_vertex(u, g);
  146. typedef typename unwrap_reference<TerminatorFunc>::type TF;
  147. // Variable is needed to workaround a borland bug.
  148. TF& fn = static_cast<TF&>(func);
  149. if (!fn(u, g))
  150. for (boost::tie(ei, ei_end) = out_edges(u, g); ei != ei_end; ++ei) {
  151. Vertex v = target(*ei, g); vis.examine_edge(*ei, g);
  152. ColorValue v_color = get(color, v);
  153. if (v_color == Color::white()) { vis.tree_edge(*ei, g);
  154. depth_first_visit_impl(g, v, vis, color, func);
  155. } else if (v_color == Color::gray()) vis.back_edge(*ei, g);
  156. else vis.forward_or_cross_edge(*ei, g);
  157. }
  158. put(color, u, Color::black()); vis.finish_vertex(u, g);
  159. }
  160. #endif
  161. } // namespace detail
  162. template <class VertexListGraph, class DFSVisitor, class ColorMap>
  163. void
  164. depth_first_search(const VertexListGraph& g, DFSVisitor vis, ColorMap color,
  165. typename graph_traits<VertexListGraph>::vertex_descriptor start_vertex)
  166. {
  167. typedef typename graph_traits<VertexListGraph>::vertex_descriptor Vertex;
  168. function_requires<DFSVisitorConcept<DFSVisitor, VertexListGraph> >();
  169. typedef typename property_traits<ColorMap>::value_type ColorValue;
  170. typedef color_traits<ColorValue> Color;
  171. typename graph_traits<VertexListGraph>::vertex_iterator ui, ui_end;
  172. for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui) {
  173. Vertex u = implicit_cast<Vertex>(*ui);
  174. put(color, u, Color::white()); vis.initialize_vertex(u, g);
  175. }
  176. if (start_vertex != implicit_cast<Vertex>(*vertices(g).first)){ vis.start_vertex(start_vertex, g);
  177. detail::depth_first_visit_impl(g, start_vertex, vis, color,
  178. detail::nontruth2());
  179. }
  180. for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui) {
  181. Vertex u = implicit_cast<Vertex>(*ui);
  182. ColorValue u_color = get(color, u);
  183. if (u_color == Color::white()) { vis.start_vertex(u, g);
  184. detail::depth_first_visit_impl(g, u, vis, color, detail::nontruth2());
  185. }
  186. }
  187. }
  188. template <class VertexListGraph, class DFSVisitor, class ColorMap>
  189. void
  190. depth_first_search(const VertexListGraph& g, DFSVisitor vis, ColorMap color)
  191. {
  192. typedef typename boost::graph_traits<VertexListGraph>::vertex_iterator vi;
  193. std::pair<vi, vi> verts = vertices(g);
  194. if (verts.first == verts.second)
  195. return;
  196. depth_first_search(g, vis, color, *verts.first);
  197. }
  198. template <class Visitors = null_visitor>
  199. class dfs_visitor {
  200. public:
  201. dfs_visitor() { }
  202. dfs_visitor(Visitors vis) : m_vis(vis) { }
  203. template <class Vertex, class Graph>
  204. void initialize_vertex(Vertex u, const Graph& g) {
  205. invoke_visitors(m_vis, u, g, ::boost::on_initialize_vertex());
  206. }
  207. template <class Vertex, class Graph>
  208. void start_vertex(Vertex u, const Graph& g) {
  209. invoke_visitors(m_vis, u, g, ::boost::on_start_vertex());
  210. }
  211. template <class Vertex, class Graph>
  212. void discover_vertex(Vertex u, const Graph& g) {
  213. invoke_visitors(m_vis, u, g, ::boost::on_discover_vertex());
  214. }
  215. template <class Edge, class Graph>
  216. void examine_edge(Edge u, const Graph& g) {
  217. invoke_visitors(m_vis, u, g, ::boost::on_examine_edge());
  218. }
  219. template <class Edge, class Graph>
  220. void tree_edge(Edge u, const Graph& g) {
  221. invoke_visitors(m_vis, u, g, ::boost::on_tree_edge());
  222. }
  223. template <class Edge, class Graph>
  224. void back_edge(Edge u, const Graph& g) {
  225. invoke_visitors(m_vis, u, g, ::boost::on_back_edge());
  226. }
  227. template <class Edge, class Graph>
  228. void forward_or_cross_edge(Edge u, const Graph& g) {
  229. invoke_visitors(m_vis, u, g, ::boost::on_forward_or_cross_edge());
  230. }
  231. template <class Vertex, class Graph>
  232. void finish_vertex(Vertex u, const Graph& g) {
  233. invoke_visitors(m_vis, u, g, ::boost::on_finish_vertex());
  234. }
  235. BOOST_GRAPH_EVENT_STUB(on_initialize_vertex,dfs)
  236. BOOST_GRAPH_EVENT_STUB(on_start_vertex,dfs)
  237. BOOST_GRAPH_EVENT_STUB(on_discover_vertex,dfs)
  238. BOOST_GRAPH_EVENT_STUB(on_examine_edge,dfs)
  239. BOOST_GRAPH_EVENT_STUB(on_tree_edge,dfs)
  240. BOOST_GRAPH_EVENT_STUB(on_back_edge,dfs)
  241. BOOST_GRAPH_EVENT_STUB(on_forward_or_cross_edge,dfs)
  242. BOOST_GRAPH_EVENT_STUB(on_finish_vertex,dfs)
  243. protected:
  244. Visitors m_vis;
  245. };
  246. template <class Visitors>
  247. dfs_visitor<Visitors>
  248. make_dfs_visitor(Visitors vis) {
  249. return dfs_visitor<Visitors>(vis);
  250. }
  251. typedef dfs_visitor<> default_dfs_visitor;
  252. // Named Parameter Variant
  253. template <class VertexListGraph, class P, class T, class R>
  254. void
  255. depth_first_search(const VertexListGraph& g,
  256. const bgl_named_params<P, T, R>& params)
  257. {
  258. typedef typename boost::graph_traits<VertexListGraph>::vertex_iterator vi;
  259. std::pair<vi, vi> verts = vertices(g);
  260. if (verts.first == verts.second)
  261. return;
  262. using namespace boost::graph::keywords;
  263. typedef bgl_named_params<P, T, R> params_type;
  264. BOOST_GRAPH_DECLARE_CONVERTED_PARAMETERS(params_type, params)
  265. depth_first_search
  266. (g,
  267. arg_pack[_visitor | make_dfs_visitor(null_visitor())],
  268. boost::detail::make_color_map_from_arg_pack(g, arg_pack),
  269. arg_pack[_root_vertex | *vertices(g).first]
  270. );
  271. }
  272. template <class IncidenceGraph, class DFSVisitor, class ColorMap>
  273. void depth_first_visit
  274. (const IncidenceGraph& g,
  275. typename graph_traits<IncidenceGraph>::vertex_descriptor u,
  276. DFSVisitor vis, ColorMap color)
  277. {
  278. vis.start_vertex(u, g);
  279. detail::depth_first_visit_impl(g, u, vis, color, detail::nontruth2());
  280. }
  281. template <class IncidenceGraph, class DFSVisitor, class ColorMap,
  282. class TerminatorFunc>
  283. void depth_first_visit
  284. (const IncidenceGraph& g,
  285. typename graph_traits<IncidenceGraph>::vertex_descriptor u,
  286. DFSVisitor vis, ColorMap color, TerminatorFunc func = TerminatorFunc())
  287. {
  288. vis.start_vertex(u, g);
  289. detail::depth_first_visit_impl(g, u, vis, color, func);
  290. }
  291. } // namespace boost
  292. #ifdef BOOST_GRAPH_USE_MPI
  293. # include <boost/graph/distributed/depth_first_search.hpp>
  294. #endif
  295. #endif