breadth_first_search.html 17 KB

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