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