breadth_first_search.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <HTML>
  2. <!--
  3. Copyright (c) Jeremy Siek 2000, 2001
  4. Distributed under the Boost Software License, Version 1.0.
  5. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. -->
  8. <Head>
  9. <Title>Boost Graph Library: Breadth-First Search</Title>
  10. <BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b"
  11. ALINK="#ff0000">
  12. <IMG SRC="../../../boost.png"
  13. ALT="C++ Boost" width="277" height="86">
  14. <BR Clear>
  15. <H1><A NAME="sec:bfs">
  16. <img src="figs/python.gif" alt="(Python)"/>
  17. <TT>breadth_first_search</TT>
  18. </H1>
  19. <P>
  20. <PRE>
  21. <i>// named parameter version</i>
  22. template &lt;class Graph, class P, class T, class R&gt;
  23. void breadth_first_search(Graph& G,
  24. typename graph_traits&lt;Graph&gt;::vertex_descriptor s,
  25. const bgl_named_params&lt;P, T, R&gt;&amp; params);
  26. <i>// non-named parameter version</i>
  27. template &lt;class Graph, class Buffer, class BFSVisitor,
  28. class ColorMap&gt;
  29. void breadth_first_search(const Graph&amp; g,
  30. typename graph_traits&lt;Graph&gt;::vertex_descriptor s,
  31. Buffer&amp; Q, BFSVisitor vis, ColorMap color);
  32. </PRE>
  33. <p>
  34. The <tt>breadth_first_search()</tt> function performs a breadth-first
  35. traversal [<a href="./bibliography.html#moore59">49</a>] of a directed
  36. or undirected graph. A breadth-first traversal visits vertices that
  37. are closer to the source before visiting vertices that are further
  38. away. In this context ``distance'' is defined as the number of edges
  39. in the shortest path from the source vertex. The
  40. <tt>breadth_first_search()</tt> function can be used to compute the
  41. shortest path from the source to all reachable vertices and the
  42. resulting shortest-path distances. For more definitions related to BFS
  43. see section <a href="./graph_theory_review.html#sec:bfs-algorithm">
  44. Breadth-First Search</a>.
  45. </p>
  46. <p>
  47. BFS uses two data structures to to implement the traversal: a color
  48. marker for each vertex and a queue. White vertices are undiscovered
  49. while gray vertices are discovered but have undiscovered adjacent
  50. vertices. Black vertices are discovered and are adjacent to only other
  51. black or gray vertices. The algorithm proceeds by removing a vertex
  52. </i>u</i> from the queue and examining each out-edge <i>(u,v)</i>. If an
  53. adjacent vertex <i>v</i> is not already discovered, it is colored gray and
  54. placed in the queue. After all of the out-edges are examined, vertex
  55. <i>u</i> is colored black and the process is repeated. Pseudo-code for the
  56. BFS algorithm is a listed below.
  57. </p>
  58. <table>
  59. <tr>
  60. <td valign="top">
  61. <pre>
  62. BFS(<i>G</i>, <i>s</i>)
  63. <b>for</b> each vertex <i>u in V[G]</i>
  64. <i>color[u] :=</i> WHITE
  65. <i>d[u] := infinity</i>
  66. <i>p[u] := u</i>
  67. <b>end for</b>
  68. <i>color[s] :=</i> GRAY
  69. <i>d[s] := 0</i>
  70. ENQUEUE(<i>Q</i>, <i>s</i>)
  71. <b>while</b> (<i>Q != &Oslash;</i>)
  72. <i>u :=</i> DEQUEUE(Q)
  73. <b>for</b> each vertex <i>v in Adj[u]</i>
  74. <b>if</b> (<i>color[v] =</i> WHITE)
  75. <i>color[v] :=</i> GRAY
  76. <i>d[v] := d[u] + 1</i>
  77. <i>p[v] := u</i>
  78. ENQUEUE(<i>Q</i>, <i>v</i>)
  79. <b>else</b>
  80. <b>if</b> (<i>color[v] =</i> GRAY)
  81. ...
  82. <b>else</b>
  83. ...
  84. <b>end for</b>
  85. <i>color[u] :=</i> BLACK
  86. <b>end while</b>
  87. return (<i>d</i>, <i>p</i>)
  88. </pre>
  89. </td>
  90. <td valign="top">
  91. <pre>
  92. initialize vertex <i>u</i>
  93. discover vertex <i>s</i>
  94. examine vertex <i>u</i>
  95. examine edge <i>(u,v)</i>
  96. <i>(u,v)</i> is a tree edge
  97. discover vertex <i>v</i>
  98. <i>(u,v)</i> is a non-tree edge
  99. <i>(u,v)</i> has a gray target
  100. <i>(u,v)</i> has a black target
  101. finish vertex <i>u</i>
  102. </pre>
  103. </tr>
  104. </table>
  105. The <tt>breadth_first_search()</tt> function can be extended with
  106. user-defined actions that will be called a certain event points. The
  107. actions must be provided in the form of a visitor object, that is, an
  108. object who's type meets the requirements for a <a
  109. href="./BFSVisitor.html">BFS Visitor</a>. In the above pseudo-code,
  110. the event points are the labels on the right. Also a description of
  111. each event point is given below. By default, the
  112. <tt>breadth_first_search()</tt> function does not carry out any
  113. actions, not even recording distances or predecessors. However these
  114. can be easily added using the <a
  115. href="./distance_recorder.html"><tt>distance_recorder</tt></a> and <a
  116. href="./predecessor_recorder.html"><tt>predecessor_recorder</tt></a>
  117. event visitors.
  118. <H3>Where Defined</H3>
  119. <P>
  120. <a href="../../../boost/graph/breadth_first_search.hpp"><TT>boost/graph/breadth_first_search.hpp</TT></a>
  121. <P>
  122. <h3>Parameters</h3>
  123. IN: <tt>Graph&amp; g</tt>
  124. <blockquote>
  125. A directed or undirected graph. The graph type must
  126. be a model of <a href="./VertexListGraph.html">Vertex List Graph</a>
  127. and <a href="./IncidenceGraph.html">Incidence Graph</a>.<br>
  128. <b>Python</b>: The parameter is named <tt>graph</tt>.
  129. </blockquote>
  130. IN: <tt>vertex_descriptor s</tt>
  131. <blockquote>
  132. The source vertex where the search is started.<br>
  133. <b>Python</b>: The parameter is named <tt>root_vertex</tt>.
  134. </blockquote>
  135. <h3>Named Parameters</h3>
  136. IN: <tt>visitor(BFSVisitor vis)</tt>
  137. <blockquote>
  138. A visitor object that is invoked inside the algorithm at the
  139. event-points specified by the <a href="BFSVisitor.html">BFS
  140. Visitor</a> concept. The visitor object is passed by value <a
  141. href="#1">[1]</a>.<br> <b>Default:</b>
  142. <tt>bfs_visitor&lt;null_visitor&gt;</tt> <br>
  143. <b>Python</b>: The parameter should be an object that derives from
  144. the <a href="BFSVisitor.html#python"><tt>BFSVisitor</tt></a> type of the graph.
  145. </blockquote>
  146. UTIL/OUT: <tt>color_map(ColorMap color)</tt>
  147. <blockquote>
  148. This is used by the algorithm to keep track of its progress through
  149. the graph. The user need not initialize the color map before calling
  150. <tt>breadth_first_search()</tt> since the algorithm initializes the
  151. color of every vertex to white at the start of the algorihtm. If you
  152. need to perform multiple breadth-first searches on a graph (for
  153. example, if there are some disconnected components) then use the <a
  154. href="./breadth_first_visit.html"><tt>breadth_first_visit()</tt></a>
  155. function and do your own color initialization.
  156. <p>The type <tt>ColorMap</tt> must be a model of <a
  157. href="../../property_map/doc/ReadWritePropertyMap.html">Read/Write
  158. Property Map</a> and its key type must be the graph's vertex
  159. descriptor type and the value type of the color map must model
  160. <a href="./ColorValue.html">ColorValue</a>.<br>
  161. <b>Default:</b> an <a
  162. href="../../property_map/doc/iterator_property_map.html">
  163. </tt>iterator_property_map</tt></a> created from a
  164. <tt>std::vector</tt> of <tt>default_color_type</tt> of size
  165. <tt>num_vertices(g)</tt> and using the <tt>i_map</tt> for the index
  166. map.<br>
  167. <b>Python</b>: The color map must be a <tt>vertex_color_map</tt> for
  168. the graph.
  169. </blockquote>
  170. IN: <tt>vertex_index_map(VertexIndexMap i_map)</tt>
  171. <blockquote>
  172. This maps each vertex to an integer in the range <tt>[0,
  173. num_vertices(g))</tt>. This parameter is only necessary when the
  174. default color property map is used. The type <tt>VertexIndexMap</tt>
  175. must be a model of <a
  176. href="../../property_map/doc/ReadablePropertyMap.html">Readable Property
  177. Map</a>. The value type of the map must be an integer type. The
  178. vertex descriptor type of the graph needs to be usable as the key
  179. type of the map.<br>
  180. <b>Default:</b> <tt>get(vertex_index, g)</tt>.
  181. Note: if you use this default, make sure your graph has
  182. an internal <tt>vertex_index</tt> property. For example,
  183. <tt>adjacency_list</tt> with <tt>VertexList=listS</tt> does
  184. not have an internal <tt>vertex_index</tt> property.<br>
  185. <b>Python</b>: Unsupported parameter.
  186. </blockquote>
  187. UTIL: <tt>buffer(Buffer&amp; Q)</tt>
  188. <blockquote>
  189. The queue used to determine the order in which vertices will be
  190. discovered. If a FIFO queue is used, then the traversal will
  191. be according to the usual BFS ordering. Other types of queues
  192. can be used, but the traversal order will be different.
  193. For example Dijkstra's algorithm can be implemented
  194. using a priority queue. The type <tt>Buffer</tt> must be a model of
  195. <a href="./Buffer.html">Buffer</a>.<br> The <tt>value_type</tt>
  196. of the buffer must be the <tt>vertex_descriptor</tt> type for the graph.<br>
  197. <b>Default:</b> <tt>boost::queue</tt><br>
  198. <b>Python</b>: The buffer must derive from the <a
  199. href="./Buffer.html">Buffer</a> type for the graph.
  200. </blockquote>
  201. <H3><A NAME="SECTION001330300000000000000">
  202. Complexity</A>
  203. </H3>
  204. <P>
  205. The time complexity is <i>O(E + V)</i>.
  206. <P>
  207. <h3>Visitor Event Points</h3>
  208. <ul>
  209. <li><b><tt>vis.initialize_vertex(v, g)</tt></b> is invoked on every vertex
  210. before the start of the search.
  211. <li><b><tt>vis.examine_vertex(u, g)</tt></b>r is invoked in each
  212. vertex as it is removed from the queue.
  213. <li><b><tt>vis.examine_edge(e, g)</tt></b> is invoked on every out-edge
  214. of each vertex immediately after the vertex is removed from the queue.
  215. <li><b><tt>vis.tree_edge(e, g)</tt></b> is invoked (in addition to
  216. <tt>examine_edge()</tt>) if the edge is a tree edge. The
  217. target vertex of edge <tt>e</tt> is discovered at this time.
  218. <li><b><tt>vis.discover_vertex(u, g)</tt></b> is invoked the first time the
  219. algorithm encounters vertex <i>u</i>. All vertices closer to the
  220. source vertex have been discovered, and vertices further from the
  221. source have not yet been discovered.
  222. <li><b><tt>vis.non_tree_edge(e, g)</tt></b> is invoked (in addition to
  223. <tt>examine_edge()</tt>) if the edge is not a tree edge.
  224. <li><b><tt>vis.gray_target(e, g)</tt></b> is invoked (in addition to
  225. <tt>non_tree_edge()</tt>) if the target vertex is colored gray at the
  226. time of examination. The color gray indicates that
  227. the vertex is currently in the queue.
  228. <li><b><tt>vis.black_target(e, g)</tt></b> is invoked (in addition to
  229. <tt>non_tree_edge()</tt>) if the target vertex is colored black at the
  230. time of examination. The color black indicates that the
  231. vertex is no longer in the queue.
  232. <li><b><tt>vis.finish_vertex(u, g)</tt></b> is invoked after all of the out
  233. edges of <i>u</i> have been examined and all of the adjacent vertices
  234. have been discovered.
  235. </ul>
  236. <H3><A NAME="SECTION001330400000000000000">
  237. Example</A>
  238. </H3>
  239. <P>
  240. The example in <a
  241. href="../example/bfs-example.cpp"><TT>example/bfs-example.cpp</TT></a>
  242. demonstrates using the BGL Breadth-first search algorithm on the graph
  243. from <A HREF="./graph_theory_review.html#fig:bfs-example">Figure
  244. 6</A>. The file
  245. <a href="../example/bfs-example2.cpp"><TT>example/bfs-example2.cpp</TT></a>
  246. contains the same example, except that the <tt>adacency_list</tt>
  247. class used has <tt>VertexList</tt> and <tt>EdgeList</tt> set
  248. to <tt>listS</tt>.
  249. </P>
  250. <h3>See Also</h3>
  251. <a href="./bfs_visitor.html"><tt>bfs_visitor</tt></a> and
  252. <a href="./depth_first_search.html"><tt>depth_first_search()</tt></a>
  253. <h3>Notes</h3>
  254. <p><a name="1">[1]</a>
  255. Since the visitor parameter is passed by value, if your visitor
  256. contains state then any changes to the state during the algorithm
  257. will be made to a copy of the visitor object, not the visitor object
  258. passed in. Therefore you may want the visitor to hold this state by
  259. pointer or reference.
  260. <br>
  261. <HR>
  262. <TABLE>
  263. <TR valign=top>
  264. <TD nowrap>Copyright &copy; 2000-2001</TD><TD>
  265. <A HREF="http://www.boost.org/people/jeremy_siek.htm">Jeremy Siek</A>, Indiana University (<A HREF="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</A>)
  266. </TD></TR></TABLE>
  267. </BODY>
  268. </HTML>