BFSVisitor.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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>Boost Graph Library: BFSVisitor</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)"/>BFS Visitor Concept</H1>
  16. This concept defines the visitor interface for <a
  17. href="./breadth_first_search.html"><tt>breadth_first_search()</tt></a>.
  18. Users can define a class with the BFS Visitor interface and pass and
  19. object of the class to <tt>breadth_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 BFS 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>Discover Vertex</td>
  70. <td><tt>vis.discover_vertex(u, g)</tt></td>
  71. <td><tt>void</tt></td>
  72. <td>
  73. This is invoked when a vertex is encountered for the first time.
  74. </td>
  75. </tr>
  76. <tr>
  77. <td>Examine Vertex</td>
  78. <td><tt>vis.examine_vertex(u, g)</tt></td>
  79. <td><tt>void</tt></td>
  80. <td>
  81. This is invoked on a vertex as it is popped from the queue. This
  82. happens immediately before <tt>examine_edge()</tt> is invoked
  83. on each of the out-edges of vertex <tt>u</tt>.
  84. </td>
  85. </tr>
  86. <tr>
  87. <td>Examine Edge</td>
  88. <td><tt>vis.examine_edge(e, g)</tt></td>
  89. <td><tt>void</tt></td>
  90. <td>
  91. This is invoked on every out-edge of each vertex after it is discovered.
  92. </td>
  93. </tr>
  94. <tr>
  95. <td>Tree Edge</td>
  96. <td><tt>vis.tree_edge(e, g)</tt></td>
  97. <td><tt>void</tt></td>
  98. <td>
  99. This is invoked on each edge as it becomes a member of the edges that
  100. form the search tree.</td>
  101. </tr>
  102. <tr>
  103. <td>Non-Tree Edge</td>
  104. <td><tt>vis.non_tree_edge(e, g)</tt></td>
  105. <td><tt>void</tt></td>
  106. <td>
  107. This is invoked on back or cross edges for directed graphs and cross
  108. edges for undirected graphs.
  109. </td>
  110. </tr>
  111. <tr>
  112. <td>Gray Target</td>
  113. <td><tt>vis.gray_target(e, g)</tt></td>
  114. <td><tt>void</tt></td>
  115. <td>
  116. This is invoked on the subset of non-tree edges whose target vertex is
  117. colored gray at the time of examination. The color gray indicates
  118. that the vertex is currently in the queue.
  119. </td>
  120. </tr>
  121. <tr>
  122. <td>Black Target</td>
  123. <td><tt>vis.black_target(e, g)</tt></td>
  124. <td><tt>void</tt></td>
  125. <td>
  126. This is invoked on the subset of non-tree edges whose target vertex is
  127. colored black at the time of examination. The color black indicates
  128. that the vertex has been removed from the queue.
  129. </td>
  130. </tr>
  131. <tr>
  132. <td>Finish Vertex</td>
  133. <td><tt>vis.finish_vertex(u, g)</tt></td>
  134. <td><tt>void</tt></td>
  135. <td>
  136. This invoked on a vertex after all of its out edges have been added to the
  137. search tree and all of the adjacent vertices have been discovered
  138. (but before the out-edges of the adjacent vertices have been examined).
  139. </td>
  140. </tr>
  141. </table>
  142. <h3>Models</h3>
  143. <ul>
  144. <li><a href="./bfs_visitor.html"><tt>bfs_visitor</tt></a>
  145. </ul>
  146. <a name="python"></a><h3>Python</h3>
  147. To implement a model of the <tt>BFSVisitor</tt> concept in Python,
  148. create a new class that derives from the <tt>BFSVisitor</tt> type of
  149. the graph, which will be
  150. named <tt><i>GraphType</i>.BFSVisitor</tt>. The events and syntax are
  151. the same as with visitors in C++. Here is an example for the
  152. Python <tt>bgl.Graph</tt> graph type:
  153. <pre>
  154. class count_tree_edges_bfs_visitor(bgl.Graph.BFSVisitor):
  155. def __init__(self, name_map):
  156. bgl.Graph.BFSVisitor.__init__(self)
  157. self.name_map = name_map
  158. def tree_edge(self, e, g):
  159. (u, v) = (g.source(e), g.target(e))
  160. print "Tree edge ",
  161. print self.name_map[u],
  162. print " -> ",
  163. print self.name_map[v]
  164. </pre>
  165. <h3>See also</h3>
  166. <a href="./visitor_concepts.html">Visitor concepts</a>
  167. <br>
  168. <HR>
  169. <TABLE>
  170. <TR valign=top>
  171. <TD nowrap>Copyright &copy; 2000-2001</TD><TD>
  172. <A HREF="http://www.boost.org/people/jeremy_siek.htm">Jeremy Siek</A>,
  173. Indiana University (<A
  174. HREF="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</A>)<br>
  175. <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>
  176. <A HREF="https://homes.cs.washington.edu/~al75">Andrew Lumsdaine</A>,
  177. Indiana University (<A
  178. HREF="mailto:lums@osl.iu.edu">lums@osl.iu.edu</A>)
  179. </TD></TR></TABLE>
  180. </BODY>
  181. </HTML>