dijkstra_shortest_paths.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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>Boost Graph Library: Dijkstra's 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:dijkstra"></A><img src="figs/python.gif" alt="(Python)"/>
  16. <TT>dijkstra_shortest_paths</TT>
  17. </H1>
  18. <P>
  19. <PRE>
  20. <i>// named parameter version</i>
  21. template &lt;typename Graph, typename P, typename T, typename R&gt;
  22. void
  23. dijkstra_shortest_paths(Graph&amp; g,
  24. typename graph_traits&lt;Graph&gt;::vertex_descriptor s,
  25. const bgl_named_params&lt;P, T, R&gt;&amp; params);
  26. <i>// non-named parameter version</i>
  27. template &lt;typename Graph, typename <a href="DijkstraVisitor.html">DijkstraVisitor</a>,
  28. typename PredecessorMap, typename DistanceMap,
  29. typename WeightMap, typename VertexIndexMap, typename <a href="http://www.boost.org/sgi/stl/BinaryPredicate.html">CompareFunction</a>, typename <a href="http://www.boost.org/sgi/stl/BinaryFunction.html">CombineFunction</a>,
  30. typename DistInf, typename DistZero, typename ColorMap = <i>default</i>&gt;
  31. void dijkstra_shortest_paths
  32. (const Graph&amp; g,
  33. typename graph_traits&lt;Graph&gt;::vertex_descriptor s,
  34. PredecessorMap predecessor, DistanceMap distance, WeightMap weight,
  35. VertexIndexMap index_map,
  36. CompareFunction compare, CombineFunction combine, DistInf inf, DistZero zero,
  37. DijkstraVisitor vis, ColorMap color = <i>default</i>)
  38. <i>// version that does not initialize the property maps (except for the default color map)</i>
  39. template &lt;class Graph, class DijkstraVisitor,
  40. class PredecessorMap, class DistanceMap,
  41. class WeightMap, class IndexMap, class Compare, class Combine,
  42. class DistZero, class ColorMap&gt;
  43. void
  44. dijkstra_shortest_paths_no_init
  45. (const Graph&amp; g,
  46. typename graph_traits&lt;Graph&gt;::vertex_descriptor s,
  47. PredecessorMap predecessor, DistanceMap distance, WeightMap weight,
  48. IndexMap index_map,
  49. Compare compare, Combine combine, DistZero zero,
  50. DijkstraVisitor vis, ColorMap color = <i>default</i>);
  51. </PRE>
  52. <P>
  53. This algorithm&nbsp;[<A HREF="bibliography.html#dijkstra59">10</A>,<A
  54. HREF="bibliography.html#clr90">8</A>] solves the single-source
  55. shortest-paths problem on a weighted, directed or undirected graph for
  56. the case where all edge weights are nonnegative. Use the Bellman-Ford
  57. algorithm for the case when some edge weights are negative. Use
  58. breadth-first search instead of Dijkstra's algorithm when all edge
  59. weights are equal to one. For the definition of the shortest-path
  60. problem see Section <A
  61. HREF="graph_theory_review.html#sec:shortest-paths-algorithms">Shortest-Paths
  62. Algorithms</A> for some background to the shortest-path problem.
  63. </P>
  64. <P>
  65. There are two main options for obtaining output from the
  66. <tt>dijkstra_shortest_paths()</tt> function. If you provide a
  67. distance property map through the <tt>distance_map()</tt> parameter
  68. then the shortest distance from the source vertex to every other
  69. vertex in the graph will be recorded in the distance map. Also you can
  70. record the shortest paths tree in a predecessor map: for each vertex
  71. <i>u in V</i>, <i>p[u]</i> will be the predecessor of <i>u</i> in
  72. the shortest paths tree (unless <i>p[u] = u</i>, in which case <i>u</i> is
  73. either the source or a vertex unreachable from the source). In
  74. addition to these two options, the user can provide their own
  75. custom-made visitor that takes actions during any of the
  76. algorithm's event points.</P>
  77. <P>
  78. Dijkstra's algorithm finds all the shortest paths from the source
  79. vertex to every other vertex by iteratively ``growing'' the set of
  80. vertices <i>S</i> to which it knows the shortest path. At each step of
  81. the algorithm, the next vertex added to <i>S</i> is determined by a
  82. priority queue. The queue contains the vertices in <i>V - S</i><a
  83. href="#1">[1]</a> prioritized by their distance label, which is the
  84. length of the shortest path seen so far for each vertex. The vertex
  85. <i>u</i> at the top of the priority queue is then added to <i>S</i>,
  86. and each of its out-edges is relaxed: if the distance to <i>u</i> plus
  87. the weight of the out-edge <i>(u,v)</i> is less than the distance
  88. label for <i>v</i> then the estimated distance for vertex <i>v</i> is
  89. reduced. The algorithm then loops back, processing the next vertex at
  90. the top of the priority queue. The algorithm finishes when the
  91. priority queue is empty.
  92. </P>
  93. <P>
  94. The algorithm uses color markers (white, gray, and black) to keep
  95. track of which set each vertex is in. Vertices colored black are in
  96. <i>S</i>. Vertices colored white or gray are in <i>V-S</i>. White vertices have
  97. not yet been discovered and gray vertices are in the priority queue.
  98. By default, the algorithm will allocate an array to store a color
  99. marker for each vertex in the graph. You can provide your own storage
  100. and access for colors with the <tt>color_map()</tt> parameter.
  101. </P>
  102. <p>
  103. The following is the pseudo-code for Dijkstra's single-source shortest
  104. paths algorithm. <i>w</i> is the edge weight, <i>d</i> is the distance label,
  105. and <i>p</i> is the predecessor of each vertex which is used to encode
  106. the shortest paths tree. <i>Q</i> is a priority queue that supports the
  107. DECREASE-KEY operation. The visitor event points for the algorithm are
  108. indicated by the labels on the right.
  109. </p>
  110. <table>
  111. <tr>
  112. <td valign="top">
  113. <pre>
  114. DIJKSTRA(<i>G</i>, <i>s</i>, <i>w</i>)
  115. <b>for</b> each vertex <i>u in V</i> <b>(This loop is not run in dijkstra_shortest_paths_no_init)</b>
  116. <i>d[u] := infinity</i>
  117. <i>p[u] := u</i>
  118. <i>color[u] :=</i> WHITE
  119. <b>end for</b>
  120. <i>color[s] := </i>GRAY
  121. <i>d[s] := 0</i>
  122. INSERT(<i>Q</i>, <i>s</i>)
  123. <b>while</b> (<i>Q != &Oslash;</i>)
  124. <i>u :=</i> EXTRACT-MIN(<i>Q</i>)
  125. <i>S := S U { u }</i>
  126. <b>for</b> each vertex <i>v in Adj[u]</i>
  127. <b>if</b> (<i>w(u,v) + d[u] < d[v]</i>)
  128. <i>d[v] := w(u,v) + d[u]</i>
  129. <i>p[v] := u</i>
  130. <b>if</b> (<i>color[v] =</i> WHITE)
  131. <i>color[v] :=</i> GRAY
  132. INSERT(<i>Q</i>, <i>v</i>)
  133. <b>else if</b> (<i>color[v] =</i> GRAY)
  134. DECREASE-KEY(<i>Q</i>, <i>v</i>)
  135. <b>else</b>
  136. <i>...</i>
  137. <b>end for</b>
  138. <i>color[u] :=</i> BLACK
  139. <b>end while</b>
  140. return (<i>d</i>, <i>p</i>)
  141. </pre>
  142. </td>
  143. <td valign="top">
  144. <pre>
  145. initialize vertex <i>u</i>
  146. discover vertex <i>s</i>
  147. examine vertex <i>u</i>
  148. examine edge <i>(u,v)</i>
  149. edge <i>(u,v)</i> relaxed
  150. discover vertex <i>v</i>
  151. edge <i>(u,v)</i> not relaxed
  152. finish vertex <i>u</i>
  153. </pre>
  154. </td>
  155. </tr>
  156. </table>
  157. <h3>Where Defined</h3>
  158. <a href="../../../boost/graph/dijkstra_shortest_paths.hpp"><tt>boost/graph/dijkstra_shortest_paths.hpp</tt></a>
  159. <h3>Parameters</h3>
  160. IN: <tt>const Graph&amp; g</tt>
  161. <blockquote>
  162. The graph object on which the algorithm will be applied.
  163. The type <tt>Graph</tt> must be a model of
  164. <a href="./VertexListGraph.html">Vertex List Graph</a>
  165. and <a href="./IncidenceGraph.html">Incidence Graph</a>.<br>
  166. <b>Python</b>: The parameter is named <tt>graph</tt>.
  167. </blockquote>
  168. IN: <tt>vertex_descriptor s</tt>
  169. <blockquote>
  170. The source vertex. All distance will be calculated from this vertex,
  171. and the shortest paths tree will be rooted at this vertex.<br>
  172. <b>Python</b>: The parameter is named <tt>root_vertex</tt>.
  173. </blockquote>
  174. <h3>Named Parameters</h3>
  175. IN: <tt>weight_map(WeightMap w_map)</tt>
  176. <blockquote>
  177. The weight or ``length'' of each edge in the graph. The weights
  178. must all be non-negative, and the algorithm will throw a
  179. <a href="./exception.html#negative_edge"><tt>negative_edge</tt></a>
  180. exception is one of the edges is negative.
  181. The type <tt>WeightMap</tt> must be a model of
  182. <a href="../../property_map/doc/ReadablePropertyMap.html">Readable Property Map</a>. The edge descriptor type of
  183. the graph needs to be usable as the key type for the weight
  184. map. The value type for this map must be
  185. the same as the value type of the distance map.<br>
  186. <b>Default:</b> <tt>get(edge_weight, g)</tt><br>
  187. <b>Python</b>: Must be an <tt>edge_double_map</tt> for the graph.<br>
  188. <b>Python default</b>: <tt>graph.get_edge_double_map("weight")</tt>
  189. </blockquote>
  190. IN: <tt>vertex_index_map(VertexIndexMap i_map)</tt>
  191. <blockquote>
  192. This maps each vertex to an integer in the range <tt>[0,
  193. num_vertices(g))</tt>. This is necessary for efficient updates of the
  194. heap data structure&nbsp;[<A
  195. HREF="bibliography.html#driscoll88">61</A>] when an edge is relaxed.
  196. The type
  197. <tt>VertexIndexMap</tt> must be a model of
  198. <a href="../../property_map/doc/ReadablePropertyMap.html">Readable Property Map</a>. The value type of the map must be an
  199. integer type. The vertex descriptor type of the graph needs to be
  200. usable as the key type of the map.<br>
  201. <b>Default:</b> <tt>get(vertex_index, g)</tt>.
  202. Note: if you use this default, make sure your graph has
  203. an internal <tt>vertex_index</tt> property. For example,
  204. <tt>adjacency_list</tt> with <tt>VertexList=listS</tt> does
  205. not have an internal <tt>vertex_index</tt> property.
  206. <br>
  207. <b>Python</b>: Unsupported parameter.
  208. </blockquote>
  209. OUT: <tt>predecessor_map(PredecessorMap p_map)</tt>
  210. <blockquote>
  211. The predecessor map records the edges in the shortest path tree, the tree computed
  212. by the traversal of the graph. Upon completion of the algorithm, the edges
  213. <i>(p[u],u)</i> for all <i>u in V</i> are in the tree. The shortest path
  214. from vertex <i>s</i> to each vertex <i>v</i> in the graph consists of the
  215. vertices <i>v</i>, <i>p[v]</i>, <i>p[p[v]]</i>, and so on until <i>s</i> is
  216. reached, in reverse order. The
  217. tree is not guaranteed to be a minimum spanning tree. If <i>p[u] =
  218. u</i> then <i>u</i> is either the source vertex or a vertex that is
  219. not reachable from the source. The <tt>PredecessorMap</tt> type
  220. must be a <a
  221. href="../../property_map/doc/ReadWritePropertyMap.html">Read/Write
  222. Property Map</a> whose key and value types are the same as the vertex
  223. descriptor type of the graph.<br>
  224. <b>Default:</b> <tt>dummy_property_map</tt><br>
  225. <b>Python</b>: Must be a <tt>vertex_vertex_map</tt> for the graph.<br>
  226. </blockquote>
  227. UTIL/OUT: <tt>distance_map(DistanceMap d_map)</tt>
  228. <blockquote>
  229. The shortest path weight from the source vertex <tt>s</tt> to each
  230. vertex in the graph <tt>g</tt> is recorded in this property map. The
  231. shortest path weight is the sum of the edge weights along the
  232. shortest path. The type <tt>DistanceMap</tt> must be a model of <a
  233. href="../../property_map/doc/ReadWritePropertyMap.html">Read/Write
  234. Property Map</a>. The vertex descriptor type of the graph needs to
  235. be usable as the key type of the distance map.
  236. The value type of the distance map is the element type of a <a
  237. href="./Monoid.html">Monoid</a> formed with the <tt>combine</tt>
  238. function object and the <tt>zero</tt> object for the identity
  239. element. Also the distance value type must have a <a
  240. href="http://www.boost.org/sgi/stl/StrictWeakOrdering.html">
  241. StrictWeakOrdering</a> provided by the <tt>compare</tt> function
  242. object.<br>
  243. <b>Default:</b> <a
  244. href="../../property_map/doc/iterator_property_map.html">
  245. <tt>iterator_property_map</tt></a> created from a
  246. <tt>std::vector</tt> of the <tt>WeightMap</tt>'s value type of size
  247. <tt>num_vertices(g)</tt> and using the <tt>i_map</tt> for the index
  248. map.<br>
  249. <b>Python</b>: Must be a <tt>vertex_double_map</tt> for the graph.<br>
  250. </blockquote>
  251. IN: <tt>distance_compare(CompareFunction cmp)</tt>
  252. <blockquote>
  253. This function is use to compare distances to determine which vertex
  254. is closer to the source vertex. The <tt>CompareFunction</tt> type
  255. must be a model of <a
  256. href="http://www.boost.org/sgi/stl/BinaryPredicate.html">Binary
  257. Predicate</a> and have argument types that match the value type of
  258. the <tt>DistanceMap</tt> property map.<br>
  259. <b>Default:</b>
  260. <tt>std::less&lt;D&gt;</tt> with <tt>D=typename
  261. property_traits&lt;DistanceMap&gt;::value_type</tt><br>
  262. <b>Python</b>: Unsupported parameter.
  263. </blockquote>
  264. IN: <tt>distance_combine(CombineFunction cmb)</tt>
  265. <blockquote>
  266. This function is used to combine distances to compute the distance
  267. of a path. The <tt>CombineFunction</tt> type must be a model of <a
  268. href="http://www.boost.org/sgi/stl/BinaryFunction.html">Binary
  269. Function</a>. The first argument type of the binary function must
  270. match the value type of the <tt>DistanceMap</tt> property map and
  271. the second argument type must match the value type of the
  272. <tt>WeightMap</tt> property map. The result type must be the same
  273. type as the distance value type.<br>
  274. <b>Default:</b> <tt>std::plus&lt;D&gt;</tt> with
  275. <tt>D=typename property_traits&lt;DistanceMap&gt;::value_type</tt><br>
  276. <b>Python</b>: Unsupported parameter.
  277. </blockquote>
  278. IN: <tt>distance_inf(D inf)</tt>
  279. <blockquote>
  280. The <tt>inf</tt> object must be the greatest value of any <tt>D</tt> object.
  281. That is, <tt>compare(d, inf) == true</tt> for any <tt>d != inf</tt>.
  282. The type <tt>D</tt> is the value type of the <tt>DistanceMap</tt>. Edges
  283. are assumed to have a weight less than (using <tt>distance_compare</tt> for
  284. comparison) this value.<br>
  285. <b>Default:</b> <tt>std::numeric_limits&lt;D&gt;::max()</tt><br>
  286. <b>Python</b>: Unsupported parameter.
  287. </blockquote>
  288. IN: <tt>distance_zero(D zero)</tt>
  289. <blockquote>
  290. The <tt>zero</tt> value must be the identity element for the
  291. <a href="./Monoid.html">Monoid</a> formed by the distance values
  292. and the <tt>combine</tt> function object.
  293. The type <tt>D</tt> is the value type of the <tt>DistanceMap</tt>.<br>
  294. <b>Default:</b> <tt>D()</tt>with
  295. <tt>D=typename property_traits&lt;DistanceMap&gt;::value_type</tt><br>
  296. <b>Python</b>: Unsupported parameter.
  297. </blockquote>
  298. UTIL/OUT: <tt>color_map(ColorMap c_map)</tt>
  299. <blockquote>
  300. This is used during the execution of the algorithm to mark the
  301. vertices. The vertices start out white and become gray when they are
  302. inserted in the queue. They then turn black when they are removed
  303. from the queue. At the end of the algorithm, vertices reachable from
  304. the source vertex will have been colored black. All other vertices
  305. will still be white. The type <tt>ColorMap</tt> must be a model of
  306. <a href="../../property_map/doc/ReadWritePropertyMap.html">Read/Write
  307. Property Map</a>. A vertex descriptor must be usable as the key type
  308. of the map, and the value type of the map must be a model of
  309. <a href="./ColorValue.html">Color Value</a>.<br>
  310. <b>Default:</b> an <a
  311. href="../../property_map/doc/iterator_property_map.html">
  312. <tt>iterator_property_map</tt></a> created from a <tt>std::vector</tt>
  313. of <tt>default_color_type</tt> of size <tt>num_vertices(g)</tt> and
  314. using the <tt>i_map</tt> for the index map.<br>
  315. <b>Python</b>: The color map must be a <tt>vertex_color_map</tt> for
  316. the graph.
  317. </blockquote>
  318. OUT: <tt>visitor(DijkstraVisitor v)</tt>
  319. <blockquote>
  320. Use this to specify actions that you would like to happen
  321. during certain event points within the algorithm.
  322. The type <tt>DijkstraVisitor</tt> must be a model of the
  323. <a href="./DijkstraVisitor.html">Dijkstra Visitor</a> concept.
  324. The visitor object is passed by value <a
  325. href="#2">[2]</a>.<br>
  326. <b>Default:</b> <tt>dijkstra_visitor&lt;null_visitor&gt;</tt><br>
  327. <b>Python</b>: The parameter should be an object that derives from
  328. the <a
  329. href="DijkstraVisitor.html#python"><tt>DijkstraVisitor</tt></a> type
  330. of the graph.
  331. </blockquote>
  332. <H3>Complexity</H3>
  333. <P>
  334. The time complexity is <i>O(V log V + E)</i>.
  335. <h3>Visitor Event Points</h3>
  336. <ul>
  337. <li><b><tt>vis.initialize_vertex(u, g)</tt></b>
  338. is invoked on each vertex in the graph before the start of the
  339. algorithm.
  340. <li><b><tt>vis.examine_vertex(u, g)</tt></b>
  341. is invoked on a vertex as it is removed from the priority queue
  342. and added to set <i>S</i>. At this point we know that <i>(p[u],u)</i>
  343. is a shortest-paths tree edge so
  344. <i>d[u] = delta(s,u) = d[p[u]] + w(p[u],u)</i>. Also, the distances
  345. of the examined vertices is monotonically increasing
  346. <i>d[u<sub>1</sub>] <= d[u<sub>2</sub>] <= d[u<sub>n</sub>]</i>.
  347. <li><b><tt>vis.examine_edge(e, g)</tt></b>
  348. is invoked on each out-edge of a vertex immediately after it has
  349. been added to set <i>S</i>.
  350. <li><b><tt>vis.edge_relaxed(e, g)</tt></b>
  351. is invoked on edge <i>(u,v)</i> if <i>d[u] + w(u,v) < d[v]</i>.
  352. The edge <i>(u,v)</i> that participated in the last
  353. relaxation for vertex <i>v</i> is an edge in the shortest paths tree.
  354. <li><b><tt>vis.discover_vertex(v, g)</tt></b>
  355. is invoked on vertex <i>v</i> when the edge
  356. <i>(u,v)</i> is examined and <i>v</i> is WHITE. Since
  357. a vertex is colored GRAY when it is discovered,
  358. each reachable vertex is discovered exactly once. This
  359. is also when the vertex is inserted into the priority queue.
  360. <li><b><tt>vis.edge_not_relaxed(e, g)</tt></b>
  361. is invoked if the edge is not relaxed (see above).
  362. <li><b><tt>vis.finish_vertex(u, g)</tt></b>
  363. is invoked on a vertex after all of its out edges have
  364. been examined.
  365. </ul>
  366. <H3>Example</H3>
  367. <P>
  368. See <a href="../example/dijkstra-example.cpp">
  369. <TT>example/dijkstra-example.cpp</TT></a> for an example of using Dijkstra's
  370. algorithm.
  371. <H3>See also</H3> <a href="dijkstra_shortest_paths_no_color_map.html">dijkstra_shortest_paths_no_color_map</a> for a version of Dijkstra's shortest path that does not use a color map.
  372. <H3>Notes</H3>
  373. <a name="1">[1]</a>
  374. The algorithm used here saves a little space by not putting all <i>V -
  375. S</i> vertices in the priority queue at once, but instead only those
  376. vertices in <i>V - S</i> that are discovered and therefore have a
  377. distance less than infinity.
  378. <p><a name="2">[2]</a>
  379. Since the visitor parameter is passed by value, if your visitor
  380. contains state then any changes to the state during the algorithm
  381. will be made to a copy of the visitor object, not the visitor object
  382. passed in. Therefore you may want the visitor to hold this state by
  383. pointer or reference.
  384. <br>
  385. <HR>
  386. <TABLE>
  387. <TR valign=top>
  388. <TD nowrap>Copyright &copy; 2000-2001</TD><TD>
  389. <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>)
  390. </TD></TR></TABLE>
  391. </BODY>
  392. </HTML>