| 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>Boost Graph Library: BFSVisitor</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)"/>BFS Visitor Concept</H1> | 
|---|
| 23 |  | 
|---|
| 24 | This concept defines the visitor interface for <a | 
|---|
| 25 | href="./breadth_first_search.html"><tt>breadth_first_search()</tt></a>. | 
|---|
| 26 | Users can define a class with the BFS Visitor interface and pass and | 
|---|
| 27 | object of the class to <tt>breadth_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 |  | 
|---|
| 36 | <h3>Notation</h3> | 
|---|
| 37 |  | 
|---|
| 38 | <Table> | 
|---|
| 39 | <TR> | 
|---|
| 40 | <TD><tt>V</tt></TD> | 
|---|
| 41 | <TD>A type that is a model of BFS Visitor.</TD> | 
|---|
| 42 | </TR> | 
|---|
| 43 |  | 
|---|
| 44 | <TR> | 
|---|
| 45 | <TD><tt>vis</tt></TD> | 
|---|
| 46 | <TD>An object of type <tt>V</tt>.</TD> | 
|---|
| 47 | </TR> | 
|---|
| 48 |  | 
|---|
| 49 | <TR> | 
|---|
| 50 | <TD><tt>G</tt></TD> | 
|---|
| 51 | <TD>A type that is a model of Graph.</TD> | 
|---|
| 52 | </TR> | 
|---|
| 53 |  | 
|---|
| 54 | <TR> | 
|---|
| 55 | <TD><tt>g</tt></TD> | 
|---|
| 56 | <TD>An object of type <tt>G</tt>.</TD> | 
|---|
| 57 | </TR> | 
|---|
| 58 |  | 
|---|
| 59 | <TR> | 
|---|
| 60 | <TD><tt>e</tt></TD> | 
|---|
| 61 | <TD>An object of type <tt>boost::graph_traits<G>::edge_descriptor</tt>.</TD> | 
|---|
| 62 | </TR> | 
|---|
| 63 |  | 
|---|
| 64 | <TR> | 
|---|
| 65 | <TD><tt>s,u</tt></TD> | 
|---|
| 66 | <TD>An object of type <tt>boost::graph_traits<G>::vertex_descriptor</tt>.</TD> | 
|---|
| 67 | </TR> | 
|---|
| 68 |  | 
|---|
| 69 | </table> | 
|---|
| 70 |  | 
|---|
| 71 | <h3>Associated Types</h3> | 
|---|
| 72 |  | 
|---|
| 73 | none | 
|---|
| 74 | <p> | 
|---|
| 75 |  | 
|---|
| 76 | <h3>Valid Expressions</h3> | 
|---|
| 77 |  | 
|---|
| 78 | <table border> | 
|---|
| 79 | <tr> | 
|---|
| 80 | <th>Name</th><th>Expression</th><th>Return Type</th><th>Description</th> | 
|---|
| 81 | </tr> | 
|---|
| 82 |  | 
|---|
| 83 | <tr> | 
|---|
| 84 | <td>Initialize Vertex</td> | 
|---|
| 85 | <td><tt>vis.initialize_vertex(s, g)</tt></td> | 
|---|
| 86 | <td><tt>void</tt></td> | 
|---|
| 87 | <td> | 
|---|
| 88 | This is invoked on every vertex of the graph before the start of the | 
|---|
| 89 | graph search. | 
|---|
| 90 | </td> | 
|---|
| 91 | </tr> | 
|---|
| 92 |  | 
|---|
| 93 | <tr> | 
|---|
| 94 | <td>Discover Vertex</td> | 
|---|
| 95 | <td><tt>vis.discover_vertex(u, g)</tt></td> | 
|---|
| 96 | <td><tt>void</tt></td> | 
|---|
| 97 | <td> | 
|---|
| 98 | This is invoked when a vertex is encountered for the first time. | 
|---|
| 99 | </td> | 
|---|
| 100 | </tr> | 
|---|
| 101 |  | 
|---|
| 102 | <tr> | 
|---|
| 103 | <td>Examine Vertex</td> | 
|---|
| 104 | <td><tt>vis.examine_vertex(u, g)</tt></td> | 
|---|
| 105 | <td><tt>void</tt></td> | 
|---|
| 106 | <td> | 
|---|
| 107 | This is invoked on a vertex as it is popped from the queue. This | 
|---|
| 108 | happens immediately before <tt>examine_edge()</tt> is invoked | 
|---|
| 109 | on each of the out-edges of vertex <tt>u</tt>. | 
|---|
| 110 | </td> | 
|---|
| 111 | </tr> | 
|---|
| 112 |  | 
|---|
| 113 | <tr> | 
|---|
| 114 | <td>Examine Edge</td> | 
|---|
| 115 | <td><tt>vis.examine_edge(e, g)</tt></td> | 
|---|
| 116 | <td><tt>void</tt></td> | 
|---|
| 117 | <td> | 
|---|
| 118 | This is invoked on every out-edge of each vertex after it is discovered. | 
|---|
| 119 | </td> | 
|---|
| 120 | </tr> | 
|---|
| 121 |  | 
|---|
| 122 |  | 
|---|
| 123 | <tr> | 
|---|
| 124 | <td>Tree Edge</td> | 
|---|
| 125 | <td><tt>vis.tree_edge(e, g)</tt></td> | 
|---|
| 126 | <td><tt>void</tt></td> | 
|---|
| 127 | <td> | 
|---|
| 128 | This is invoked on each edge as it becomes a member of the edges that | 
|---|
| 129 | form the search tree.</td> | 
|---|
| 130 | </tr> | 
|---|
| 131 |  | 
|---|
| 132 | <tr> | 
|---|
| 133 | <td>Non-Tree Edge</td> | 
|---|
| 134 | <td><tt>vis.non_tree_edge(e, g)</tt></td> | 
|---|
| 135 | <td><tt>void</tt></td> | 
|---|
| 136 | <td> | 
|---|
| 137 | This is invoked on back or cross edges for directed graphs and cross | 
|---|
| 138 | edges for undirected graphs. | 
|---|
| 139 | </td> | 
|---|
| 140 | </tr> | 
|---|
| 141 |  | 
|---|
| 142 | <tr> | 
|---|
| 143 | <td>Gray Target</td> | 
|---|
| 144 | <td><tt>vis.gray_target(e, g)</tt></td> | 
|---|
| 145 | <td><tt>void</tt></td> | 
|---|
| 146 | <td> | 
|---|
| 147 | This is invoked on the subset of non-tree edges who's target vertex is | 
|---|
| 148 | colored gray at the time of examination. The color gray indicates | 
|---|
| 149 | that the vertex is currently in the queue.  | 
|---|
| 150 | </td> | 
|---|
| 151 | </tr> | 
|---|
| 152 |  | 
|---|
| 153 | <tr> | 
|---|
| 154 | <td>Black Target</td> | 
|---|
| 155 | <td><tt>vis.black_target(e, g)</tt></td> | 
|---|
| 156 | <td><tt>void</tt></td> | 
|---|
| 157 | <td> | 
|---|
| 158 | This is invoked on the subset of non-tree edges who's target vertex is | 
|---|
| 159 | colored black at the time of examination. The color black indicates | 
|---|
| 160 | that the vertex has been removed from the queue. | 
|---|
| 161 | </td> | 
|---|
| 162 | </tr> | 
|---|
| 163 |  | 
|---|
| 164 | <tr> | 
|---|
| 165 | <td>Finish Vertex</td> | 
|---|
| 166 | <td><tt>vis.finish_vertex(u, g)</tt></td> | 
|---|
| 167 | <td><tt>void</tt></td> | 
|---|
| 168 | <td> | 
|---|
| 169 | This invoked on a vertex after all of its out edges have been added to the | 
|---|
| 170 | search tree and all of the adjacent vertices have been discovered | 
|---|
| 171 | (but before the out-edges of the adjacent vertices have been examined). | 
|---|
| 172 | </td> | 
|---|
| 173 | </tr> | 
|---|
| 174 |  | 
|---|
| 175 | </table> | 
|---|
| 176 |  | 
|---|
| 177 | <h3>Models</h3> | 
|---|
| 178 |  | 
|---|
| 179 | <ul> | 
|---|
| 180 |  <li><a href="./bfs_visitor.html"><tt>bfs_visitor</tt></a> | 
|---|
| 181 | </ul> | 
|---|
| 182 |  | 
|---|
| 183 | <a name="python"></a><h3>Python</h3> | 
|---|
| 184 | To implement a model of the <tt>BFSVisitor</tt> concept in Python, | 
|---|
| 185 | create a new class that derives from the <tt>BFSVisitor</tt> type of | 
|---|
| 186 | the graph, which will be | 
|---|
| 187 | named <tt><i>GraphType</i>.BFSVisitor</tt>. The events and syntax are | 
|---|
| 188 | the same as with visitors in C++. Here is an example for the | 
|---|
| 189 | Python <tt>bgl.Graph</tt> graph type: | 
|---|
| 190 |  | 
|---|
| 191 | <pre> | 
|---|
| 192 | class count_tree_edges_bfs_visitor(bgl.Graph.BFSVisitor): | 
|---|
| 193 |   def __init__(self, name_map): | 
|---|
| 194 |     bgl.Graph.BFSVisitor.__init__(self) | 
|---|
| 195 |     self.name_map = name_map | 
|---|
| 196 |  | 
|---|
| 197 |   def tree_edge(self, e, g): | 
|---|
| 198 |     (u, v) = (g.source(e), g.target(e)) | 
|---|
| 199 |     print "Tree edge ", | 
|---|
| 200 |     print self.name_map[u], | 
|---|
| 201 |     print " -> ", | 
|---|
| 202 |     print self.name_map[v] | 
|---|
| 203 | </pre> | 
|---|
| 204 |  | 
|---|
| 205 | <h3>See also</h3> | 
|---|
| 206 |  | 
|---|
| 207 | <a href="./visitor_concepts.html">Visitor concepts</a> | 
|---|
| 208 |  | 
|---|
| 209 | <br> | 
|---|
| 210 | <HR> | 
|---|
| 211 | <TABLE> | 
|---|
| 212 | <TR valign=top> | 
|---|
| 213 | <TD nowrap>Copyright © 2000-2001</TD><TD> | 
|---|
| 214 | <A HREF="../../../people/jeremy_siek.htm">Jeremy Siek</A>, | 
|---|
| 215 | Indiana University (<A | 
|---|
| 216 | HREF="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</A>)<br> | 
|---|
| 217 | <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> | 
|---|
| 218 | <A HREF=http://www.osl.iu.edu/~lums>Andrew Lumsdaine</A>, | 
|---|
| 219 | Indiana University (<A | 
|---|
| 220 | HREF="mailto:lums@osl.iu.edu">lums@osl.iu.edu</A>) | 
|---|
| 221 | </TD></TR></TABLE> | 
|---|
| 222 |  | 
|---|
| 223 | </BODY> | 
|---|
| 224 | </HTML>  | 
|---|