breadth_first_search.hpp 14 KB

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