1 | <HTML> |
---|
2 | <!-- |
---|
3 | -- Copyright (c) Jeremy Siek, Lie-Quan Lee, and Andrew Lumsdaine 2000 |
---|
4 | -- |
---|
5 | -- Permission to use, copy, modify, distribute and sell this software |
---|
6 | -- and its documentation for any purpose is hereby granted without fee, |
---|
7 | -- provided that the above copyright notice appears in all copies and |
---|
8 | -- that both that copyright notice and this permission notice appear |
---|
9 | -- in supporting documentation. We make no |
---|
10 | -- representations about the suitability of this software for any |
---|
11 | -- purpose. It is provided "as is" without express or implied warranty. |
---|
12 | --> |
---|
13 | <Head> |
---|
14 | <Title>DFS Visitor</Title> |
---|
15 | <BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b" |
---|
16 | ALINK="#ff0000"> |
---|
17 | <IMG SRC="../../../boost.png" |
---|
18 | ALT="C++ Boost" width="277" height="86"> |
---|
19 | |
---|
20 | <BR Clear> |
---|
21 | |
---|
22 | <H1><img src="figs/python.gif" alt="(Python)"/>DFS Visitor Concept</H1> |
---|
23 | |
---|
24 | This concept defines the visitor interface for <a |
---|
25 | href="./depth_first_search.html"><tt>depth_first_search()</tt></a>. |
---|
26 | Users can define a class with the DFS Visitor interface and pass an |
---|
27 | object of the class to <tt>depth_first_search()</tt>, thereby |
---|
28 | augmenting the actions taken during the graph search. |
---|
29 | |
---|
30 | <h3>Refinement of</h3> |
---|
31 | |
---|
32 | <a href="../../utility/CopyConstructible.html">Copy Constructible</a> |
---|
33 | (copying a visitor should be a lightweight operation). |
---|
34 | |
---|
35 | <h3>Notation</h3> |
---|
36 | |
---|
37 | <Table> |
---|
38 | <TR> |
---|
39 | <TD><tt>V</tt></TD> |
---|
40 | <TD>A type that is a model of DFS Visitor.</TD> |
---|
41 | </TR> |
---|
42 | |
---|
43 | <TR> |
---|
44 | <TD><tt>vis</tt></TD> |
---|
45 | <TD>An object of type <tt>V</tt>.</TD> |
---|
46 | </TR> |
---|
47 | |
---|
48 | <TR> |
---|
49 | <TD><tt>G</tt></TD> |
---|
50 | <TD>A type that is a model of Graph.</TD> |
---|
51 | </TR> |
---|
52 | |
---|
53 | <TR> |
---|
54 | <TD><tt>g</tt></TD> |
---|
55 | <TD>An object of type <tt>G</tt>.</TD> |
---|
56 | </TR> |
---|
57 | |
---|
58 | <TR> |
---|
59 | <TD><tt>e</tt></TD> |
---|
60 | <TD>An object of type <tt>boost::graph_traits<G>::edge_descriptor</tt>.</TD> |
---|
61 | </TR> |
---|
62 | |
---|
63 | <TR> |
---|
64 | <TD><tt>s,u</tt></TD> |
---|
65 | <TD>An object of type <tt>boost::graph_traits<G>::vertex_descriptor</tt>.</TD> |
---|
66 | </TR> |
---|
67 | |
---|
68 | </table> |
---|
69 | |
---|
70 | <h3>Associated Types</h3> |
---|
71 | |
---|
72 | none |
---|
73 | <p> |
---|
74 | |
---|
75 | <h3>Valid Expressions</h3> |
---|
76 | |
---|
77 | <table border> |
---|
78 | <tr> |
---|
79 | <th>Name</th><th>Expression</th><th>Return Type</th><th>Description</th> |
---|
80 | </tr> |
---|
81 | |
---|
82 | <tr> |
---|
83 | <td>Initialize Vertex</td> |
---|
84 | <td><tt>vis.initialize_vertex(s, g)</tt></td> |
---|
85 | <td><tt>void</tt></td> |
---|
86 | <td> |
---|
87 | This is invoked on every vertex of the graph before the start of the |
---|
88 | graph search. |
---|
89 | </td> |
---|
90 | </tr> |
---|
91 | |
---|
92 | <tr> |
---|
93 | <td>Start Vertex</td> |
---|
94 | <td><tt>vis.start_vertex(s, g)</tt></td> |
---|
95 | <td><tt>void</tt></td> |
---|
96 | <td> |
---|
97 | This is invoked on the source vertex once before the start of the |
---|
98 | search. |
---|
99 | </td> |
---|
100 | </tr> |
---|
101 | |
---|
102 | <tr> |
---|
103 | <td>Discover Vertex</td> |
---|
104 | <td><tt>vis.discover_vertex(u, g)</tt></td> |
---|
105 | <td><tt>void</tt></td> |
---|
106 | <td> |
---|
107 | This is invoked when a vertex is encountered for the first time. |
---|
108 | </td> |
---|
109 | </tr> |
---|
110 | |
---|
111 | <tr> |
---|
112 | <td>Examine Edge</td> |
---|
113 | <td><tt>vis.examine_edge(e, g)</tt></td> |
---|
114 | <td><tt>void</tt></td> |
---|
115 | <td> |
---|
116 | This is invoked on every out-edge of each vertex after it is discovered. |
---|
117 | </td> |
---|
118 | </tr> |
---|
119 | |
---|
120 | |
---|
121 | <tr> |
---|
122 | <td>Tree Edge</td> |
---|
123 | <td><tt>vis.tree_edge(e, g)</tt></td> |
---|
124 | <td><tt>void</tt></td> |
---|
125 | <td> |
---|
126 | This is invoked on each edge as it becomes a member of the edges that |
---|
127 | form the search tree.</td> |
---|
128 | </tr> |
---|
129 | |
---|
130 | <tr> |
---|
131 | <td>Back Edge</td> |
---|
132 | <td><tt>vis.back_edge(e, g)</tt></td> |
---|
133 | <td><tt>void</tt></td> |
---|
134 | <td> |
---|
135 | This is invoked on the back edges in the graph. For an undirected |
---|
136 | graph there is some ambiguity between tree edges and back edges since |
---|
137 | the edge <i>(u,v)</i> and <i>(v,u)</i> are the same edge, but both the |
---|
138 | <tt>tree_edge()</tt> and <tt>back_edge()</tt> functions will be |
---|
139 | invoked. One way to resolve this ambiguity is to record the tree |
---|
140 | edges, and then disregard the back-edges that are already marked as |
---|
141 | tree edges. An easy way to record tree edges is to record |
---|
142 | predecessors at the <tt>tree_edge</tt> event point. |
---|
143 | </td> |
---|
144 | </tr> |
---|
145 | |
---|
146 | <tr> |
---|
147 | <td>Forward or Cross Edge</td> |
---|
148 | <td><tt>vis.forward_or_cross_edge(e, g)</tt></td> |
---|
149 | <td><tt>void</tt></td> |
---|
150 | <td> |
---|
151 | This is invoked on forward or cross edges in the graph. In an |
---|
152 | undirected graph this method is never called. |
---|
153 | </td> |
---|
154 | </tr> |
---|
155 | |
---|
156 | <tr> |
---|
157 | <td>Finish Vertex</td> |
---|
158 | <td><tt>vis.finish_vertex(u, g)</tt></td> |
---|
159 | <td><tt>void</tt></td> |
---|
160 | <td> |
---|
161 | This is invoked on vertex <tt>u</tt> after <tt>finish_vertex</tt> has |
---|
162 | been called for all the vertices in the DFS-tree rooted at vertex |
---|
163 | <tt>u</tt>. If vertex <tt>u</tt> is a leaf in the DFS-tree, then |
---|
164 | the <tt>finish_vertex</tt> function is call on <tt>u</tt> after |
---|
165 | all the out-edges of <tt>u</tt> have been examined. |
---|
166 | </td> |
---|
167 | </tr> |
---|
168 | |
---|
169 | </table> |
---|
170 | |
---|
171 | <h3>Models</h3> |
---|
172 | |
---|
173 | <ul> |
---|
174 | <li><a href="./dfs_visitor.html"><tt>dfs_visitor</tt></a> |
---|
175 | </ul> |
---|
176 | |
---|
177 | <a name="python"></a> |
---|
178 | <h3>Python</h3> |
---|
179 | |
---|
180 | To implement a model of the <tt>DFSVisitor</tt> concept in Python, |
---|
181 | create a new class that derives from the <tt>DFSVisitor</tt> type of |
---|
182 | the graph, which will be |
---|
183 | named <tt><i>GraphType</i>.DFSVisitor</tt>. The events and syntax are |
---|
184 | the same as with visitors in C++. Here is an example for the |
---|
185 | Python <tt>bgl.Graph</tt> graph type: |
---|
186 | |
---|
187 | <pre> |
---|
188 | class count_tree_edges_dfs_visitor(bgl.Graph.DFSVisitor): |
---|
189 | def __init__(self, name_map): |
---|
190 | bgl.Graph.DFSVisitor.__init__(self) |
---|
191 | self.name_map = name_map |
---|
192 | |
---|
193 | def tree_edge(self, e, g): |
---|
194 | (u, v) = (g.source(e), g.target(e)) |
---|
195 | print "Tree edge ", |
---|
196 | print self.name_map[u], |
---|
197 | print " -> ", |
---|
198 | print self.name_map[v] |
---|
199 | </pre> |
---|
200 | |
---|
201 | <br> |
---|
202 | <HR> |
---|
203 | <TABLE> |
---|
204 | <TR valign=top> |
---|
205 | <TD nowrap>Copyright © 2000-2001</TD><TD> |
---|
206 | <A HREF="../../../people/jeremy_siek.htm">Jeremy Siek</A>, |
---|
207 | Indiana University (<A |
---|
208 | HREF="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</A>)<br> |
---|
209 | <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> |
---|
210 | <A HREF=http://www.osl.iu.edu/~lums>Andrew Lumsdaine</A>, |
---|
211 | Indiana University (<A |
---|
212 | HREF="mailto:lums@osl.iu.edu">lums@osl.iu.edu</A>) |
---|
213 | </TD></TR></TABLE> |
---|
214 | |
---|
215 | </BODY> |
---|
216 | </HTML> |
---|