BellmanFordVisitor.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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: Bellman Ford 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)"/>Bellman Ford Visitor Concept</H1>
  16. This concept defines the visitor interface for <a
  17. href="./bellman_ford_shortest.html"><tt>bellman_ford_shortest_paths()</tt></a>.
  18. Users can define a class with the Bellman Ford Visitor interface and
  19. pass and object of the class to <tt>bellman_ford_shortest_paths()</tt>,
  20. thereby 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 Bellman Ford 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>Examine Edge</td>
  61. <td><tt>vis.examine_edge(e, g)</tt></td>
  62. <td><tt>void</tt></td>
  63. <td>
  64. This is invoked on every edge in the graph <tt>num_vertices(g)</tt> times.
  65. </td>
  66. </tr>
  67. <tr>
  68. <td>Edge Relaxed</td>
  69. <td><tt>vis.edge_relaxed(e, g)</tt></td>
  70. <td><tt>void</tt></td>
  71. <td>
  72. Upon examination, if the following condition holds then the edge
  73. is relaxed (its distance is reduced), and this method is invoked.<br>
  74. <tt>
  75. tie(u,v) = incident(e, g);<br>
  76. D d_u = get(d, u), d_v = get(d, v);<br>
  77. W w_e = get(w, e);<br>
  78. assert(compare(combine(d_u, w_e), d_v));<br>
  79. </tt>
  80. </td>
  81. </tr>
  82. <tr>
  83. <td>Edge Not Relaxed</td>
  84. <td><tt>edge_not_relaxed(e, g)</tt></td>
  85. <td><tt>void</tt></td>
  86. <td>
  87. Upon examination, if the edge is not relaxed (see above) then
  88. this method is invoked.
  89. </td>
  90. </tr>
  91. <tr>
  92. <td>Edge Minimized</td>
  93. <td><tt>vis.edge_minimized(e, g)</tt></td>
  94. <td><tt>void</tt></td>
  95. <td>
  96. After <tt>num_vertices(g)</tt> iterations through the edge set
  97. of the graph are completed, one last iteration is made to test whether
  98. each edge was minimized. If the edge is minimized then this function
  99. is invoked.
  100. </td>
  101. </tr>
  102. <tr>
  103. <td>Edge Not Minimized</td>
  104. <td><tt>edge_not_minimized(e, g)</tt></td>
  105. <td><tt>void</tt></td>
  106. <td>
  107. If the edge is not minimized, this function is invoked. This happens
  108. when there is a negative cycle in the graph.
  109. </td>
  110. </tr>
  111. </table>
  112. <h3>Models</h3>
  113. <ul>
  114. <li><a href="./bellman_visitor.html"><tt>bellman_visitor</tt></a>
  115. </ul>
  116. <a name="python"></a>
  117. <h3>Python</h3>
  118. To implement a model of the <tt>BellmanFordVisitor</tt> concept in Python,
  119. create a new class that derives from the <tt>BellmanFordVisitor</tt> type of
  120. the graph, which will be
  121. named <tt><i>GraphType</i>.BellmanFordVisitor</tt>. The events and syntax are
  122. the same as with visitors in C++. Here is an example for the
  123. Python <tt>bgl.Graph</tt> graph type:
  124. <pre>
  125. class count_tree_edges_bellman_ford_visitor(bgl.Graph.BellmanFordVisitor):
  126. def __init__(self, name_map):
  127. bgl.Graph.BellmanFordVisitor.__init__(self)
  128. self.name_map = name_map
  129. def edge_relaxed(self, e, g):
  130. (u, v) = (g.source(e), g.target(e))
  131. print "Relaxed edge ",
  132. print self.name_map[u],
  133. print " -> ",
  134. print self.name_map[v]
  135. </pre>
  136. <br>
  137. <HR>
  138. <TABLE>
  139. <TR valign=top>
  140. <TD nowrap>Copyright &copy; 2000-2001</TD><TD>
  141. <A HREF="http://www.boost.org/people/jeremy_siek.htm">Jeremy Siek</A>,
  142. Indiana University (<A
  143. HREF="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</A>)<br>
  144. <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>
  145. <A HREF="https://homes.cs.washington.edu/~al75">Andrew Lumsdaine</A>,
  146. Indiana University (<A
  147. HREF="mailto:lums@osl.iu.edu">lums@osl.iu.edu</A>)
  148. </TD></TR></TABLE>
  149. </BODY>
  150. </HTML>