breadth_first_search.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. .. Copyright (C) 2004-2008 The Trustees of Indiana University.
  2. Use, modification and distribution is subject to the Boost Software
  3. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. http://www.boost.org/LICENSE_1_0.txt)
  5. ===========================
  6. |Logo| Breadth-First Search
  7. ===========================
  8. ::
  9. // named parameter version
  10. template <class Graph, class P, class T, class R>
  11. void breadth_first_search(Graph& G,
  12. typename graph_traits<Graph>::vertex_descriptor s,
  13. const bgl_named_params<P, T, R>& params);
  14. // non-named parameter version
  15. template <class Graph, class Buffer, class BFSVisitor,
  16. class ColorMap>
  17. void breadth_first_search(const Graph& g,
  18. typename graph_traits<Graph>::vertex_descriptor s,
  19. Buffer& Q, BFSVisitor vis, ColorMap color);
  20. The ``breadth_first_search()`` function performs a distributed breadth-first
  21. traversal of a directed or undirected graph. The distributed BFS is
  22. syntactically equivalent to its `sequential counterpart`_, which
  23. provides additional background and discussion. Differences in
  24. semantics are highlighted here, and we refer the reader to the
  25. documentation of the `sequential breadth-first search`_ for the
  26. remainder of the details.
  27. This distributed breadth-first search algorithm implements a
  28. *level-synchronized* breadth-first search, meaning that all vertices
  29. in a given level of the BFS tree will be processed (potentially in
  30. parallel) before any vertices from a successive level in the tree are
  31. processed. Distributed breadth-first search visitors should account
  32. for this behavior, a topic discussed further in `Visitor Event
  33. Points`_.
  34. .. contents::
  35. Where Defined
  36. -------------
  37. <``boost/graph/breadth_first_search.hpp``>
  38. Parameter Defaults
  39. ------------------
  40. All parameters of the `sequential breadth-first search`_ are supported
  41. and have essentially the same meaning. Only differences are documented
  42. here.
  43. IN: ``Graph& g``
  44. The graph type must be a model of `Distributed Graph`_.
  45. IN: ``vertex_descriptor s``
  46. The start vertex must be the same in every process.
  47. IN: ``visitor(BFSVisitor vis)``
  48. The visitor must be a distributed BFS visitor. The suble differences
  49. between sequential and distributed BFS visitors are discussed in the
  50. section `Visitor Event Points`_.
  51. UTIL/OUT: ``color_map(ColorMap color)``
  52. The color map must be a `Distributed Property Map`_ with the same
  53. process group as the graph ``g`` whose colors must monotonically
  54. darken (white -> gray -> black). The default value is a distributed
  55. ``iterator_property_map`` created from a ``std::vector`` of
  56. ``default_color_type``.
  57. UTIL: ``buffer(Buffer& Q)``
  58. The queue must be a distributed queue that passes vertices to their
  59. owning process. If already-visited vertices should not be visited
  60. again (as is typical for BFS), the queue must filter duplicates
  61. itself. The queue controls synchronization within the algorithm: its
  62. ``empty()`` method must not return ``true`` until all local queues
  63. are empty.
  64. **Default:** A ``distributed_queue`` of a ``filtered_queue`` over a
  65. standard ``boost::queue``. This queue filters out duplicate
  66. vertices and distributes vertices appropriately.
  67. Complexity
  68. ----------
  69. This algorithm performs *O(V + E)* work in *d + 1* BSP supersteps,
  70. where *d* is the diameter of the connected component being
  71. searched. Over all supersteps, *O(E)* messages of constant size will
  72. be transmitted.
  73. Visitor Event Points
  74. --------------------
  75. The `BFS Visitor`_ concept defines 9 event points that will be
  76. triggered by the `sequential breadth-first search`_. The distributed
  77. BFS retains these nine event points, but the sequence of events
  78. triggered and the process in which each event occurs will change
  79. depending on the distribution of the graph.
  80. ``initialize_vertex(s, g)``
  81. This will be invoked by every process for each local vertex.
  82. ``discover_vertex(u, g)``
  83. This will be invoked each time a process discovers a new vertex
  84. ``u``. Due to incomplete information in distributed property maps,
  85. this event may be triggered many times for the same vertex ``u``.
  86. ``examine_vertex(u, g)``
  87. This will be invoked by the process owning the vertex ``u``. If the
  88. distributed queue prevents duplicates, it will be invoked only
  89. once for a particular vertex ``u``.
  90. ``examine_edge(e, g)``
  91. This will be invoked by the process owning the source vertex of
  92. ``e``. If the distributed queue prevents duplicates, it will be
  93. invoked only once for a particular edge ``e``.
  94. ``tree_edge(e, g)``
  95. Similar to ``examine_edge``, this will be invoked by the process
  96. owning the source vertex and may be invoked only once. Unlike the
  97. sequential BFS, this event may be triggered even when the target has
  98. already been discovered (but by a different process). Thus, some
  99. ``non_tree_edge`` events in a sequential BFS may become
  100. ``tree_edge`` in a distributed BFS.
  101. ``non_tree_edge(e, g)``
  102. Some ``non_tree_edge`` events in a sequential BFS may become
  103. ``tree_edge`` events in a distributed BFS. See the description of
  104. ``tree_edge`` for additional details.
  105. ``gray_target(e, g)``
  106. As with ``tree_edge`` not knowing when another process has already
  107. discovered a vertex, ``gray_target`` events may occur in a
  108. distributed BFS when ``black_target`` events may occur in a
  109. sequential BFS, due to a lack of information on a given
  110. processor. The source of edge ``e`` will be local to the process
  111. executing this event.
  112. ``black_target(e, g)``
  113. See documentation for ``gray_target``
  114. ``finish_vertex(e, g)``
  115. See documentation for ``examine_vertex``.
  116. Making Visitors Safe for Distributed BFS
  117. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  118. The three most important things to remember when updating an existing
  119. BFS visitor for distributed BFS or writing a new distributed BFS
  120. visitor are:
  121. 1. Be sure that all state is either entirely local or in a
  122. distributed data structure (most likely a property map!) using
  123. the same process group as the graph.
  124. 2. Be sure that the visitor doesn't require precise event sequences
  125. that cannot be guaranteed by distributed BFS, e.g., requiring
  126. ``tree_edge`` and ``non_tree_edge`` events to be completely
  127. distinct.
  128. 3. Be sure that the visitor can operate on incomplete
  129. information. This often includes using an appropriate reduction
  130. operation in a `distributed property map`_ and verifying that
  131. results written are "better" that what was previously written.
  132. Distributed BFS Visitor Example
  133. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  134. To illustrate the differences between sequential and distributed BFS
  135. visitors, we consider a BFS visitor that places the distance from the
  136. source vertex to every other vertex in a property map. The sequential
  137. visitor is very simple::
  138. template<typename DistanceMap>
  139. struct bfs_discovery_visitor : bfs_visitor<>
  140. {
  141. bfs_discovery_visitor(DistanceMap distance) : distance(distance) {}
  142. template<typename Edge, typename Graph>
  143. void tree_edge(Edge e, const Graph& g)
  144. {
  145. std::size_t new_distance = get(distance, source(e, g)) + 1;
  146. put(distance, target(e, g), new_distance);
  147. }
  148. private:
  149. DistanceMap distance;
  150. };
  151. To revisit this code for distributed BFS, we consider the three points
  152. in the section `Making Visitors Safe for Distributed BFS`_:
  153. 1. The distance map will need to become distributed, because the
  154. distance to each vertex should be stored in the process owning the
  155. vertex. This is actually a requirement on the user to provide such
  156. a distributed property map, although in many cases the property map
  157. will automatically be distributed and no syntactic changes will be
  158. required.
  159. 2. This visitor *does* require a precise sequence of events that may
  160. change with a distributed BFS. The extraneous ``tree_edge`` events
  161. that may occur could result in attempts to put distances into the
  162. distance map multiple times for a single vertex. We therefore need
  163. to consider bullet #3.
  164. 3. Since multiple distance values may be written for each vertex, we
  165. must always choose the best value we can find to update the
  166. distance map. The distributed property map ``distance`` needs to
  167. resolve distances to the smallest distance it has seen. For
  168. instance, process 0 may find vertex ``u`` at level 3 but process 1
  169. finds it at level 5: the distance must remain at 3. To do this, we
  170. state that the property map's *role* is as a distance map, which
  171. introduces an appropriate reduction operation::
  172. set_property_map_role(vertex_distance, distance);
  173. The resulting distributed BFS visitor (which also applies, with no
  174. changes, in the sequential BFS) is very similar to our original
  175. sequential BFS visitor. Note the single-line difference in the
  176. constructor::
  177. template<typename DistanceMap>
  178. struct bfs_discovery_visitor : bfs_visitor<>
  179. {
  180. bfs_discovery_visitor(DistanceMap distance) : distance(distance)
  181. {
  182. set_property_map_role(vertex_distance, distance);
  183. }
  184. template<typename Edge, typename Graph>
  185. void tree_edge(Edge e, const Graph& g)
  186. {
  187. std::size_t new_distance = get(distance, source(e, g)) + 1;
  188. put(distance, target(e, g), new_distance);
  189. }
  190. private:
  191. DistanceMap distance;
  192. };
  193. Performance
  194. -----------
  195. The performance of Breadth-First Search is illustrated by the
  196. following charts. Scaling and performance is reasonable for all of the
  197. graphs we have tried.
  198. .. image:: http://www.osl.iu.edu/research/pbgl/performance/chart.php?generator=ER,SF,SW&dataset=TimeSparse&columns=4
  199. :align: left
  200. .. image:: http://www.osl.iu.edu/research/pbgl/performance/chart.php?generator=ER,SF,SW&dataset=TimeSparse&columns=4&speedup=1
  201. .. image:: http://www.osl.iu.edu/research/pbgl/performance/chart.php?generator=ER,SF,SW&dataset=TimeDense&columns=4
  202. :align: left
  203. .. image:: http://www.osl.iu.edu/research/pbgl/performance/chart.php?generator=ER,SF,SW&dataset=TimeDense&columns=4&speedup=1
  204. -----------------------------------------------------------------------------
  205. Copyright (C) 2004 The Trustees of Indiana University.
  206. Authors: Douglas Gregor and Andrew Lumsdaine
  207. .. |Logo| image:: pbgl-logo.png
  208. :align: middle
  209. :alt: Parallel BGL
  210. :target: http://www.osl.iu.edu/research/pbgl
  211. .. _sequential counterpart: http://www.boost.org/libs/graph/doc/breadth_first_search.html
  212. .. _sequential breadth-first search: http://www.boost.org/libs/graph/doc/breadth_first_search.html
  213. .. _Distributed Graph: DistributedGraph.html
  214. .. _Distributed Property Map: distributed_property_map.html
  215. .. _BFS Visitor: http://www.boost.org/libs/graph/doc/BFSVisitor.html