DFSVisitor.html 5.5 KB

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