dag_shortest_paths.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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: Directed Acyclic Graph 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:dag_shortest_paths"></A>
  16. <img src="figs/python.gif" alt="(Python)"/>
  17. <TT>dag_shortest_paths</TT>
  18. </H1>
  19. <P>
  20. <PRE>
  21. <i>// named paramter version</i>
  22. template &lt;class VertexListGraph, class Param, class Tag, class Rest&gt;
  23. void dag_shortest_paths(const VertexListGraph&amp; g,
  24. typename graph_traits&lt;VertexListGraph&gt;::vertex_descriptor s,
  25. const bgl_named_params&lt;Param,Tag,Rest&gt;&amp; params)
  26. <i>// non-named parameter version</i>
  27. template &lt;class VertexListGraph, class DijkstraVisitor,
  28. class DistanceMap, class WeightMap, class ColorMap,
  29. class PredecessorMap,
  30. class Compare, class Combine,
  31. class DistInf, class DistZero&gt;
  32. void dag_shortest_paths(const VertexListGraph&amp; g,
  33. typename graph_traits&lt;VertexListGraph&gt;::vertex_descriptor s,
  34. DistanceMap distance, WeightMap weight, ColorMap color,
  35. PredecessorMap pred, DijkstraVisitor vis,
  36. Compare compare, Combine combine, DistInf inf, DistZero zero)
  37. </PRE>
  38. <P>
  39. This algorithm&nbsp;[<A HREF="bibliography.html#clr90">8</A>] solves
  40. the single-source shortest-paths problem on a weighted, directed
  41. acyclic graph (DAG). This algorithm is more efficient for DAG's
  42. than either the Dijkstra or Bellman-Ford algorithm.
  43. Use breadth-first search instead of this algorithm
  44. when all edge weights are equal to one. For the definition of the
  45. shortest-path problem see Section <A
  46. HREF="graph_theory_review.html#sec:shortest-paths-algorithms">Shortest-Paths
  47. Algorithms</A> for some background to the shortest-path problem.
  48. </P>
  49. <P>
  50. There are two main options for obtaining output from the
  51. <tt>dag_shortest_paths()</tt> function. If you provide a
  52. distance property map through the <tt>distance_map()</tt> parameter
  53. then the shortest distance from the source vertex to every other
  54. vertex in the graph will be recorded in the distance map. Also you can
  55. record the shortest paths tree in a predecessor map: for each vertex
  56. <i>u in V</i>, <i>p[u]</i> will be the predecessor of <i>u</i> in
  57. the shortest paths tree (unless <i>p[u] = u</i>, in which case <i>u</i> is
  58. either the source or a vertex unreachable from the source). In
  59. addition to these two options, the user can provide there own
  60. custom-made visitor that can takes actions during any of the
  61. algorithm's event points.</P>
  62. <h3>Where Defined</h3>
  63. <a href="../../../boost/graph/dag_shortest_paths.hpp"><tt>boost/graph/dag_shortest_paths.hpp</tt></a>
  64. <h3>Parameters</h3>
  65. IN: <tt>const VertexListGraph&amp; g</tt>
  66. <blockquote>
  67. The graph object on which the algorithm will be applied.
  68. The type <tt>VertexListGraph</tt> must be a model of \concept{VertexListGraph}.<br>
  69. <b>Python</b>: The parameter is named <tt>graph</tt>.
  70. </blockquote>
  71. IN: <tt>vertex_descriptor s</tt>
  72. <blockquote>
  73. The source vertex. All distance will be calculated from this vertex,
  74. and the shortest paths tree will be rooted at this vertex.<br>
  75. <b>Python</b>: The parameter is named <tt>root_vertex</tt>.
  76. </blockquote>
  77. <h3>Named Parameters</h3>
  78. IN: <tt>weight_map(WeightMap w_map)</tt>
  79. <blockquote>
  80. The weight or ``length'' of each edge in the graph.
  81. The type <tt>WeightMap</tt> must be a model of
  82. <a href="../../property_map/doc/ReadablePropertyMap.html">Readable Property Map</a>. The edge descriptor type of
  83. the graph needs to be usable as the key type for the weight
  84. map. The value type for the map must be
  85. <i>Addable</i> with the value type of the distance map.<br>
  86. <b>Default:</b> <tt>get(edge_weight, g)</tt><br>
  87. <b>Python</b>: Must be an <tt>edge_double_map</tt> for the graph.<br>
  88. <b>Python default</b>: <tt>graph.get_edge_double_map("weight")</tt>
  89. </blockquote>
  90. IN: <tt>vertex_index_map(VertexIndexMap i_map)</tt>
  91. <blockquote>
  92. This maps each vertex to an integer in the range <tt>[0,
  93. num_vertices(g))</tt>. This is necessary for efficient updates of the
  94. heap data structure when an edge is relaxed. The type
  95. <tt>VertexIndexMap</tt> must be a model of
  96. <a href="../../property_map/doc/ReadablePropertyMap.html">Readable Property Map</a>. The value type of the map must be an
  97. integer type. The vertex descriptor type of the graph needs to be
  98. usable as the key type of the map.<br>
  99. <b>Default:</b> <tt>get(vertex_index, g)</tt>.
  100. Note: if you use this default, make sure your graph has
  101. an internal <tt>vertex_index</tt> property. For example,
  102. <tt>adjacency_list</tt> with <tt>VertexList=listS</tt> does
  103. not have an internal <tt>vertex_index</tt> property.<br>
  104. <b>Python</b>: Unsupported parameter.
  105. </blockquote>
  106. OUT: <tt>predecessor_map(PredecessorMap p_map)</tt>
  107. <blockquote>
  108. The predecessor map records the edges in the minimum spanning
  109. tree. Upon completion of the algorithm, the edges <i>(p[u],u)</i>
  110. for all <i>u in V</i> are in the minimum spanning tree. If <i>p[u] =
  111. u</i> then <i>u</i> is either the source vertex or a vertex that is
  112. not reachable from the source. The <tt>PredecessorMap</tt> type
  113. must be a <a
  114. href="../../property_map/doc/ReadWritePropertyMap.html">Read/Write
  115. Property Map</a> which key and vertex types the same as the vertex
  116. descriptor type of the graph.<br>
  117. <b>Default:</b> <tt>dummy_property_map</tt><br>
  118. <b>Python</b>: Must be a <tt>vertex_vertex_map</tt> for the graph.<br>
  119. </blockquote>
  120. UTIL/OUT: <tt>distance_map(DistanceMap d_map)</tt>
  121. <blockquote>
  122. The shortest path weight from the source vertex <tt>s</tt> to each
  123. vertex in the graph <tt>g</tt> is recorded in this property map. The
  124. shortest path weight is the sum of the edge weights along the
  125. shortest path. The type <tt>DistanceMap</tt> must be a model of <a
  126. href="../../property_map/doc/ReadWritePropertyMap.html">Read/Write
  127. Property Map</a>. The vertex descriptor type of the graph needs to
  128. be usable as the key type of the distance map.
  129. The value type of the distance map is the element type of a <a
  130. href="./Monoid.html">Monoid</tt> formed with the <tt>combine</tt>
  131. function object and the <tt>zero</tt> object for the identity
  132. element. Also the distance value type must have a <a
  133. href="http://www.boost.org/sgi/stl/StrictWeakOrdering.html">
  134. StrictWeakOrdering</a> provided by the <tt>compare</tt> function
  135. object.<br>
  136. <b>Default:</b> <a
  137. href="../../property_map/doc/iterator_property_map.html">
  138. <tt>iterator_property_map</tt></a> created from a
  139. <tt>std::vector</tt> of the <tt>WeightMap</tt>'s value type of size
  140. <tt>num_vertices(g)</tt> and using the <tt>i_map</tt> for the index
  141. map.<br>
  142. <b>Python</b>: Must be a <tt>vertex_double_map</tt> for the graph.
  143. </blockquote>
  144. IN: <tt>distance_compare(CompareFunction cmp)</tt>
  145. <blockquote>
  146. This function is use to compare distances to determine which vertex
  147. is closer to the source vertex. The <tt>CompareFunction</tt> type
  148. must be a model of <a
  149. href="http://www.boost.org/sgi/stl/BinaryPredicate.html">Binary
  150. Predicate</a> and have argument types that match the value type of
  151. the <tt>DistanceMap</tt> property map.<br>
  152. <b>Default:</b>
  153. <tt>std::less&lt;D&gt;</tt> with <tt>D=typename
  154. property_traits&lt;DistanceMap&gt;::value_type</tt><br>
  155. <b>Python</b>: Unsupported parameter.
  156. </blockquote>
  157. IN: <tt>distance_combine(CombineFunction cmb)</tt>
  158. <blockquote>
  159. This function is used to combine distances to compute the distance
  160. of a path. The <tt>CombineFunction</tt> type must be a model of <a
  161. href="http://www.boost.org/sgi/stl/BinaryFunction.html">Binary
  162. Function</a>. The first argument type of the binary function must
  163. match the value type of the <tt>DistanceMap</tt> property map and
  164. the second argument type must match the value type of the
  165. <tt>WeightMap</tt> property map. The result type must be the same
  166. type as the distance value type.<br>
  167. <b>Default:</b> <tt>std::plus&lt;D&gt;</tt> with
  168. <tt>D=typename property_traits&lt;DistanceMap&gt;::value_type</tt><br>
  169. <b>Python</b>: Unsupported parameter.
  170. </blockquote>
  171. IN: <tt>distance_inf(D inf)</tt>
  172. <blockquote>
  173. The <tt>inf</tt> object must be the greatest value of any <tt>D</tt> object.
  174. That is, <tt>compare(d, inf) == true</tt> for any <tt>d != inf</tt>.
  175. The type <tt>D</tt> is the value type of the <tt>DistanceMap</tt>.<br>
  176. <b>Default:</b> <tt>std::numeric_limits&lt;D&gt;::max()</tt><br>
  177. <b>Python</b>: Unsupported parameter.
  178. </blockquote>
  179. IN: <tt>distance_zero(D zero)</tt>
  180. <blockquote>
  181. The <tt>zero</tt> value must be the identity element for the
  182. <a href="./Monoid.html">Monoid</a> formed by the distance values
  183. and the <tt>combine</tt> function object.
  184. The type \code{D} is the value type of the \code{DistanceMap}
  185. <b>Default:</b> <tt>D()</tt><br>
  186. <b>Python</b>: Unsupported parameter.
  187. </blockquote>
  188. UTIL/OUT: <tt>color_map(ColorMap c_map)</tt>
  189. <blockquote>
  190. This is used during the execution of the algorithm to mark the
  191. vertices. The vertices start out white and become gray when they are
  192. inserted in the queue. They then turn black when they are removed
  193. from the queue. At the end of the algorithm, vertices reachable from
  194. the source vertex will have been colored black. All other vertices
  195. will still be white. The type <tt>ColorMap</tt> must be a model of
  196. <a href="../../property_map/doc/ReadWritePropertyMap.html">Read/Write
  197. Property Map</a>. A vertex descriptor must be usable as the key type
  198. of the map, and the value type of the map must be a model of
  199. <a href="./ColorValue.html">Color Value</a>.<br>
  200. <b>Default:</b> an <a
  201. href="../../property_map/doc/iterator_property_map.html">
  202. <tt>iterator_property_map</tt></a> created from a <tt>std::vector</tt>
  203. of <tt>default_color_type</tt> of size <tt>num_vertices(g)</tt> and
  204. using the <tt>i_map</tt> for the index map.<br>
  205. <b>Python</b>: The color map must be a <tt>vertex_color_map</tt> for
  206. the graph.
  207. </blockquote>
  208. OUT: <tt>visitor(DijkstraVisitor v)</tt>
  209. <blockquote>
  210. Use this to specify actions that you would like to happen
  211. during certain event points within the algorithm.
  212. The type <tt>DijkstraVisitor</tt> must be a model of the
  213. <a href="./DijkstraVisitor.html">Dijkstra Visitor</a> concept.
  214. The visitor object is passed by value <a
  215. href="#1">[1]</a>.<br>
  216. <b>Default:</b> <tt>dijkstra_visitor&lt;null_visitor&gt;</tt><br>
  217. <b>Python</b>: The parameter should be an object that derives from
  218. the <a
  219. href="DijkstraVisitor.html#python"><tt>DijkstraVisitor</tt></a> type
  220. of the graph.
  221. </blockquote>
  222. <H3>Complexity</H3>
  223. <P>
  224. The time complexity is <i>O(V + E)</i>.
  225. <h3>Visitor Event Points</h3>
  226. <ul>
  227. <li><b><tt>vis.initialize_vertex(u, g)</tt></b>
  228. is invoked on each vertex in the graph before the start of the
  229. algorithm.
  230. <li><b><tt>vis.examine_vertex(u, g)</tt></b>
  231. is invoked on a vertex as it is added to set <i>S</i>.
  232. At this point we know that <i>(p[u],u)</i>
  233. is a shortest-paths tree edge so
  234. <i>d[u] = delta(s,u) = d[p[u]] + w(p[u],u)</i>. Also, the distances
  235. of the examined vertices is monotonically increasing
  236. <i>d[u<sub>1</sub>] <= d[u<sub>2</sub>] <= d[u<sub>n</sub>]</i>.
  237. <li><b><tt>vis.examine_edge(e, g)</tt></b>
  238. is invoked on each out-edge of a vertex immediately after it has
  239. been added to set <i>S</i>.
  240. <li><b><tt>vis.edge_relaxed(e, g)</tt></b>
  241. is invoked on edge <i>(u,v)</i> if <i>d[u] + w(u,v) < d[v]</i>.
  242. The edge <i>(u,v)</i> that participated in the last
  243. relaxation for vertex <i>v</i> is an edge in the shortest paths tree.
  244. <li><b><tt>vis.discover_vertex(v, g)</tt></b>
  245. is invoked on vertex <i>v</i> when the edge
  246. <i>(u,v)</i> is examined and <i>v</i> is WHITE. Since
  247. a vertex is colored GRAY when it is discovered,
  248. each reacable vertex is discovered exactly once.
  249. <li><b><tt>vis.edge_not_relaxed(e, g)</tt></b>
  250. is invoked if the edge is not relaxed (see above).
  251. <li><b><tt>vis.finish_vertex(u, g)</tt></b>
  252. is invoked on a vertex after all of its out edges have
  253. been examined.
  254. </ul>
  255. <H3>Example</H3>
  256. <P>
  257. See <a href="../example/dag_shortest_paths.cpp">
  258. <TT>example/dag_shortest_paths.cpp</TT></a> for an example of using this
  259. algorithm.
  260. <H3>Notes</H3>
  261. <p><a name="1">[1]</a>
  262. Since the visitor parameter is passed by value, if your visitor
  263. contains state then any changes to the state during the algorithm
  264. will be made to a copy of the visitor object, not the visitor object
  265. passed in. Therefore you may want the visitor to hold this state by
  266. pointer or reference.
  267. <br>
  268. <HR>
  269. <TABLE>
  270. <TR valign=top>
  271. <TD nowrap>Copyright &copy; 2000-2001</TD><TD>
  272. <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>)
  273. </TD></TR></TABLE>
  274. </BODY>
  275. </HTML>