breadth_first_search.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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_BREADTH_FIRST_SEARCH_HPP
  12. #define BOOST_GRAPH_BREADTH_FIRST_SEARCH_HPP
  13. /*
  14. Breadth First Search Algorithm (Cormen, Leiserson, and Rivest p. 470)
  15. */
  16. #include <boost/config.hpp>
  17. #include <vector>
  18. #include <boost/pending/queue.hpp>
  19. #include <boost/graph/graph_traits.hpp>
  20. #include <boost/graph/graph_concepts.hpp>
  21. #include <boost/graph/visitors.hpp>
  22. #include <boost/graph/named_function_params.hpp>
  23. #include <boost/graph/overloading.hpp>
  24. #include <boost/graph/graph_concepts.hpp>
  25. #include <boost/graph/two_bit_color_map.hpp>
  26. #include <boost/graph/detail/mpi_include.hpp>
  27. #include <boost/concept/assert.hpp>
  28. #include BOOST_GRAPH_MPI_INCLUDE(< boost / graph / distributed / concepts.hpp >)
  29. namespace boost
  30. {
  31. template < class Visitor, class Graph > struct BFSVisitorConcept
  32. {
  33. void constraints()
  34. {
  35. BOOST_CONCEPT_ASSERT((CopyConstructibleConcept< Visitor >));
  36. vis.initialize_vertex(u, g);
  37. vis.discover_vertex(u, g);
  38. vis.examine_vertex(u, g);
  39. vis.examine_edge(e, g);
  40. vis.tree_edge(e, g);
  41. vis.non_tree_edge(e, g);
  42. vis.gray_target(e, g);
  43. vis.black_target(e, g);
  44. vis.finish_vertex(u, g);
  45. }
  46. Visitor vis;
  47. Graph g;
  48. typename graph_traits< Graph >::vertex_descriptor u;
  49. typename graph_traits< Graph >::edge_descriptor e;
  50. };
  51. // Multiple-source version
  52. template < class IncidenceGraph, class Buffer, class BFSVisitor, class ColorMap,
  53. class SourceIterator >
  54. void breadth_first_visit(const IncidenceGraph& g, SourceIterator sources_begin,
  55. SourceIterator sources_end, Buffer& Q, BFSVisitor vis, ColorMap color)
  56. {
  57. BOOST_CONCEPT_ASSERT((IncidenceGraphConcept< IncidenceGraph >));
  58. typedef graph_traits< IncidenceGraph > GTraits;
  59. typedef typename GTraits::vertex_descriptor Vertex;
  60. BOOST_CONCEPT_ASSERT((BFSVisitorConcept< BFSVisitor, IncidenceGraph >));
  61. BOOST_CONCEPT_ASSERT((ReadWritePropertyMapConcept< ColorMap, Vertex >));
  62. typedef typename property_traits< ColorMap >::value_type ColorValue;
  63. typedef color_traits< ColorValue > Color;
  64. typename GTraits::out_edge_iterator ei, ei_end;
  65. for (; sources_begin != sources_end; ++sources_begin)
  66. {
  67. Vertex s = *sources_begin;
  68. put(color, s, Color::gray());
  69. vis.discover_vertex(s, g);
  70. Q.push(s);
  71. }
  72. while (!Q.empty())
  73. {
  74. Vertex u = Q.top();
  75. Q.pop();
  76. vis.examine_vertex(u, g);
  77. for (boost::tie(ei, ei_end) = out_edges(u, g); ei != ei_end; ++ei)
  78. {
  79. Vertex v = target(*ei, g);
  80. vis.examine_edge(*ei, g);
  81. ColorValue v_color = get(color, v);
  82. if (v_color == Color::white())
  83. {
  84. vis.tree_edge(*ei, g);
  85. put(color, v, Color::gray());
  86. vis.discover_vertex(v, g);
  87. Q.push(v);
  88. }
  89. else
  90. {
  91. vis.non_tree_edge(*ei, g);
  92. if (v_color == Color::gray())
  93. vis.gray_target(*ei, g);
  94. else
  95. vis.black_target(*ei, g);
  96. }
  97. } // end for
  98. put(color, u, Color::black());
  99. vis.finish_vertex(u, g);
  100. } // end while
  101. } // breadth_first_visit
  102. // Single-source version
  103. template < class IncidenceGraph, class Buffer, class BFSVisitor,
  104. class ColorMap >
  105. void breadth_first_visit(const IncidenceGraph& g,
  106. typename graph_traits< IncidenceGraph >::vertex_descriptor s, Buffer& Q,
  107. BFSVisitor vis, ColorMap color)
  108. {
  109. typename graph_traits< IncidenceGraph >::vertex_descriptor sources[1]
  110. = { s };
  111. breadth_first_visit(g, sources, sources + 1, Q, vis, color);
  112. }
  113. template < class VertexListGraph, class SourceIterator, class Buffer,
  114. class BFSVisitor, class ColorMap >
  115. void breadth_first_search(const VertexListGraph& g,
  116. SourceIterator sources_begin, SourceIterator sources_end, Buffer& Q,
  117. BFSVisitor vis, ColorMap color)
  118. {
  119. // Initialization
  120. typedef typename property_traits< ColorMap >::value_type ColorValue;
  121. typedef color_traits< ColorValue > Color;
  122. typename boost::graph_traits< VertexListGraph >::vertex_iterator i, i_end;
  123. for (boost::tie(i, i_end) = vertices(g); i != i_end; ++i)
  124. {
  125. vis.initialize_vertex(*i, g);
  126. put(color, *i, Color::white());
  127. }
  128. breadth_first_visit(g, sources_begin, sources_end, Q, vis, color);
  129. }
  130. template < class VertexListGraph, class Buffer, class BFSVisitor,
  131. class ColorMap >
  132. void breadth_first_search(const VertexListGraph& g,
  133. typename graph_traits< VertexListGraph >::vertex_descriptor s, Buffer& Q,
  134. BFSVisitor vis, ColorMap color)
  135. {
  136. typename graph_traits< VertexListGraph >::vertex_descriptor sources[1]
  137. = { s };
  138. breadth_first_search(g, sources, sources + 1, Q, vis, color);
  139. }
  140. namespace graph
  141. {
  142. struct bfs_visitor_event_not_overridden
  143. {
  144. };
  145. }
  146. template < class Visitors = null_visitor > class bfs_visitor
  147. {
  148. public:
  149. bfs_visitor() {}
  150. bfs_visitor(Visitors vis) : m_vis(vis) {}
  151. template < class Vertex, class Graph >
  152. graph::bfs_visitor_event_not_overridden initialize_vertex(
  153. Vertex u, Graph& g)
  154. {
  155. invoke_visitors(m_vis, u, g, ::boost::on_initialize_vertex());
  156. return graph::bfs_visitor_event_not_overridden();
  157. }
  158. template < class Vertex, class Graph >
  159. graph::bfs_visitor_event_not_overridden discover_vertex(Vertex u, Graph& g)
  160. {
  161. invoke_visitors(m_vis, u, g, ::boost::on_discover_vertex());
  162. return graph::bfs_visitor_event_not_overridden();
  163. }
  164. template < class Vertex, class Graph >
  165. graph::bfs_visitor_event_not_overridden examine_vertex(Vertex u, Graph& g)
  166. {
  167. invoke_visitors(m_vis, u, g, ::boost::on_examine_vertex());
  168. return graph::bfs_visitor_event_not_overridden();
  169. }
  170. template < class Edge, class Graph >
  171. graph::bfs_visitor_event_not_overridden examine_edge(Edge e, Graph& g)
  172. {
  173. invoke_visitors(m_vis, e, g, ::boost::on_examine_edge());
  174. return graph::bfs_visitor_event_not_overridden();
  175. }
  176. template < class Edge, class Graph >
  177. graph::bfs_visitor_event_not_overridden tree_edge(Edge e, Graph& g)
  178. {
  179. invoke_visitors(m_vis, e, g, ::boost::on_tree_edge());
  180. return graph::bfs_visitor_event_not_overridden();
  181. }
  182. template < class Edge, class Graph >
  183. graph::bfs_visitor_event_not_overridden non_tree_edge(Edge e, Graph& g)
  184. {
  185. invoke_visitors(m_vis, e, g, ::boost::on_non_tree_edge());
  186. return graph::bfs_visitor_event_not_overridden();
  187. }
  188. template < class Edge, class Graph >
  189. graph::bfs_visitor_event_not_overridden gray_target(Edge e, Graph& g)
  190. {
  191. invoke_visitors(m_vis, e, g, ::boost::on_gray_target());
  192. return graph::bfs_visitor_event_not_overridden();
  193. }
  194. template < class Edge, class Graph >
  195. graph::bfs_visitor_event_not_overridden black_target(Edge e, Graph& g)
  196. {
  197. invoke_visitors(m_vis, e, g, ::boost::on_black_target());
  198. return graph::bfs_visitor_event_not_overridden();
  199. }
  200. template < class Vertex, class Graph >
  201. graph::bfs_visitor_event_not_overridden finish_vertex(Vertex u, Graph& g)
  202. {
  203. invoke_visitors(m_vis, u, g, ::boost::on_finish_vertex());
  204. return graph::bfs_visitor_event_not_overridden();
  205. }
  206. BOOST_GRAPH_EVENT_STUB(on_initialize_vertex, bfs)
  207. BOOST_GRAPH_EVENT_STUB(on_discover_vertex, bfs)
  208. BOOST_GRAPH_EVENT_STUB(on_examine_vertex, bfs)
  209. BOOST_GRAPH_EVENT_STUB(on_examine_edge, bfs)
  210. BOOST_GRAPH_EVENT_STUB(on_tree_edge, bfs)
  211. BOOST_GRAPH_EVENT_STUB(on_non_tree_edge, bfs)
  212. BOOST_GRAPH_EVENT_STUB(on_gray_target, bfs)
  213. BOOST_GRAPH_EVENT_STUB(on_black_target, bfs)
  214. BOOST_GRAPH_EVENT_STUB(on_finish_vertex, bfs)
  215. protected:
  216. Visitors m_vis;
  217. };
  218. template < class Visitors >
  219. bfs_visitor< Visitors > make_bfs_visitor(Visitors vis)
  220. {
  221. return bfs_visitor< Visitors >(vis);
  222. }
  223. typedef bfs_visitor<> default_bfs_visitor;
  224. namespace detail
  225. {
  226. template < class VertexListGraph, class ColorMap, class BFSVisitor, class P,
  227. class T, class R >
  228. void bfs_helper(VertexListGraph& g,
  229. typename graph_traits< VertexListGraph >::vertex_descriptor s,
  230. ColorMap color, BFSVisitor vis,
  231. const bgl_named_params< P, T, R >& params, boost::mpl::false_)
  232. {
  233. typedef graph_traits< VertexListGraph > Traits;
  234. // Buffer default
  235. typedef typename Traits::vertex_descriptor Vertex;
  236. typedef boost::queue< Vertex > queue_t;
  237. queue_t Q;
  238. breadth_first_search(g, s,
  239. choose_param(get_param(params, buffer_param_t()), boost::ref(Q))
  240. .get(),
  241. vis, color);
  242. }
  243. #ifdef BOOST_GRAPH_USE_MPI
  244. template < class DistributedGraph, class ColorMap, class BFSVisitor,
  245. class P, class T, class R >
  246. void bfs_helper(DistributedGraph& g,
  247. typename graph_traits< DistributedGraph >::vertex_descriptor s,
  248. ColorMap color, BFSVisitor vis,
  249. const bgl_named_params< P, T, R >& params, boost::mpl::true_);
  250. #endif // BOOST_GRAPH_USE_MPI
  251. //-------------------------------------------------------------------------
  252. // Choose between default color and color parameters. Using
  253. // function dispatching so that we don't require vertex index if
  254. // the color default is not being used.
  255. template < class ColorMap > struct bfs_dispatch
  256. {
  257. template < class VertexListGraph, class P, class T, class R >
  258. static void apply(VertexListGraph& g,
  259. typename graph_traits< VertexListGraph >::vertex_descriptor s,
  260. const bgl_named_params< P, T, R >& params, ColorMap color)
  261. {
  262. bfs_helper(g, s, color,
  263. choose_param(get_param(params, graph_visitor),
  264. make_bfs_visitor(null_visitor())),
  265. params,
  266. boost::mpl::bool_<
  267. boost::is_base_and_derived< distributed_graph_tag,
  268. typename graph_traits<
  269. VertexListGraph >::traversal_category >::value >());
  270. }
  271. };
  272. template <> struct bfs_dispatch< param_not_found >
  273. {
  274. template < class VertexListGraph, class P, class T, class R >
  275. static void apply(VertexListGraph& g,
  276. typename graph_traits< VertexListGraph >::vertex_descriptor s,
  277. const bgl_named_params< P, T, R >& params, param_not_found)
  278. {
  279. null_visitor null_vis;
  280. bfs_helper(g, s,
  281. make_two_bit_color_map(num_vertices(g),
  282. choose_const_pmap(
  283. get_param(params, vertex_index), g, vertex_index)),
  284. choose_param(get_param(params, graph_visitor),
  285. make_bfs_visitor(null_vis)),
  286. params,
  287. boost::mpl::bool_<
  288. boost::is_base_and_derived< distributed_graph_tag,
  289. typename graph_traits<
  290. VertexListGraph >::traversal_category >::value >());
  291. }
  292. };
  293. } // namespace detail
  294. #if 1
  295. // Named Parameter Variant
  296. template < class VertexListGraph, class P, class T, class R >
  297. void breadth_first_search(const VertexListGraph& g,
  298. typename graph_traits< VertexListGraph >::vertex_descriptor s,
  299. const bgl_named_params< P, T, R >& params)
  300. {
  301. // The graph is passed by *const* reference so that graph adaptors
  302. // (temporaries) can be passed into this function. However, the
  303. // graph is not really const since we may write to property maps
  304. // of the graph.
  305. VertexListGraph& ng = const_cast< VertexListGraph& >(g);
  306. typedef typename get_param_type< vertex_color_t,
  307. bgl_named_params< P, T, R > >::type C;
  308. detail::bfs_dispatch< C >::apply(
  309. ng, s, params, get_param(params, vertex_color));
  310. }
  311. #endif
  312. // This version does not initialize colors, user has to.
  313. template < class IncidenceGraph, class P, class T, class R >
  314. void breadth_first_visit(const IncidenceGraph& g,
  315. typename graph_traits< IncidenceGraph >::vertex_descriptor s,
  316. const bgl_named_params< P, T, R >& params)
  317. {
  318. // The graph is passed by *const* reference so that graph adaptors
  319. // (temporaries) can be passed into this function. However, the
  320. // graph is not really const since we may write to property maps
  321. // of the graph.
  322. IncidenceGraph& ng = const_cast< IncidenceGraph& >(g);
  323. typedef graph_traits< IncidenceGraph > Traits;
  324. // Buffer default
  325. typedef typename Traits::vertex_descriptor vertex_descriptor;
  326. typedef boost::queue< vertex_descriptor > queue_t;
  327. queue_t Q;
  328. breadth_first_visit(ng, s,
  329. choose_param(get_param(params, buffer_param_t()), boost::ref(Q)).get(),
  330. choose_param(
  331. get_param(params, graph_visitor), make_bfs_visitor(null_visitor())),
  332. choose_pmap(get_param(params, vertex_color), ng, vertex_color));
  333. }
  334. namespace graph
  335. {
  336. namespace detail
  337. {
  338. template < typename Graph, typename Source >
  339. struct breadth_first_search_impl
  340. {
  341. typedef void result_type;
  342. template < typename ArgPack >
  343. void operator()(
  344. const Graph& g, const Source& source, const ArgPack& arg_pack)
  345. {
  346. using namespace boost::graph::keywords;
  347. typename boost::graph_traits< Graph >::vertex_descriptor
  348. sources[1]
  349. = { source };
  350. boost::queue<
  351. typename boost::graph_traits< Graph >::vertex_descriptor >
  352. Q;
  353. boost::breadth_first_search(g, &sources[0], &sources[1],
  354. boost::unwrap_ref(arg_pack[_buffer | boost::ref(Q)]),
  355. arg_pack[_visitor | make_bfs_visitor(null_visitor())],
  356. boost::detail::make_color_map_from_arg_pack(g, arg_pack));
  357. }
  358. };
  359. }
  360. BOOST_GRAPH_MAKE_FORWARDING_FUNCTION(breadth_first_search, 2, 4)
  361. }
  362. #if 0
  363. // Named Parameter Variant
  364. BOOST_GRAPH_MAKE_OLD_STYLE_PARAMETER_FUNCTION(breadth_first_search, 2)
  365. #endif
  366. } // namespace boost
  367. #include BOOST_GRAPH_MPI_INCLUDE(< boost / graph / distributed / breadth_first_search.hpp >)
  368. #endif // BOOST_GRAPH_BREADTH_FIRST_SEARCH_HPP