depth_first_search.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <HTML>
  2. <!--
  3. Copyright (c) Jeremy Siek, Lie-Quan Lee, and Andrew Lumsdaine 2000
  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: Depth-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:depth-first-search"></A><img src="figs/python.gif" alt="(Python)"/>
  16. <TT>depth_first_search</TT>
  17. </H1>
  18. <P>
  19. <PRE>
  20. <i>// named parameter version</i>
  21. template &lt;class Graph, class class P, class T, class R&gt;
  22. void depth_first_search(Graph&amp; G,
  23. const bgl_named_params&lt;P, T, R&gt;&amp; params);
  24. <i>// non-named parameter version</i>
  25. template &lt;class Graph, class <a href="DFSVisitor.html">DFSVisitor</a>, class ColorMap&gt;
  26. void depth_first_search(const Graph&amp; g, DFSVisitor vis, ColorMap color)
  27. template &lt;class Graph, class <a href="DFSVisitor.html">DFSVisitor</a>, class ColorMap&gt;
  28. void depth_first_search(const Graph&amp; g, DFSVisitor vis, ColorMap color,
  29. typename graph_traits&lt;Graph&gt;::vertex_descriptor start)
  30. </PRE>
  31. <p>
  32. The <tt>depth_first_search()</tt> function performs a depth-first
  33. traversal of the vertices in a directed graph. When
  34. possible, a depth-first traversal chooses a vertex adjacent to the
  35. current vertex to visit next. If all adjacent vertices have already
  36. been discovered, or there are no adjacent vertices, then the algorithm
  37. backtracks to the last vertex that had undiscovered neighbors. Once
  38. all reachable vertices have been visited, the algorithm selects from
  39. any remaining undiscovered vertices and continues the traversal. The
  40. algorithm finishes when all vertices have been visited. Depth-first
  41. search is useful for categorizing edges in a graph, and for imposing
  42. an ordering on the vertices. Section <a
  43. href="./graph_theory_review.html#sec:dfs-algorithm">Depth-First
  44. Search</a> describes the various properties of DFS and walks through
  45. an example.
  46. </p>
  47. <p>
  48. Similar to BFS, color markers are used to keep track of which vertices
  49. have been discovered. White marks vertices that have yet to be
  50. discovered, gray marks a vertex that is discovered but still has
  51. vertices adjacent to it that are undiscovered. A black vertex is
  52. discovered vertex that is not adjacent to any white vertices.
  53. <p>
  54. <p>
  55. The <tt>depth_first_search()</tt> function invokes user-defined
  56. actions at certain event-points within the algorithm. This provides a
  57. mechanism for adapting the generic DFS algorithm to the many
  58. situations in which it can be used. In the pseudo-code below, the
  59. event points for DFS are the labels on
  60. the right. The user-defined actions must be provided in the form of a
  61. visitor object, that is, an object whose type meets the requirements
  62. for a <a href="./DFSVisitor.html">DFS Visitor</a>. In the pseudo-code
  63. we show the algorithm computing predecessors <i>p</i>, discover time
  64. <i>d</i> and finish time <i>t</i>. By default, the
  65. <tt>depth_first_search()</tt> function does not compute these
  66. properties, however there are pre-defined visitors such as <a
  67. href="./predecessor_recorder.html"><tt>predecessor_recorder</tt></a>
  68. and <a href="./time_stamper.html"><tt>time_stamper</tt></a> that can
  69. be used to do this.
  70. </p>
  71. <table>
  72. <tr>
  73. <td valign="top">
  74. <pre>
  75. DFS(<i>G</i>)
  76. <b>for</b> each vertex <i>u in V</i>
  77. <i>color[u] :=</i> WHITE
  78. <i>p[u] = u</i>
  79. <b>end for</b>
  80. <i>time := 0</i>
  81. <b>if</b> there is a starting vertex <i>s</i>
  82. <b>call</b> DFS-VISIT(<i>G</i>, <i>s</i>)
  83. <b>for</b> each vertex <i>u in V</i>
  84. <b>if</b> <i>color[u] =</i> WHITE
  85. <b>call</b> DFS-VISIT(<i>G</i>, <i>u</i>)
  86. <b>end for</b>
  87. return (<i>p</i>,<i>d_time</i>,<i>f_time</i>) <br>
  88. DFS-VISIT(<i>G</i>, <i>u</i>)
  89. <i>color[u] :=</i> GRAY
  90. <i>d_time[u] := time := time + 1</i>
  91. <b>for</b> each <i>v in Adj[u]</i>
  92. <b>if</b> (<i>color[v] =</i> WHITE)
  93. <i>p[v] = u</i>
  94. <b>call</b> DFS-VISIT(<i>G</i>, <i>v</i>)
  95. <b>else if</b> (<i>color[v] =</i> GRAY)
  96. <i>...</i>
  97. <b>else if</b> (<i>color[v] =</i> BLACK)
  98. <i>...</i>
  99. <i>...</i>
  100. <b>end for</b>
  101. <i>color[u] :=</i> BLACK
  102. <i>f_time[u] := time := time + 1</i>
  103. <pre>
  104. </td>
  105. <td valign="top">
  106. <pre>
  107. -
  108. -
  109. initialize vertex <i>u</i>
  110. -
  111. -
  112. -
  113. -
  114. start vertex <i>s</i>
  115. -
  116. -
  117. start vertex <i>u</i>
  118. -
  119. -
  120. -
  121. -
  122. discover vertex <i>u</i>
  123. -
  124. examine edge <i>(u,v)</i>
  125. -
  126. <i>(u,v)</i> is a tree edge
  127. -
  128. -
  129. <i>(u,v)</i> is a back edge
  130. -
  131. <i>(u,v)</i> is a cross or forward edge
  132. -
  133. finish edge <i>(u,v)</i>
  134. -
  135. finish vertex <i>u</i>
  136. -
  137. </pre>
  138. </td>
  139. </tr>
  140. </table>
  141. <H3>Where Defined</H3>
  142. <P>
  143. <a href="../../../boost/graph/depth_first_search.hpp"><TT>boost/graph/depth_first_search.hpp</TT></a>
  144. <h3>Parameters</h3>
  145. IN: <tt>Graph&amp; g</tt>
  146. <blockquote>
  147. A directed graph. The graph type must
  148. be a model of <a href="./IncidenceGraph.html">Incidence Graph</a>
  149. and <a href="./VertexListGraph.html">Vertex List Graph</a>.<br>
  150. <b>Python</b>: The parameter is named <tt>graph</tt>.
  151. </blockquote>
  152. <h3>Named Parameters</h3>
  153. IN: <tt>visitor(DFSVisitor vis)</tt>
  154. <blockquote>
  155. A visitor object that is invoked inside the algorithm at the
  156. event-points specified by the <a href="./DFSVisitor.html">DFS
  157. Visitor</a> concept. The visitor object is passed by value <a
  158. href="#1">[1]</a>. <br> <b>Default:</b>
  159. <tt>dfs_visitor&lt;null_visitor&gt;</tt><br>
  160. <b>Python</b>: The parameter should be an object that derives from
  161. the <a href="DFSVisitor.html#python"><tt>DFSVisitor</tt></a> type of
  162. the graph.
  163. </blockquote>
  164. UTIL/OUT: <tt>color_map(ColorMap color)</tt>
  165. <blockquote>
  166. This is used by the algorithm to keep track of its progress through
  167. the graph. The type <tt>ColorMap</tt> must be a model of <a
  168. href="../../property_map/doc/ReadWritePropertyMap.html">Read/Write
  169. Property Map</a> and its key type must be the graph's vertex
  170. descriptor type and the value type of the color map must model
  171. <a href="./ColorValue.html">ColorValue</a>.<br>
  172. <b>Default:</b> an <a
  173. href="../../property_map/doc/iterator_property_map.html">
  174. </tt>iterator_property_map</tt></a> created from a
  175. <tt>std::vector</tt> of <tt>default_color_type</tt> of size
  176. <tt>num_vertices(g)</tt> and using the <tt>i_map</tt> for the index
  177. map.<br>
  178. <b>Python</b>: The color map must be a <tt>vertex_color_map</tt> for
  179. the graph.
  180. </blockquote>
  181. IN: <tt>root_vertex(typename
  182. graph_traits&lt;VertexListGraph&gt;::vertex_descriptor start)</tt>
  183. <blockquote>
  184. This specifies the vertex that the depth-first search should
  185. originate from. The type is the type of a vertex descriptor for the
  186. given graph.<br>
  187. <b>Default:</b> <tt>*vertices(g).first</tt><br>
  188. </blockquote>
  189. IN: <tt>vertex_index_map(VertexIndexMap i_map)</tt>
  190. <blockquote>
  191. This maps each vertex to an integer in the range <tt>[0,
  192. num_vertices(g))</tt>. This parameter is only necessary when the
  193. default color property map is used. The type <tt>VertexIndexMap</tt>
  194. must be a model of <a
  195. href="../../property_map/doc/ReadablePropertyMap.html">Readable Property
  196. Map</a>. The value type of the map must be an integer type. The
  197. vertex descriptor type of the graph needs to be usable as the key
  198. type of the map.<br>
  199. <b>Default:</b> <tt>get(vertex_index, g)</tt>.
  200. Note: if you use this default, make sure your graph has
  201. an internal <tt>vertex_index</tt> property. For example,
  202. <tt>adjacency_list</tt> with <tt>VertexList=listS</tt> does
  203. not have an internal <tt>vertex_index</tt> property.<br>
  204. <b>Python</b>: Unsupported parameter.
  205. </blockquote>
  206. <P>
  207. <H3><A NAME="SECTION001340300000000000000">
  208. Complexity</A>
  209. </H3>
  210. <P>
  211. The time complexity is <i>O(E + V)</i>.
  212. <P>
  213. <h3>Visitor Event Points</h3>
  214. <ul>
  215. <li><b><tt>vis.initialize_vertex(s, g)</tt></b> is invoked on every
  216. vertex of the graph before the start of the graph search.
  217. <li><b><tt>vis.start_vertex(s, g)</tt></b> is invoked on the source
  218. vertex once before the start of the search.
  219. <li><b><tt>vis.discover_vertex(u, g)</tt></b> is invoked when a vertex
  220. is encountered for the first time.
  221. <li><b><tt>vis.examine_edge(e, g)</tt></b> is invoked on every out-edge
  222. of each vertex after it is discovered.
  223. <li><b><tt>vis.tree_edge(e, g)</tt></b> is invoked on each edge as it
  224. becomes a member of the edges that form the search tree. If you
  225. wish to record predecessors, do so at this event point.
  226. <li><b><tt>vis.back_edge(e, g)</tt></b> is invoked on the back edges in
  227. the graph.
  228. <li><b><tt>vis.forward_or_cross_edge(e, g)</tt></b> is invoked on
  229. forward or cross edges in the graph. In an undirected graph this
  230. method is never called.
  231. <li><b><tt>vis.finish_edge(e, g)</tt></b> is invoked on the non-tree edges in
  232. the graph as well as on each tree edge after its target vertex is finished.
  233. <li><b><tt>vis.finish_vertex(u, g)</tt></b> is invoked on a vertex after
  234. all of its out edges have been added to the search tree and all of
  235. the adjacent vertices have been discovered (but before their
  236. out-edges have been examined).
  237. </ul>
  238. <H3>Example</H3>
  239. <P>
  240. The example in <a href="../example/dfs-example.cpp">
  241. <TT>examples/dfs-example.cpp</TT></a> shows DFS applied to the graph in
  242. <A HREF="./graph_theory_review.html#fig:dfs-example">Figure 1</A>.
  243. <h3>See Also</h3>
  244. <a href="./depth_first_visit.html"><tt>depth_first_visit</tt></a>
  245. <a href="./undirected_dfs.html"><tt>undirected_dfs</tt></a>
  246. <h3>Notes</h3>
  247. <p><a name="1">[1]</a>
  248. Since the visitor parameter is passed by value, if your visitor
  249. contains state then any changes to the state during the algorithm
  250. will be made to a copy of the visitor object, not the visitor object
  251. passed in. Therefore you may want the visitor to hold this state by
  252. pointer or reference.
  253. <br>
  254. <HR>
  255. <TABLE>
  256. <TR valign=top>
  257. <TD nowrap>Copyright &copy; 2000-2001</TD><TD>
  258. <A HREF="http://www.boost.org/people/jeremy_siek.htm">Jeremy Siek</A>,
  259. Indiana University (<A
  260. HREF="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</A>)<br>
  261. <A HREF="http://www.boost.org/people/liequan_lee.htm">Lie-Quan Lee</A>, Indiana University (<A HREF="mailto:llee@cs.indiana.edu">llee@cs.indiana.edu</A>)<br>
  262. <A HREF="https://homes.cs.washington.edu/~al75">Andrew Lumsdaine</A>,
  263. Indiana University (<A
  264. HREF="mailto:lums@osl.iu.edu">lums@osl.iu.edu</A>)
  265. </TD></TR></TABLE>
  266. </BODY>
  267. </HTML>