dijkstra_shortest_paths_no_color_map.html 17 KB

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