johnson_all_pairs_shortest.html 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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>Johnson All Pairs 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:johnson"></A>
  16. <TT>johnson_all_pairs_shortest_paths</TT>
  17. </H1>
  18. <PRE>
  19. <i>// named parameter version</i>
  20. template &lt;class VertexAndEdgeListGraph, class DistanceMatrix,
  21. class VertexID, class Weight, class BinaryPredicate,
  22. class BinaryFunction, class Infinity, class DistanceZero&gt;
  23. bool
  24. johnson_all_pairs_shortest_paths(VertexAndEdgeListGraph& g1,
  25. DistanceMatrix&amp; D,
  26. VertexID id1, Weight w1, const BinaryPredicate&amp; compare,
  27. const BinaryFunction&amp; combine, const Infinity&amp; inf,
  28. DistanceZero zero);
  29. template &lt;typename Graph, typename DistanceMatrix, typename P, typename T, typename R&gt;
  30. bool johnson_all_pairs_shortest_paths(Graph&amp; g, DistanceMatrix&amp; D,
  31. const bgl_named_params&lt;P, T, R&gt;&amp; params = <i>all defaults</i>)
  32. <i>// non-named parameter version</i>
  33. template &lt;typename Graph, typename DistanceMatrix,
  34. typename VertexIndex, typename WeightMap, typename DT&gt;
  35. bool
  36. johnson_all_pairs_shortest_paths(VertexAndEdgeListGraph&amp; g1,
  37. DistanceMatrix&amp; D,
  38. VertexIndex i_map, WeightMap w_map, DT zero)
  39. </PRE>
  40. <P>
  41. This algorithm finds the shortest distance between every pair of
  42. vertices in the graph. The algorithm returns false if there is a
  43. negative weight cycle in the graph and true otherwise. The distance
  44. between each pair of vertices is stored in the distance matrix
  45. <TT>D</TT>. This is one of the more time intensive graph algorithms,
  46. having a time complexity of <i>O(V E log V)</i>.
  47. <P>This algorithm should be used to compute shortest paths between
  48. every pair of vertices for sparse graphs. For dense graphs, use <a
  49. href="floyd_warshall_shortest.html"><code>floyd_warshall_all_pairs_shortest_paths</code></a>.
  50. <H3>Where Defined</H3>
  51. <P>
  52. <a href="../../../boost/graph/johnson_all_pairs_shortest.hpp"><TT>boost/graph/johnson_all_pairs_shortest.hpp</TT></a>
  53. <h3>Parameters</h3>
  54. IN: <tt>Graph&amp; g</tt>
  55. <blockquote>
  56. A directed or undirected graph. The graph type must be a model of
  57. <a href="VertexListGraph.html">Vertex List Graph</a>,
  58. <a href="EdgeListGraph.html">Edge List Graph</a>,
  59. and <a href="IncidenceGraph.html">Incidence Graph</a>.
  60. </blockquote>
  61. OUT: <tt>DistanceMatrix&amp; D</tt>
  62. <blockquote>
  63. The length of the shortest path between each pair of vertices
  64. <i>u,v</i> in the graph is stored in <tt>D[u][v]</tt>. The tuple of
  65. types (<tt>DistanceMatrix, vertices_size_type, D</tt>) must be a model
  66. of <a href="BasicMatrix.html">BasicMatrix</a> where <tt>D</tt> is the
  67. value type of the <tt>DistanceMap</tt>. There must be implicit conversions
  68. between the value type of the distance matrix and the value type of the weight
  69. map.
  70. </blockquote>
  71. <h3>Named Parameters</h3>
  72. IN: <tt>weight_map(WeightMap w_map)</tt>
  73. <blockquote>
  74. The weight or "length" of each edge in the graph.
  75. The type <tt>WeightMap</tt> must be a model of
  76. <a href="../../property_map/doc/ReadablePropertyMap.html">Readable Property Map</a>. The edge descriptor type of
  77. the graph needs to be usable as the key type for the weight
  78. map. The value type of the weight map must support a subtraction operation.<br>
  79. <b>Default:</b> <tt>get(edge_weight, g)</tt>
  80. </blockquote>
  81. UTIL: <tt>weight_map2(WeightMap2 w2_map)</tt>
  82. <blockquote>
  83. This parameter is no longer needed, and will be ignored.
  84. </blockquote>
  85. IN: <tt>vertex_index_map(VertexIndexMap i_map)</tt>
  86. <blockquote>
  87. This maps each vertex to an integer in the range <tt>[0,
  88. num_vertices(g))</tt>. This is necessary for efficient updates of the
  89. heap data structure in the internal call to Dijkstra's algorithm. The type
  90. <tt>VertexIndexMap</tt> must be a model of
  91. <a href="../../property_map/doc/ReadablePropertyMap.html">Readable Property Map</a>. The value type of the map must be an
  92. integer type. The vertex descriptor type of the graph needs to be
  93. usable as the key type of the map.<br>
  94. <b>Default:</b> <tt>get(vertex_index, g)</tt>
  95. Note: if you use this default, make sure your graph has
  96. an internal <tt>vertex_index</tt> property. For example,
  97. <tt>adjacency_list</tt> with <tt>VertexList=listS</tt> does
  98. not have an internal <tt>vertex_index</tt> property.
  99. <br>
  100. </blockquote>
  101. UTIL: <tt>distance_map(DistanceMap d_map)</tt>
  102. <blockquote>
  103. This parameter is no longer needed, and will be ignored.
  104. </blockquote>
  105. IN: <tt>distance_compare(CompareFunction cmp)</tt>
  106. <blockquote>
  107. This function is use to compare distances to determine
  108. which vertex is closer to the source vertex.
  109. The <tt>CompareFunction</tt> type must be a model of
  110. <a href="http://www.boost.org/sgi/stl/BinaryPredicate.html">Binary Predicate</a>
  111. and have argument types that
  112. match the value type of the <tt>WeightMap</tt> property map.<br>
  113. <b>Default:</b> <tt>std::less&lt;DT&gt;</tt> with
  114. <tt>DT=typename&nbsp;property_traits&lt;WeightMap&gt;::value_type</tt>
  115. </blockquote>
  116. IN: <tt>distance_combine(CombineFunction cmb)</tt>
  117. <blockquote>
  118. This function is used to combine distances to compute the distance
  119. of a path. The <tt>CombineFunction</tt> type must be a model of <a
  120. href="http://www.boost.org/sgi/stl/BinaryFunction.html">Binary
  121. Function</a>. Both argument types and the return type of the binary function
  122. must match the value type of the <tt>WeightMap</tt> property map. This operation is required to act as the sum operation for the weight type; in particular, it must be the inverse of the binary <tt>-</tt> operator on that type.<br>
  123. <b>Default:</b> <tt>std::plus&lt;DT&gt;</tt> with
  124. <tt>DT=typename&nbsp;property_traits&lt;WeightMap&gt;::value_type</tt>
  125. </blockquote>
  126. IN: <tt>distance_inf(DT inf)</tt>
  127. <blockquote>
  128. This value is used to initialize the distance for each
  129. vertex before the start of the algorithm.
  130. The type <tt>DT</tt> must be the value type of the <tt>WeightMap</tt>.<br>
  131. <b>Default:</b> <tt>std::numeric_limits::max()</tt>
  132. </blockquote>
  133. IN: <tt>distance_zero(DT zero)</tt>
  134. <blockquote>
  135. This value is used to initialize the distance for the source
  136. vertex before the start of the algorithm. The type <tt>DT</tt>
  137. must be the value type of the <tt>WeightMap</tt>.<br>
  138. <b>Default:</b> <tt>0</tt>
  139. </blockquote>
  140. UTIL/OUT: <tt>color_map(ColorMap c_map)</tt>
  141. <blockquote>
  142. This is used during the execution of the algorithm to mark the
  143. vertices. The vertices start out white and become gray when they are
  144. inserted in the queue. They then turn black when they are removed
  145. from the queue. At the end of the algorithm, vertices reachable from
  146. the source vertex will have been colored black. All other vertices
  147. will still be white. The type <tt>ColorMap</tt> must be a model of
  148. <a href="../../property_map/doc/ReadWritePropertyMap.html">Read/Write
  149. Property Map</a>. A vertex descriptor must be usable as the key type
  150. of the map, and the value type of the map must be a model of
  151. <a href="./ColorValue.html">Color Value</a>.<br>
  152. <b>Default:</b> an <a
  153. href="../../property_map/doc/iterator_property_map.html">
  154. <tt>iterator_property_map</tt></a> created from a <tt>std::vector</tt>
  155. of <tt>default_color_type</tt> of size <tt>num_vertices(g)</tt> and
  156. using the <tt>i_map</tt> for the index map.
  157. </blockquote>
  158. <h3>Complexity</h3>
  159. The time complexity is <i>O(V E log V)</i>.
  160. <H3>Example</H3>
  161. <P>
  162. The file <a
  163. href="../example/johnson-eg.cpp"><tt>example/johnson-eg.cpp</tt></a> applies
  164. Johnson's algorithm for all-pairs shortest paths to the example graph
  165. from page 568 of the CLR&nbsp;[<A
  166. HREF="bibliography.html#clr90">8</A>].
  167. <br>
  168. <HR>
  169. <TABLE>
  170. <TR valign=top>
  171. <TD nowrap>Copyright &copy; 2000-2001</TD><TD>
  172. <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>)
  173. </TD></TR></TABLE>
  174. </BODY>
  175. </HTML>