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