undirected_dfs.html 11 KB

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