floyd_warshall_shortest.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <HTML>
  2. <!--
  3. Copyright (c) 2002 Rensselaer Polytechnic Institute
  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>Floyd-Warshall 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:floyd-warshall">
  16. <TT>floyd_warshall_all_pairs_shortest_paths</TT>
  17. </H1>
  18. <PRE><em>// Named parameters version</em>
  19. template &lt;class VertexListGraph, class DistanceMatrix,
  20. class P, class T, class R&gt;
  21. bool floyd_warshall_initialized_all_pairs_shortest_paths(
  22. const VertexListGraph&amp; g, DistanceMatrix&amp; d,
  23. const bgl_named_params&lt;P, T, R&gt;&amp; params)
  24. template &lt;class VertexAndEdgeListGraph, class DistanceMatrix,
  25. class P, class T, class R&gt;
  26. bool floyd_warshall_all_pairs_shortest_paths(
  27. const VertexAndEdgeListGraph&amp; g, DistanceMatrix&amp; d,
  28. const bgl_named_params&lt;P, T, R&gt;&amp; params)
  29. <em>// Positional parameter versions</em>
  30. \begin{verbatim}
  31. template &lt;typename VertexListGraph, typename DistanceMatrix,
  32. typename BinaryPredicate, typename BinaryFunction,
  33. typename Infinity, typename Zero&gt;
  34. bool floyd_warshall_initialized_all_pairs_shortest_paths(
  35. const VertexListGraph&amp; g, DistanceMatrix&amp; d,
  36. const BinaryPredicate&amp; compare, const BinaryFunction&amp; combine,
  37. const Infinity&amp; inf, const Zero&amp; zero)
  38. template &lt;typename VertexAndEdgeListGraph, typename DistanceMatrix,
  39. typename WeightMap, typename BinaryPredicate,
  40. typename BinaryFunction, typename Infinity, typename Zero&gt;
  41. bool floyd_warshall_all_pairs_shortest_paths(
  42. const VertexAndEdgeListGraph&amp; g, DistanceMatrix&amp; d,
  43. const WeightMap&amp; w, const BinaryPredicate&amp; compare,
  44. const BinaryFunction&amp; combine,
  45. const Infinity&amp; inf, const Zero&amp; zero)</PRE>
  46. <P>
  47. These algorithms find the shortest distance between every pair of
  48. vertices in the graph. The algorithms return false if there is a
  49. negative weight cycle in the graph, true otherwise. The shortest
  50. distance between each pair of vertices is stored in the distance
  51. matrix <code>d</code>. The difference between the two algorithms is in
  52. whether the distance matrix is assumed to be initialized or not, as
  53. discussed below under the OUT parameter description.
  54. <P>This algorithm should be used to compute shortest paths between
  55. every pair of vertices for dense graphs. For sparse graphs, use <a
  56. href="johnson_all_pairs_shortest.html"><code>johnson_all_pairs_shortest_paths</code></a>.
  57. <H3>Where Defined</H3>
  58. <P>
  59. <a href="../../../boost/graph/floyd_warshall_shortest.hpp"><TT>boost/graph/floyd_warshall_shortest.hpp</TT></a>
  60. <h3>Parameters</h3>
  61. IN: <code>Graph&amp; g</code>
  62. <blockquote>
  63. A directed or undirected graph. The graph must be a model of <a href="VertexListGraph.html">Vertex List Graph</a> for calls to
  64. <code>floyd_warshall_initialized_all_pairs_shortest_paths</code>, and
  65. <a href="VertexAndEdgeListGraph.html">Vertex And Edge List Graph</a> for calls to
  66. <code>floyd_warshall_all_pairs_shortest_paths</code>.<br>
  67. </blockquote>
  68. OUT: <code>DistanceMatrix&amp; d</code>
  69. <blockquote>
  70. The length of the shortest path between each pair of vertices
  71. <code>u</code>,<code>v</code> are
  72. stored in the matrix at location <code>D[u][v]</code>. The
  73. DistanceMatrix must be
  74. of type <code>{M, I, V}</code> where <code>I</code> is of type
  75. <code>vertex_descriptor</code> and <code>V</code> is the
  76. type of the <code>weight_map</code>. The set of types must be a model of
  77. <a href="BasicMatrix.html">BasicMatrix</a>, with the exceptions that
  78. it isn't required to
  79. run in constant time, and it must be mutable. The matrix must be
  80. properly initialized when it is passed to the function
  81. <code>floyd_warshall_initialized_all_pairs_shortest_paths</code>. If the
  82. function <code>floyd_warshall_all_pairs_shortest_paths</code> is used then the
  83. matrix will be initialized for the user.
  84. </blockquote>
  85. <h3>Named Parameters</h3>
  86. IN: <code>weight_map(WeightMap w)</code>
  87. <blockquote>
  88. The weight of length of each edge in the graph. The <code>WeightMap</code>
  89. must be a model of <a href="../../property_map/doc/ReadablePropertyMap.html">Readable Property
  90. Map</a>. The edge descriptor
  91. type of the graph needs to be usable as the key type for the weight
  92. map. The <code>value_type</code> of the weight map must be the type of the
  93. <code>DistanceMatrix</code>, and must always either be part of the
  94. graph passed to the function, or passed in as a parameter.<br>
  95. <b>Default:</b> <code>get(edge_weight, g)</code>
  96. </blockquote>
  97. IN: <code>distance_compare(CompareFunction cmp)</code>
  98. <blockquote>
  99. The function used to compare distances to determine which target
  100. vertex is closer to the source vertex. The CompareFunction must be a
  101. model of <code>BinaryPredicate</code>. The argument types must match the
  102. value type of the <code>WeightMap</code>.<br>
  103. <b>Default:</b> <code>std::less&lt;WM&gt;</code>with <code>WM = typename property_traits&lt;WeightMap&gt;::value_type</code>
  104. </blockquote>
  105. IN: <code>distance_combine(CombineFunction cmb)</code>
  106. <blockquote>
  107. The function used to combine distance to compute the distance of a
  108. path. The CombineFunction must be a model of <code>BinaryFunction</code>.
  109. The argument types must match the value type of the <code>WeightMap</code>.
  110. The result type must be the same as the distance value type.<br>
  111. <b>Default:</b> <code>boost::closed_plus&lt;WM&gt;</code> with <code>WM = typename property_traits&lt;WeightMap&gt;::value_type</code>
  112. </blockquote>
  113. IN: <code>distance_inf(WM inf)</code>
  114. <blockquote>
  115. The value used to initialize the distance for each vertex before
  116. starting the algorithm, and to represent the distance between vertices
  117. for which there is no path. Should be larger than any possible valid
  118. path length. The argument type must match the value type of the <code>
  119. WeightMap</code>.<br>
  120. <b>Default:</b> <code>std::numeric_limits&lt;WM&gt;::max()</code> with <code>WM = typename property_traits&lt;WeightMap&gt;::value_type</code>
  121. </blockquote>
  122. IN: <code>distance_zero(WM zero)</code>
  123. <blockquote>
  124. The value used to represent the distance from a vertex to itself, and
  125. to determine if a value is negative. The argument type must match the
  126. value type of the <code>WeightMap</code>.<br>
  127. <b>Default:</b> <code>0</code>
  128. </blockquote>
  129. <h3>Complexity</h3>
  130. The time complexity is <i>O(V<sup>3</sup>)</i>.
  131. <br>
  132. <HR>
  133. <TABLE>
  134. <TR valign=top>
  135. <TD nowrap>Copyright &copy; 2002-2004</TD><TD>
  136. Lauren Foutz, Rensselaer Polytechnic Institute</td>
  137. </tr><tr valign="top"><td></td>
  138. <td>Scott Hill, Rensselaer Polytechnic Institute
  139. </TD></TR></TABLE>
  140. </BODY>
  141. </HTML>