neighbor_bfs.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. //
  2. //=======================================================================
  3. // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
  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. //
  11. #ifndef BOOST_GRAPH_NEIGHBOR_BREADTH_FIRST_SEARCH_HPP
  12. #define BOOST_GRAPH_NEIGHBOR_BREADTH_FIRST_SEARCH_HPP
  13. /*
  14. Neighbor Breadth First Search
  15. Like BFS, but traverses in-edges as well as out-edges.
  16. (for directed graphs only. use normal BFS for undirected graphs)
  17. */
  18. #include <boost/config.hpp>
  19. #include <boost/ref.hpp>
  20. #include <vector>
  21. #include <boost/pending/queue.hpp>
  22. #include <boost/graph/graph_traits.hpp>
  23. #include <boost/graph/graph_concepts.hpp>
  24. #include <boost/graph/visitors.hpp>
  25. #include <boost/graph/named_function_params.hpp>
  26. #include <boost/concept/assert.hpp>
  27. namespace boost
  28. {
  29. template < class Visitor, class Graph > struct NeighborBFSVisitorConcept
  30. {
  31. void constraints()
  32. {
  33. BOOST_CONCEPT_ASSERT((CopyConstructibleConcept< Visitor >));
  34. vis.initialize_vertex(u, g);
  35. vis.discover_vertex(u, g);
  36. vis.examine_vertex(u, g);
  37. vis.examine_out_edge(e, g);
  38. vis.examine_in_edge(e, g);
  39. vis.tree_out_edge(e, g);
  40. vis.tree_in_edge(e, g);
  41. vis.non_tree_out_edge(e, g);
  42. vis.non_tree_in_edge(e, g);
  43. vis.gray_target(e, g);
  44. vis.black_target(e, g);
  45. vis.gray_source(e, g);
  46. vis.black_source(e, g);
  47. vis.finish_vertex(u, g);
  48. }
  49. Visitor vis;
  50. Graph g;
  51. typename graph_traits< Graph >::vertex_descriptor u;
  52. typename graph_traits< Graph >::edge_descriptor e;
  53. };
  54. template < class Visitors = null_visitor > class neighbor_bfs_visitor
  55. {
  56. public:
  57. neighbor_bfs_visitor(Visitors vis = Visitors()) : m_vis(vis) {}
  58. template < class Vertex, class Graph >
  59. void initialize_vertex(Vertex u, Graph& g)
  60. {
  61. invoke_visitors(m_vis, u, g, on_initialize_vertex());
  62. }
  63. template < class Vertex, class Graph >
  64. void discover_vertex(Vertex u, Graph& g)
  65. {
  66. invoke_visitors(m_vis, u, g, on_discover_vertex());
  67. }
  68. template < class Vertex, class Graph >
  69. void examine_vertex(Vertex u, Graph& g)
  70. {
  71. invoke_visitors(m_vis, u, g, on_examine_vertex());
  72. }
  73. template < class Edge, class Graph > void examine_out_edge(Edge e, Graph& g)
  74. {
  75. invoke_visitors(m_vis, e, g, on_examine_edge());
  76. }
  77. template < class Edge, class Graph > void tree_out_edge(Edge e, Graph& g)
  78. {
  79. invoke_visitors(m_vis, e, g, on_tree_edge());
  80. }
  81. template < class Edge, class Graph >
  82. void non_tree_out_edge(Edge e, Graph& g)
  83. {
  84. invoke_visitors(m_vis, e, g, on_non_tree_edge());
  85. }
  86. template < class Edge, class Graph > void gray_target(Edge e, Graph& g)
  87. {
  88. invoke_visitors(m_vis, e, g, on_gray_target());
  89. }
  90. template < class Edge, class Graph > void black_target(Edge e, Graph& g)
  91. {
  92. invoke_visitors(m_vis, e, g, on_black_target());
  93. }
  94. template < class Edge, class Graph > void examine_in_edge(Edge e, Graph& g)
  95. {
  96. invoke_visitors(m_vis, e, g, on_examine_edge());
  97. }
  98. template < class Edge, class Graph > void tree_in_edge(Edge e, Graph& g)
  99. {
  100. invoke_visitors(m_vis, e, g, on_tree_edge());
  101. }
  102. template < class Edge, class Graph > void non_tree_in_edge(Edge e, Graph& g)
  103. {
  104. invoke_visitors(m_vis, e, g, on_non_tree_edge());
  105. }
  106. template < class Edge, class Graph > void gray_source(Edge e, Graph& g)
  107. {
  108. invoke_visitors(m_vis, e, g, on_gray_target());
  109. }
  110. template < class Edge, class Graph > void black_source(Edge e, Graph& g)
  111. {
  112. invoke_visitors(m_vis, e, g, on_black_target());
  113. }
  114. template < class Vertex, class Graph >
  115. void finish_vertex(Vertex u, Graph& g)
  116. {
  117. invoke_visitors(m_vis, u, g, on_finish_vertex());
  118. }
  119. protected:
  120. Visitors m_vis;
  121. };
  122. template < class Visitors >
  123. neighbor_bfs_visitor< Visitors > make_neighbor_bfs_visitor(Visitors vis)
  124. {
  125. return neighbor_bfs_visitor< Visitors >(vis);
  126. }
  127. namespace detail
  128. {
  129. template < class BidirectionalGraph, class Buffer, class BFSVisitor,
  130. class ColorMap >
  131. void neighbor_bfs_impl(const BidirectionalGraph& g,
  132. typename graph_traits< BidirectionalGraph >::vertex_descriptor s,
  133. Buffer& Q, BFSVisitor vis, ColorMap color)
  134. {
  135. BOOST_CONCEPT_ASSERT((BidirectionalGraphConcept< BidirectionalGraph >));
  136. typedef graph_traits< BidirectionalGraph > GTraits;
  137. typedef typename GTraits::vertex_descriptor Vertex;
  138. typedef typename GTraits::edge_descriptor Edge;
  139. BOOST_CONCEPT_ASSERT(
  140. (NeighborBFSVisitorConcept< BFSVisitor, BidirectionalGraph >));
  141. BOOST_CONCEPT_ASSERT((ReadWritePropertyMapConcept< ColorMap, Vertex >));
  142. typedef typename property_traits< ColorMap >::value_type ColorValue;
  143. typedef color_traits< ColorValue > Color;
  144. put(color, s, Color::gray());
  145. vis.discover_vertex(s, g);
  146. Q.push(s);
  147. while (!Q.empty())
  148. {
  149. Vertex u = Q.top();
  150. Q.pop(); // pop before push to avoid problem if Q is priority_queue.
  151. vis.examine_vertex(u, g);
  152. typename GTraits::out_edge_iterator ei, ei_end;
  153. for (boost::tie(ei, ei_end) = out_edges(u, g); ei != ei_end; ++ei)
  154. {
  155. Edge e = *ei;
  156. vis.examine_out_edge(e, g);
  157. Vertex v = target(e, g);
  158. ColorValue v_color = get(color, v);
  159. if (v_color == Color::white())
  160. {
  161. vis.tree_out_edge(e, g);
  162. put(color, v, Color::gray());
  163. vis.discover_vertex(v, g);
  164. Q.push(v);
  165. }
  166. else
  167. {
  168. vis.non_tree_out_edge(e, g);
  169. if (v_color == Color::gray())
  170. vis.gray_target(e, g);
  171. else
  172. vis.black_target(e, g);
  173. }
  174. } // for out-edges
  175. typename GTraits::in_edge_iterator in_ei, in_ei_end;
  176. for (boost::tie(in_ei, in_ei_end) = in_edges(u, g);
  177. in_ei != in_ei_end; ++in_ei)
  178. {
  179. Edge e = *in_ei;
  180. vis.examine_in_edge(e, g);
  181. Vertex v = source(e, g);
  182. ColorValue v_color = get(color, v);
  183. if (v_color == Color::white())
  184. {
  185. vis.tree_in_edge(e, g);
  186. put(color, v, Color::gray());
  187. vis.discover_vertex(v, g);
  188. Q.push(v);
  189. }
  190. else
  191. {
  192. vis.non_tree_in_edge(e, g);
  193. if (v_color == Color::gray())
  194. vis.gray_source(e, g);
  195. else
  196. vis.black_source(e, g);
  197. }
  198. } // for in-edges
  199. put(color, u, Color::black());
  200. vis.finish_vertex(u, g);
  201. } // while
  202. }
  203. template < class VertexListGraph, class ColorMap, class BFSVisitor, class P,
  204. class T, class R >
  205. void neighbor_bfs_helper(VertexListGraph& g,
  206. typename graph_traits< VertexListGraph >::vertex_descriptor s,
  207. ColorMap color, BFSVisitor vis,
  208. const bgl_named_params< P, T, R >& params)
  209. {
  210. typedef graph_traits< VertexListGraph > Traits;
  211. // Buffer default
  212. typedef typename Traits::vertex_descriptor Vertex;
  213. typedef boost::queue< Vertex > queue_t;
  214. queue_t Q;
  215. // Initialization
  216. typedef typename property_traits< ColorMap >::value_type ColorValue;
  217. typedef color_traits< ColorValue > Color;
  218. typename boost::graph_traits< VertexListGraph >::vertex_iterator i,
  219. i_end;
  220. for (boost::tie(i, i_end) = vertices(g); i != i_end; ++i)
  221. {
  222. put(color, *i, Color::white());
  223. vis.initialize_vertex(*i, g);
  224. }
  225. neighbor_bfs_impl(g, s,
  226. choose_param(get_param(params, buffer_param_t()), boost::ref(Q))
  227. .get(),
  228. vis, color);
  229. }
  230. //-------------------------------------------------------------------------
  231. // Choose between default color and color parameters. Using
  232. // function dispatching so that we don't require vertex index if
  233. // the color default is not being used.
  234. template < class ColorMap > struct neighbor_bfs_dispatch
  235. {
  236. template < class VertexListGraph, class P, class T, class R >
  237. static void apply(VertexListGraph& g,
  238. typename graph_traits< VertexListGraph >::vertex_descriptor s,
  239. const bgl_named_params< P, T, R >& params, ColorMap color)
  240. {
  241. neighbor_bfs_helper(g, s, color,
  242. choose_param(get_param(params, graph_visitor),
  243. make_neighbor_bfs_visitor(null_visitor())),
  244. params);
  245. }
  246. };
  247. template <> struct neighbor_bfs_dispatch< param_not_found >
  248. {
  249. template < class VertexListGraph, class P, class T, class R >
  250. static void apply(VertexListGraph& g,
  251. typename graph_traits< VertexListGraph >::vertex_descriptor s,
  252. const bgl_named_params< P, T, R >& params, param_not_found)
  253. {
  254. std::vector< default_color_type > color_vec(num_vertices(g));
  255. null_visitor null_vis;
  256. neighbor_bfs_helper(g, s,
  257. make_iterator_property_map(color_vec.begin(),
  258. choose_const_pmap(
  259. get_param(params, vertex_index), g, vertex_index),
  260. color_vec[0]),
  261. choose_param(get_param(params, graph_visitor),
  262. make_neighbor_bfs_visitor(null_vis)),
  263. params);
  264. }
  265. };
  266. } // namespace detail
  267. // Named Parameter Variant
  268. template < class VertexListGraph, class P, class T, class R >
  269. void neighbor_breadth_first_search(const VertexListGraph& g,
  270. typename graph_traits< VertexListGraph >::vertex_descriptor s,
  271. const bgl_named_params< P, T, R >& params)
  272. {
  273. // The graph is passed by *const* reference so that graph adaptors
  274. // (temporaries) can be passed into this function. However, the
  275. // graph is not really const since we may write to property maps
  276. // of the graph.
  277. VertexListGraph& ng = const_cast< VertexListGraph& >(g);
  278. typedef typename get_param_type< vertex_color_t,
  279. bgl_named_params< P, T, R > >::type C;
  280. detail::neighbor_bfs_dispatch< C >::apply(
  281. ng, s, params, get_param(params, vertex_color));
  282. }
  283. // This version does not initialize colors, user has to.
  284. template < class IncidenceGraph, class P, class T, class R >
  285. void neighbor_breadth_first_visit(IncidenceGraph& g,
  286. typename graph_traits< IncidenceGraph >::vertex_descriptor s,
  287. const bgl_named_params< P, T, R >& params)
  288. {
  289. typedef graph_traits< IncidenceGraph > Traits;
  290. // Buffer default
  291. typedef boost::queue< typename Traits::vertex_descriptor > queue_t;
  292. queue_t Q;
  293. detail::neighbor_bfs_impl(g, s,
  294. choose_param(get_param(params, buffer_param_t()), boost::ref(Q)).get(),
  295. choose_param(get_param(params, graph_visitor),
  296. make_neighbor_bfs_visitor(null_visitor())),
  297. choose_pmap(get_param(params, vertex_color), g, vertex_color));
  298. }
  299. } // namespace boost
  300. #endif // BOOST_GRAPH_NEIGHBOR_BREADTH_FIRST_SEARCH_HPP