File: johnson_all_pairs_shortest.html

package info (click to toggle)
boost1.35 1.35.0-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 203,856 kB
  • ctags: 337,867
  • sloc: cpp: 938,683; xml: 56,847; ansic: 41,589; python: 18,999; sh: 11,566; makefile: 664; perl: 494; yacc: 456; asm: 353; csh: 6
file content (212 lines) | stat: -rw-r--r-- 8,112 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<HTML>
<!--
  -- Copyright (c) Jeremy Siek 2000
  --
  -- Distributed under the Boost Software License, Version 1.0.
  -- (See accompanying file LICENSE_1_0.txt or copy at
  -- http://www.boost.org/LICENSE_1_0.txt)
  -->
<Head>
<Title>Johnson All Pairs Shortest Paths</Title>
<BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b" 
        ALINK="#ff0000"> 
<IMG SRC="../../../boost.png" 
     ALT="C++ Boost" width="277" height="86"> 

<BR Clear>

<H1><A NAME="sec:johnson">
<TT>johnson_all_pairs_shortest_paths</TT>
</H1>

<PRE>
<i>// named parameter version</i>
  template &lt;class VertexAndEdgeListGraph, class DistanceMatrix,
            class VertexID, class Weight, class BinaryPredicate, 
            class BinaryFunction, class Infinity, class DistanceZero&gt;
  bool
  johnson_all_pairs_shortest_paths(VertexAndEdgeListGraph& g1, 
               DistanceMatrix&amp; D,
               VertexID id1, Weight w1, const BinaryPredicate&amp; compare, 
               const BinaryFunction&amp; combine, const Infinity&amp; inf,
               DistanceZero zero);

template &lt;typename Graph, typename DistanceMatrix, typename P, typename T, typename R&gt;
bool johnson_all_pairs_shortest_paths(Graph&amp; g, DistanceMatrix&amp; D,
  const bgl_named_params&lt;P, T, R&gt;&amp; params = <i>all defaults</i>)

<i>// non-named parameter version</i>
template &lt;typename Graph, typename DistanceMatrix,
          typename VertexIndex, typename WeightMap, typename DT&gt;
bool
johnson_all_pairs_shortest_paths(VertexAndEdgeListGraph&amp; g1, 
  DistanceMatrix&amp; D,
  VertexIndex i_map, WeightMap w_map, DT zero)
</PRE>

<P>
This algorithm finds the shortest distance between every pair of
vertices in the graph. The algorithm returns false if there is a
negative weight cycle in the graph and true otherwise. The distance
between each pair of vertices is stored in the distance matrix
<TT>D</TT>. This is one of the more time intensive graph algorithms,
having a time complexity of <i>O(V E log V)</i>.

<P>This algorithm should be used to compute shortest paths between
every pair of vertices for sparse graphs. For dense graphs, use <a
href="floyd_warshall_shortest.html"><code>floyd_warshall_all_pairs_shortest_paths</code></a>.

<H3>Where Defined</H3>

<P>
<a href="../../../boost/graph/johnson_all_pairs_shortest.hpp"><TT>boost/graph/johnson_all_pairs_shortest.hpp</TT></a>


<h3>Parameters</h3>

IN: <tt>Graph&amp; g</tt>
<blockquote>
A directed or undirected graph. The graph type must be a model of
<a href="VertexListGraph.html">Vertex List Graph</a>,
<a href="EdgeListGraph.html">Edge List Graph</a>,
and <a href="IncidenceGraph.html">Incidence Graph</a>.
</blockquote>

OUT: <tt>DistanceMatrix&amp; D</tt>
<blockquote>
The length of the shortest path between each pair of vertices
<i>u,v</i> in the graph is stored in <tt>D[u][v]</tt>. The set of
types {<tt>DistanceMatrix, vertices_size_type, D</tt>} must be a model
of <a href="BasicMatrix.html">BasicMatrix</a> where <tt>D</tt> is the
value type of the <tt>DistanceMap</tt>.
</blockquote>


<h3>Named Parameters</h3>

IN: <tt>weight_map(WeightMap w_map)</tt>   
<blockquote>
  The weight or ``length'' of each edge in the graph.
  The type <tt>WeightMap</tt> must be a model of
  <a href="../../property_map/ReadablePropertyMap.html">Readable Property Map</a>. The edge descriptor type of
  the graph needs to be usable as the key type for the weight
  map. The value type for the map must be
  <i>Addable</i> with the value type of the distance map.<br>
  <b>Default:</b>  <tt>get(edge_weight, g)</tt>
</blockquote>

UTIL: <tt>weight_map2(WeightMap2 w2_map)</tt>   
<blockquote>
  This parameter is no longer needed, and will be ignored.
</blockquote>

IN: <tt>vertex_index_map(VertexIndexMap i_map)</tt> 
<blockquote>
  This maps each vertex to an integer in the range <tt>[0,
    num_vertices(g))</tt>. This is necessary for efficient updates of the
  heap data structure in the internal call to Dijkstra's algorithm.  The type
  <tt>VertexIndexMap</tt> must be a model of
  <a href="../../property_map/ReadablePropertyMap.html">Readable Property Map</a>. The value type of the map must be an
  integer type. The vertex descriptor type of the graph needs to be
  usable as the key type of the map.<br>
  <b>Default:</b> <tt>get(vertex_index, g)</tt>
    Note: if you use this default, make sure your graph has
    an internal <tt>vertex_index</tt> property. For example,
    <tt>adjacenty_list</tt> with <tt>VertexList=listS</tt> does
    not have an internal <tt>vertex_index</tt> property.
   <br>
</blockquote>


UTIL: <tt>distance_map(DistanceMap d_map)</tt> 
<blockquote>
  This parameter is no longer needed, and will be ignored.
</blockquote>

IN: <tt>distance_compare(CompareFunction cmp)</tt> 
<blockquote>
  This function is use to compare distances to determine
  which vertex is closer to the source vertex.
  The <tt>CompareFunction</tt> type must be a model of
  \stlconcept{BinaryPredicate} and have argument types that
  match the value type of the <tt>WeightMap</tt> property map.<br>
  <b>Default:</b> <tt>std::less&lt;DT&gt;</tt> with
  <tt>DT=typename&nbsp;property_traits&lt;WeightMap&gt;::value_type</tt>
</blockquote>

IN: <tt>distance_combine(CombineFunction cmb)</tt> 
<blockquote>
  This function is used to combine distances to compute the distance
  of a path. The <tt>CombineFunction</tt> type must be a model of <a
  href="http://www.sgi.com/tech/stl/BinaryFunction.html">Binary
  Function</a>. The first argument type of the binary function must
  match the value type of the <tt>DistanceMap</tt> property map and
  the second argument type must match the value type of the
  <tt>WeightMap</tt> property map.  The result type must be the same
  type as the distance value type.<br>
  <b>Default:</b> <tt>std::plus&lt;DT&gt;</tt> with
   <tt>DT=typename&nbsp;property_traits&lt;WeightMap&gt;::value_type</tt>
</blockquote>

IN: <tt>distance_inf(DT inf)</tt> 
<blockquote>
  This value is used to initialize the distance for each
  vertex before the start of the algorithm. 
  The type <tt>DT</tt> must be the value type of the <tt>WeigthMap</tt>.<br>
  <b>Default:</b> <tt>std::numeric_limits::max()</tt>
</blockquote>

IN: <tt>distance_zero(DT zero)</tt> 
<blockquote>
  This value is used to initialize the distance for the source
  vertex before the start of the algorithm.  The type <tt>DT</tt>
  must be the value type of the <tt>WeigthMap</tt>.<br>
  <b>Default:</b> <tt>0</tt>
</blockquote>

UTIL/OUT: <tt>color_map(ColorMap c_map)</tt> 
<blockquote>
  This is used during the execution of the algorithm to mark the
  vertices. The vertices start out white and become gray when they are
  inserted in the queue. They then turn black when they are removed
  from the queue. At the end of the algorithm, vertices reachable from
  the source vertex will have been colored black. All other vertices
  will still be white. The type <tt>ColorMap</tt> must be a model of
  <a href="../../property_map/ReadWritePropertyMap.html">Read/Write
  Property Map</a>. A vertex descriptor must be usable as the key type
  of the map, and the value type of the map must be a model of
  <a href="./ColorValue.html">Color Value</a>.<br>
  <b>Default:</b> an <a
  href="../../property_map/iterator_property_map.html">
  <tt>iterator_property_map</tt></a> created from a <tt>std::vector</tt>
  of <tt>default_color_type</tt> of size <tt>num_vertices(g)</tt> and
  using the <tt>i_map</tt> for the index map.
</blockquote>


<h3>Complexity</h3>

The time complexity is <i>O(V E log V)</i>.



<H3>Example</H3>

<P>
The file <a
href="../example/johnson-eg.cpp"><tt>example/johnson-eg.cpp</tt></a> applies
Johnson's algorithm for all-pairs shortest paths to the example graph
from page 568 of the CLR&nbsp;[<A
HREF="bibliography.html#clr90">8</A>].


<br>
<HR>
<TABLE>
<TR valign=top>
<TD nowrap>Copyright &copy 2000-2001</TD><TD>
<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>)
</TD></TR></TABLE>

</BODY>
</HTML>