bellman_ford_shortest.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <HTML>
  2. <!--
  3. Copyright (c) Jeremy Siek 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>Bellman Ford Shortest Paths</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><A NAME="sec:bellman-ford"></A><img src="figs/python.gif" alt="(Python)"/>
  16. <TT>bellman_ford_shortest_paths</TT>
  17. </H1>
  18. <P>
  19. <PRE>
  20. <i>// named paramter version</i>
  21. template &lt;class <a href="./EdgeListGraph.html">EdgeListGraph</a>, class Size, class P, class T, class R&gt;
  22. bool bellman_ford_shortest_paths(const EdgeListGraph&amp; g, Size N,
  23. const bgl_named_params&lt;P, T, R&gt;&amp; params = <i>all defaults</i>);
  24. template &lt;class <a href="./VertexAndEdgeListGraph.html">VertexAndEdgeListGraph</a>, class P, class T, class R&gt;
  25. bool bellman_ford_shortest_paths(const VertexAndEdgeListGraph&amp; g,
  26. const bgl_named_params&lt;P, T, R&gt;&amp; params = <i>all defaults</i>);
  27. <i>// non-named parameter version</i>
  28. template &lt;class <a href="./EdgeListGraph.html">EdgeListGraph</a>, class Size, class WeightMap,
  29. class PredecessorMap, class DistanceMap,
  30. class <a href="http://www.boost.org/sgi/stl/BinaryFunction.html">BinaryFunction</a>, class <a href="http://www.boost.org/sgi/stl/BinaryPredicate.html">BinaryPredicate</a>,
  31. class <a href="./BellmanFordVisitor.html">BellmanFordVisitor</a>&gt;
  32. bool bellman_ford_shortest_paths(EdgeListGraph&amp; g, Size N,
  33. WeightMap weight, PredecessorMap pred, DistanceMap distance,
  34. BinaryFunction combine, BinaryPredicate compare, BellmanFordVisitor v)
  35. </PRE>
  36. <P>
  37. The Bellman-Ford algorithm&nbsp;[<A
  38. HREF="bibliography.html#bellman58">4</A>,<A
  39. HREF="bibliography.html#ford62:_flows">11</A>,<A
  40. HREF="bibliography.html#lawler76:_comb_opt">20</A>,<A
  41. HREF="bibliography.html#clr90">8</A>] solves the single-source
  42. shortest paths problem for a graph with both positive and negative
  43. edge weights. For the definition of the shortest paths problem see
  44. Section <A
  45. HREF="./graph_theory_review.html#sec:shortest-paths-algorithms">Shortest-Paths
  46. Algorithms</A>.
  47. If you only need to solve the shortest paths problem for positive edge
  48. weights, Dijkstra's algorithm provides a more efficient
  49. alternative. If all the edge weights are all equal to one then breadth-first
  50. search provides an even more efficient alternative.
  51. </p>
  52. <p>
  53. Before calling the <tt>bellman_ford_shortest_paths()</tt> function,
  54. the user must assign the source vertex a distance of zero and all
  55. other vertices a distance of infinity <i>unless</i> you are providing
  56. a starting vertex. The Bellman-Ford algorithm
  57. proceeds by looping through all of the edges in the graph, applying
  58. the relaxation operation to each edge. In the following pseudo-code,
  59. <i>v</i> is a vertex adjacent to <i>u</i>, <i>w</i> maps edges to
  60. their weight, and <i>d</i> is a distance map that records the length
  61. of the shortest path to each vertex seen so far. <i>p</i> is a
  62. predecessor map which records the parent of each vertex, which will
  63. ultimately be the parent in the shortest paths tree
  64. </p>
  65. <table>
  66. <tr>
  67. <td valign="top">
  68. <pre>
  69. RELAX(<i>u</i>, <i>v</i>, <i>w</i>, <i>d</i>, <i>p</i>)
  70. <b>if</b> (<i>w(u,v) + d[u] < d[v]</i>)
  71. <i>d[v] := w(u,v) + d[u]</i>
  72. <i>p[v] := u</i>
  73. <b>else</b>
  74. ...
  75. </pre>
  76. </td>
  77. <td valign="top">
  78. <pre>
  79. relax edge <i>(u,v)</i>
  80. edge <i>(u,v)</i> is not relaxed
  81. </pre>
  82. </td>
  83. </tr>
  84. </table>
  85. <p>
  86. The algorithm repeats this loop <i>|V|</i> times after which it is
  87. guaranteed that the distances to each vertex have been reduced to the
  88. minimum possible unless there is a negative cycle in the graph. If
  89. there is a negative cycle, then there will be edges in the graph that
  90. were not properly minimized. That is, there will be edges <i>(u,v)</i> such
  91. that <i>w(u,v) + d[u] < d[v]</i>. The algorithm loops over the edges in
  92. the graph one final time to check if all the edges were minimized,
  93. returning <tt>true</tt> if they were and returning <tt>false</tt>
  94. otherwise.
  95. </p>
  96. <table>
  97. <tr>
  98. <td valign="top">
  99. <pre>
  100. BELLMAN-FORD(<i>G</i>)
  101. <i>// Optional initialization</i>
  102. <b>for</b> each vertex <i>u in V</i>
  103. <i>d[u] := infinity</i>
  104. <i>p[u] := u</i>
  105. <b>end for</b>
  106. <b>for</b> <i>i := 1</i> <b>to</b> <i>|V|-1</i>
  107. <b>for</b> each edge <i>(u,v) in E</i>
  108. RELAX(<i>u</i>, <i>v</i>, <i>w</i>, <i>d</i>, <i>p</i>)
  109. <b>end for</b>
  110. <b>end for</b>
  111. <b>for</b> each edge <i>(u,v) in E</i>
  112. <b>if</b> (<i>w(u,v) + d[u] < d[v]</i>)
  113. <b>return</b> (false, , )
  114. <b>else</b>
  115. ...
  116. <b>end for</b>
  117. <b>return</b> (true, <i>p</i>, <i>d</i>)
  118. </pre>
  119. </td>
  120. <td valign="top">
  121. <pre>
  122. examine edge <i>(u,v)</i>
  123. edge <i>(u,v)</i> was not minimized
  124. edge <i>(u,v)</i> was minimized
  125. </pre>
  126. </td>
  127. </tr>
  128. </table>
  129. There are two main options for obtaining output from the
  130. <tt>bellman_ford_shortest_paths()</tt> function. If the user provides
  131. a distance property map through the <tt>distance_map()</tt> parameter
  132. then the shortest distance from the source vertex to every other
  133. vertex in the graph will be recorded in the distance map (provided the
  134. function returns <tt>true</tt>). The second option is recording the
  135. shortest paths tree in the <tt>predecessor_map()</tt>. For each vertex
  136. <i>u in V</i>, <i>p[u]</i> will be the predecessor of <i>u</i> in the
  137. shortest paths tree (unless <i>p[u] = u</i>, in which case <i>u</i> is
  138. either the source vertex or a vertex unreachable from the source). In
  139. addition to these two options, the user can provide her own
  140. custom-made visitor that can take actions at any of the
  141. algorithm's event points.
  142. <P>
  143. <h3>Parameters</h3>
  144. IN: <tt>EdgeListGraph&amp; g</tt>
  145. <blockquote>
  146. A directed or undirected graph whose type must be a model of
  147. <a href="./EdgeListGraph.html">Edge List Graph</a>. If a root vertex is
  148. provided, then the graph must also model
  149. <a href="./VertexListGraph.html">Vertex List Graph</a>.<br>
  150. <b>Python</b>: The parameter is named <tt>graph</tt>.
  151. </blockquote>
  152. IN: <tt>Size N</tt>
  153. <blockquote>
  154. The number of vertices in the graph. The type <tt>Size</tt> must
  155. be an integer type.<br>
  156. <b>Default:</b> <tt>num_vertices(g)</tt>.<br>
  157. <b>Python</b>: Unsupported parameter.
  158. </blockquote>
  159. <h3>Named Parameters</h3>
  160. IN: <tt>weight_map(WeightMap w)</tt>
  161. <blockquote>
  162. The weight (also know as ``length'' or ``cost'') of each edge in the
  163. graph. The <tt>WeightMap</tt> type must be a model of <a
  164. href="../../property_map/doc/ReadablePropertyMap.html">Readable Property
  165. Map</a>. The key type for this property map must be the edge
  166. descriptor of the graph. The value type for the weight map must be
  167. <i>Addable</i> with the distance map's value type. <br>
  168. <b>Default:</b> <tt>get(edge_weight, g)</tt><br>
  169. <b>Python</b>: Must be an <tt>edge_double_map</tt> for the graph.<br>
  170. <b>Python default</b>: <tt>graph.get_edge_double_map("weight")</tt>
  171. </blockquote>
  172. OUT: <tt>predecessor_map(PredecessorMap p_map)</tt>
  173. <blockquote>
  174. The predecessor map records the edges in the minimum spanning
  175. tree. Upon completion of the algorithm, the edges <i>(p[u],u)</i>
  176. for all <i>u in V</i> are in the minimum spanning tree. If <i>p[u] =
  177. u</i> then <i>u</i> is either the source vertex or a vertex that is
  178. not reachable from the source. The <tt>PredecessorMap</tt> type
  179. must be a <a
  180. href="../../property_map/doc/ReadWritePropertyMap.html">Read/Write
  181. Property Map</a> which key and vertex types the same as the vertex
  182. descriptor type of the graph.<br>
  183. <b>Default:</b> <tt>dummy_property_map</tt><br>
  184. <b>Python</b>: Must be a <tt>vertex_vertex_map</tt> for the graph.<br>
  185. </blockquote>
  186. IN/OUT: <tt>distance_map(DistanceMap d)</tt>
  187. <blockquote>
  188. The shortest path weight from the source vertex to each vertex in
  189. the graph <tt>g</tt> is recorded in this property map. The type
  190. <tt>DistanceMap</tt> must be a model of <a
  191. href="../../property_map/doc/ReadWritePropertyMap.html">Read/Write
  192. Property Map</a>. The key type of the property map must be the
  193. vertex descriptor type of the graph, and the value type of the
  194. distance map must be <a
  195. href="http://www.boost.org/sgi/stl/LessThanComparable.html"> Less
  196. Than Comparable</a>.<br> <b>Default:</b> <tt>get(vertex_distance,
  197. g)</tt><br>
  198. <b>Python</b>: Must be a <tt>vertex_double_map</tt> for the graph.<br>
  199. </blockquote>
  200. IN: <tt>root_vertex(Vertex s)</tt>
  201. <blockquote>
  202. The starting (or "root") vertex from which shortest paths will be
  203. computed. When provided, the distance map need not be initialized
  204. (the algorithm will perform the initialization itself). However, the
  205. graph must model <a href="./VertexListGraph.html">Vertex List
  206. Graph</a> when this parameter is provided.<br>
  207. <b>Default:</b> None; if omitted, the user must initialize the
  208. distance map.
  209. </blockquote>
  210. IN: <tt>visitor(BellmanFordVisitor v)</tt>
  211. <blockquote>
  212. The visitor object, whose type must be a model of
  213. <a href="./BellmanFordVisitor.html">Bellman-Ford Visitor</a>.
  214. The visitor object is passed by value <a
  215. href="#1">[1]</a>.
  216. <br>
  217. <b>Default:</b> <tt>bellman_visitor&lt;null_visitor&gt;</tt><br>
  218. <b>Python</b>: The parameter should be an object that derives from
  219. the <a
  220. href="BellmanFordVisitor.html#python"><tt>BellmanFordVisitor</tt></a> type
  221. of the graph.
  222. </blockquote>
  223. IN: <tt>distance_combine(BinaryFunction combine)</tt>
  224. <blockquote>
  225. This function object replaces the role of addition in the relaxation
  226. step. The first argument type must match the distance map's value
  227. type and the second argument type must match the weight map's value
  228. type. The result type must be the same as the distance map's value
  229. type.<br>
  230. <b>Default:</b><tt>std::plus&lt;D&gt;</tt>
  231. with <tt>D=typename&nbsp;property_traits&lt;DistanceMap&gt;::value_type</tt>.<br>
  232. <b>Python</b>: Unsupported parameter.
  233. </blockquote>
  234. IN: <tt>distance_compare(BinaryPredicate compare)</tt>
  235. <blockquote>
  236. This function object replaces the role of the less-than operator
  237. that compares distances in the relaxation step. The argument types
  238. must match the distance map's value type.<br>
  239. <b>Default:</b> <tt>std::less&lt;D&gt;</tt>
  240. with <tt>D=typename&nbsp;property_traits&lt;DistanceMap&gt;::value_type</tt>.<br>
  241. <b>Python</b>: Unsupported parameter.
  242. </blockquote>
  243. <P>
  244. <H3>Complexity</H3>
  245. <P>
  246. The time complexity is <i>O(V E)</i>.
  247. <h3>Visitor Event Points</h3>
  248. <ul>
  249. <li><b><tt>vis.examine_edge(e, g)</tt></b> is invoked on every edge in
  250. the graph <i>|V|</i> times.
  251. <li><b><tt>vis.edge_relaxed(e, g)</tt></b> is invoked when the distance
  252. label for the target vertex is decreased. The edge <i>(u,v)</i> that
  253. participated in the last relaxation for vertex <i>v</i> is an edge in the
  254. shortest paths tree.
  255. <li><b><tt>vis.edge_not_relaxed(e, g)</tt></b> is invoked if the distance label
  256. for the target vertex is not decreased.
  257. <li><b><tt>vis.edge_minimized(e, g)</tt></b> is invoked during the
  258. second stage of the algorithm, during the test of whether each edge
  259. was minimized. If the edge is minimized then this function
  260. is invoked.
  261. <li><b><tt>vis.edge_not_minimized(e, g)</tt></b> is also invoked during the
  262. second stage of the algorithm, during the test of whether each edge
  263. was minimized. If the edge was not minimized, this function is
  264. invoked. This happens when there is a negative cycle in the graph.
  265. </ul>
  266. <H3>Example</H3>
  267. <P>
  268. An example of using the Bellman-Ford algorithm is in <a
  269. href="../example/bellman-example.cpp"><TT>examples/bellman-example.cpp</TT></a>.
  270. <h3>Notes</h3>
  271. <p><a name="1">[1]</a>
  272. Since the visitor parameter is passed by value, if your visitor
  273. contains state then any changes to the state during the algorithm
  274. will be made to a copy of the visitor object, not the visitor object
  275. passed in. Therefore you may want the visitor to hold this state by
  276. pointer or reference.
  277. <br>
  278. <HR>
  279. <TABLE>
  280. <TR valign=top>
  281. <TD nowrap>Copyright &copy; 2000</TD><TD>
  282. <A HREF="http://www.boost.org/people/jeremy_siek.htm">Jeremy Siek</A>, Indiana University (<A HREF="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</A>)
  283. </TD></TR></TABLE>
  284. </BODY>
  285. </HTML>