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