Académique Documents
Professionnel Documents
Culture Documents
Andrew V. Goldberg
Microsoft Research
Silicon Valley
http://research.microsoft.com/users/goldberg
Einstein Quote
Variants
Point to point, single source, all pairs.
Nonnegative and arbitrary arc lengths.
Integer lengths, word RAM model.
Static, dynamic graphs, dynamic (arrival-dependent) lengths.
Directed and undirected.
Unless mentioned otherwise, study directed graphs.
Nonnegative len. undirected problem = symmetric directed.
General undirected problem complicated (matching).
SSSP Problem
Single-Source Shortest Paths (SSSP) problem:
Input: Digraph G = (V, A), : A R, source s V .
Goal: Find shortest paths and distances from s to all vertices.
Special case: Nonnegative lengths (NSSSP).
W.l.g. assume all vertices reachable from s.
(In linear time can find unreachable vertices.)
One of the most fundamental problems:
A point-to-point problem is no harder.
n SSSP problems give all pairs problem.
(In fact, one SSSP and n 1 NSSSPs.)
DIKU Summer School on Shortest Paths
Time Bounds
Bounds/currently best for some parameters
year
1955
1958
bound
O(n4)
O(nm)
1983
1989
1993
2005
3/4m log U )
O(n
O(nm log(nU ))
O( nm log N )
wU )
O(n
due to
Shimbel
Bellman,
Ford,
Moore
Gabow
Gabow & Tarjan
Goldberg
Sankowski
Yuster & Zwick
comments
n = |V |
m = |A|
int. in [U, U ]
Will
int. in [N, )
w 2.38 (matrix
mult. exp.)
Structural results.
Scanning method.
Negative cycle detection.
Bellman-Ford-Moore (BFM) algorithm.
Practical relatives of BFM.
The scaling algorithm.
Potential Transformation
Replace by d.
Lemma (reduced cost of a path): For P = (v1, . . . , vk ),
d(P ) = (P ) + d(v1) d(vk ).
Proof: Recall d(vi, vi+1) = (vi, vi+1) + d(vi) d(vi+1).
Corollary: Cycle cost unchanged.
Corollary: For a fixed s, t pair, all s-t path lengths change by the
same amount, d(s) d(t).
Equivalence Theorem: For any d : V R, and d define equivalent problems.
Feasibility Condition: The problem is feasible iff
d : (v, w) A, d(v, w) 0 (feasible d).
Proof: Only if: no negative cycles (for d and thus for ).
If: negative cycle implies no feasible d exists.
DIKU Summer School on Shortest Paths
s
DIKU Summer School on Shortest Paths
Zero Cycles
10
Scanning Method
labeled
scanned
11
12
13
Proof: Last time (w) set to v, d(w) = d(v) + (v, w). After
14
Correctness (cont.)
15
16
PERT application.
Linear time algorithm:
1. Topologically order vertices.
2. Scan in topological order.
Correctness: When scanning i, all of its predecessors have correct
distances.
Running time: Linear.
17
Bellman-Ford-Moore Algorithm
The BFM algorithm processes labeled vertices in FIFO order.
Use a queue with constant time enqueue/dequeue operations.
Definition: Initialization is pass zero. Pass i + 1 consists of
processing vertices on the queue at the end of pass i.
Lemma: No negative cycles termination in less then n passes.
18
Heuristics
19
20
Tarjans Algorithm
Subtree disassembly: do subtree traversal; if v is not in the
subtree, delete all vertices u 6= w of the subtree from G , set
(u) = NULL and S(u) = unreached.
Remark: This variation of the scanning method allows unreached
vertices with finite d.
Correctness: Since d(w) just decreased, removed vertices have
d(u) > dist(s, u), so they will become labeled again.
Analysis: Similar to BFM, except note that subtree disassembly
work can be amortized over the construction of the subtree.
O(nm) time.
Practical improvement: Some labeled vertices u with d(u) >
dist(s, u) become unreached and not scanned; direct and (potentially bigger) indirect savings.
DIKU Summer School on Shortest Paths
21
GOR Algorithm
[Goldberg & Radzik 93]
22
N
N
tarj
gor
simp
J
J
palt
N
J
pal
means good,
means fair,
means poor, and means bad.
GOR is O(m) on acyclic graphs.
J
23
Square Grids
time (sec.) / scans per vertex
GOR scans include DFS scans.
nodes/arcs
4097
12288
16385
49152
65537
196608
262145
786432
1048577
3145728
bfm
0.03
2.74
0.21
5.05
1.92
9.66
19.30
19.68
165.57
41.78
tarj
0.02
1.35
0.09
1.43
0.39
1.48
1.69
1.52
7.50
1.57
gor
0.02
2.26
0.08
2.29
0.37
2.28
1.94
2.29
7.52
2.30
simp
0.02
1.34
0.10
1.42
0.45
1.46
1.93
1.50
8.52
1.54
palt
0.01
1.25
0.05
1.26
0.29
1.27
1.20
1.27
5.16
1.27
pal
0.02
1.25
0.04
1.26
0.22
1.27
0.97
1.27
4.08
1.27
24
nodes/arcs
4098
16385
16386
65537
65538
262145
262146
1048577
1048578
4194305
bfm
0.05
4.78
0.40
9.19
3.35
17.43
33.66
34.12
279.75
70.97
tarj
0.03
2.37
0.16
2.48
0.67
2.52
2.88
2.56
12.55
2.62
gor
0.05
4.51
0.18
4.57
0.87
4.59
4.08
4.62
16.58
4.62
simp
0.03
2.37
0.18
2.46
0.81
2.50
3.37
2.54
14.72
2.58
palt
0.32
29.10
2.14
39.21
16.49
71.46
130.42
124.35
pal
0.26
38.14
2.24
71.31
26.28
166.50
387.80
489.18
25
Experiments/Negative Cycles
These are most robust algorithms; really bad ones excluded
tarj
Rand-5
SQNC01
SQNC02
SQNC03
SQNC04
SQNC05
PNC01
PNC02
PNC03
PNC04
PNC05
means good,
gorc
palt
simp
J
N
J
J
J
J
J
means poor.
26
Random Graphs
PALT
SIMP
GOR
TARJ
0
1000
2000
4000 60008000
16000
|length lower bound| (logscale)
32000
64000
27
28
Scaling Algorithm
m
l
(v,w)
i
.
(v, w) =
2i
Note: L 0, 0 = , no new negative cycles.
Let d = 2d and c = Li
d , note c 1.
29
k.
Admissible graph GA is induced by arcs (v, w) with cp(v, w) 0.
Initialize p = 0.
Main loop:
1. DFS admissible graph. Stop if a negative cycle found. Contract zero cycles.
2. Add an artificial source, connect to all vertices by zero arcs.
3. Compute shortest paths in the resulting (acyclic) GA.
30
A Bucketing Argument
Ci
>0
2 1 0
An efficient way to find P or C:
Put vertices in buckets according to the distance d in GA.
31
Fixing Cuts
32
1
0
1
1
0
2
1
2
1 1+1 x1
x 0, x 1 1, no new bad arcs.
DIKU Summer School on Shortest Paths
33
0
0
0
1
0
2
0
2
1 1+1 x1
x 0, x 1 1, no new bad arcs.
DIKU Summer School on Shortest Paths
34
0
3
0
2
0
1
0
1
1 1+1 x1
x 0, x 1 1, no new bad arcs.
DIKU Summer School on Shortest Paths
35
Fixing Paths
36
s are between 0
time.
new bad arcs and
Proof: Enough to show n main loop iterations. O( k) iterations reduce the number of bad arcs by a factor of two. The
total is bounded by
r
X
X
n
i
= n
( 2) = O( n).
i
i=0 2
i=0
37
38
Use of landmarks.
A demo.
39
40
discoverer
bounds
note
1959
1964
1969
1977
1978
1979
1987
1990
1996
1996
1997
1997
2001
2001
2002
2004
2004
Dijkstra
Williams
Dial
Johnson
Dinitz
Denardo & Fox
Fredman & Tarjan
Ahuja et al.
Thorup
Raman
Cherkassky et al.
Raman
Meyer
Goldberg
Han & Thorup
Hagerup
Thorup
O(n2)
O(m log n)
O(m + nU )
O(m log(2+m)/n n)
O(m + n(U/delta))
log U )
)
O(m + n log
log U
O(m + n
log n)
O(m + n log U )
O(m log log
n)
O(m + n log n)
O(m + n(log U )(1/3)+)
O(m + n(log U log log U )1/4+)
E(m) & O(nm log n)
E(m)p& O(m + n log U )
E(m log log min(n, U ))
E(m)& O(m + n log n)
O(m + n log log min(n, U ))
min. selection
2-heap
buckets
d-heap
non-min. selection
MB
Fibonacci heap
MB+Fib. heap
new heap
new heap
HOT q.
HOT+impr. heap
expected
MB variant
randomized
more distributions
new heap
41
42
43
0
2
2
4
9 10
s
DIKU Summer School on Shortest Paths
44
Dials Algorithm
Maintain an array B[0 . . . (n 1)U ] of buckets (vertex sets).
Keep a labeled vertex v in B[d(v)].
The smallest-labeled vertex is in the first nonempty (active)
bucket.
0 0
s
2
0
b
e
d
s
a
45
Dials Algorithm
Maintain an array B[0 . . . (n 1)U ] of buckets (vertex sets).
Keep a labeled vertex v in B[d(v)].
The smallest-labeled vertex is in the first nonempty (active)
bucket.
0 0
s
2
0
a
2
b
0
2
e
d
b
c b
46
Dials Algorithm
Maintain an array B[0 . . . (n 1)U ] of buckets (vertex sets).
Keep a labeled vertex v in B[d(v)].
The smallest-labeled vertex is in the first nonempty (active)
bucket.
0
a
0 0
s
2
2
b
1
c
0
2
e
d
c b
b
47
Dials Algorithm
Maintain an array B[0 . . . (n 1)U ] of buckets (vertex sets).
Keep a labeled vertex v in B[d(v)].
The smallest-labeled vertex is in the first nonempty (active)
bucket.
0
a
0 0
s
2
1
b
1
c
0
2
3
e
e
ed
48
Dials Algorithm
Maintain an array B[0 . . . (n 1)U ] of buckets (vertex sets).
Keep a labeled vertex v in B[d(v)].
The smallest-labeled vertex is in the first nonempty (active)
bucket.
0
a
0 0
s
2
1
b
1
0
2
1
c
3
d
3
e
ed
d
DIKU Summer School on Shortest Paths
49
Dials Algorithm
Maintain an array B[0 . . . (n 1)U ] of buckets (vertex sets).
Keep a labeled vertex v in B[d(v)].
The smallest-labeled vertex is in the first nonempty (active)
bucket.
0
a
0 0
s
2
1
b
1
0
2
1
c
3
d
3
e
50
Dials Performance
51
Two-Level Buckets
0...3
4...7
***
*
8...11 12...15 16...19 20...23
*
8
**
U = 16
9 10 11
U.
52
O(m + n U ) time.
53
Multi-Level Buckets
54
Low-Level Details
Word RAM model, AC 0 word operations.
B(i, j): j-th bucket at level i.
is the last extracted key value (initially zero).
Bucket positions are w.r.t. , which is a the bottom (level 0)
t denotes base with t lowest bits replaced by 0s.
Computing positions in B:
Consider values base (power of two).
Buckets correspond to value ranges:
B(i, j) [i + ji, i + (j + 1)i).
Position of u: i is the index of the first digit u and differ
in, j is the digit value.
Can be computed in constant time.
DIKU Summer School on Shortest Paths
55
Example
e
cd
a b
Base = 2, = 00110.
a = 00110, b = 00111, c = 01000, d = 01111, e = 10010.
56
Details (cont.)
Insert: Find the position (i, j) of u and insert u into B(i, j).
decreaseKey: Delete, insert with the new key.
Both operations take constant time.
extractMin:
Find lowest nonempty level i (constant time).
Set j to the i-th digit of , while B(i, j) empty increment j.
If i = 0 delete u from B(i, j), set = u, return u.
Otherwise
Find and delete minimum u in B(i, j), set = u.
Expand B(i, j) by inserting v B(i, j) into new positions.
return u.
Positions change only for v B(i, j), vertex levels decrease.
Incrementing j charged to extracted vertex;
expand work charged to vertex level decreases.
DIKU Summer School on Shortest Paths
57
HOT Queues
) amortized time.
58
log U = 6
59
Dinitz Algorithm
60
Calibers
61
62
Worst-Case Time
63
Average-Case Analysis
Probabilistic model: Assume that arc lengths are integers uniformly distributed on [1, M ]. Similar analysis works for some
other distributions, e.g., reals on [0, 1].
64
V
A w(u, v).
65
Experimental Evaluation
66
Engineering Aspects
67
Random graphs: degree 4, arc lengths independent and uniformly distributed on [1, M ].
Many labeled vertices most of the time.
RAND-I: M = n, n grows.
RAND-C: n fixed, M grows.
Long grids: Width 8, arc lengths independent and uniformly
distributed on [1, M ].
Few labeled vertices at any time.
LONG-I: M = n, n grows.
LONG-C: n fixed, M grows.
68
RAND-I Data
BFS
0.15
sec.
218
0.30
sec.
219
0.62
sec.
220
1.30
sec.
221
2.90
sec.
MB2L
1.55
3.01
1.09
1.73
3.03
1.45
1.68
3.34
1.06
1.83
3.34
1.41
1.83
3.67
1.16
SQ2L
1.56
1.09
1.04
1.64
0.79
1.19
1.63
1.12
1.01
1.79
0.79
1.20
1.77
1.13
1.03
MB2D
3.86
0.74
7.14
4.15
0.74
7.65
4.41
0.74
8.15
4.64
0.74
8.65
4.73
0.74
9.13
SQ2D
2.26
0.01
1.56
2.26
0.01
1.56
2.31
0.00
1.58
2.35
0.00
1.58
2.29
0.00
1.56
MB-A
1.88
2.05
2.00
1.93
2.04
2.36
1.89
2.36
1.96
1.94
2.37
2.09
2.00
2.36
2.33
SQ-A
1.63
0.06
1.05
1.73
0.04
1.19
1.71
0.05
1.01
1.79
0.03
1.06
1.78
0.03
1.16
H2
4.33
H4
3.60
PAL
1.67
5.03
4.30
1.64
5.58
4.60
1.84
5.99
4.86
1.85
6.54
5.07
1.84
Note: SQ2D vs. MB2D; 2L codes work well: SQ-A is competitive: PAL similar to MB-A; heaps are not competitive.
69
LONG-I Data
BFS
0.08
sec.
218
0.17
sec.
219
0.35
sec.
220
0.75
sec.
221
1.61
sec.
MB2L
1.71
8.59
0.46
1.66
12.45
0.27
1.65
13.65
0.42
1.59
17.89
0.23
1.60
23.59
0.40
SQ2L
1.71
4.77
0.39
1.61
5.10
0.22
1.65
9.43
0.38
1.60
10.09
0.21
1.63
18.84
0.37
MB2D
2.71
1.00
1.51
2.80
1.00
1.51
2.74
1.00
1.51
2.72
1.00
1.51
2.65
1.00
1.51
SQ2D
2.14
0.29
0.83
2.13
0.29
0.83
2.17
0.29
0.83
2.10
0.29
0.83
2.06
0.29
0.83
MB-A
1.86
4.88
0.61
1.81
4.17
0.63
1.73
8.89
0.52
1.64
6.98
0.45
1.62
5.88
0.52
SQ-A
1.71
1.00
0.41
1.68
1.31
0.46
1.61
1.40
0.34
1.60
1.47
0.31
1.59
2.44
0.42
H2
2.50
H4
2.50
PAL
1.63
2.35
2.35
1.58
2.29
2.29
1.51
2.09
2.08
1.41
1.96
1.94
1.34
Note: Easy problems, especially for PAL and heaps; SQ2D vs.
MB2D; 2L and SQ-A perform OK.
70
Hard Problems
000
001
00f
01f
0ff
1ff
fff
=
16.
Arc
lengths
are given in
hexadecimal.
Arcs designed
to manipulate
vertex calibers
omitted.
71
time
emp.
exp.
time
emp.
exp.
time
emp.
exp.
time
emp.
exp.
time
emp.
exp.
time
emp.
exp.
time
emp.
exp.
time
emp.
exp.
BFS
0.63
sec.
0.63
sec.
0.62
sec.
0.63
sec.
0.62
sec.
0.63
sec.
0.63
sec.
0.62
sec.
MB
1189.20
52428.94
0.60
10.66
1170.14
1.14
2.68
170.44
1.56
2.25
24.31
2.46
2.87
6.37
3.89
3.70
3.12
5.36
5.86
1.41
8.32
16.32
0.49
17.75
SQ
1.38
0.40
0.30
1.39
0.15
0.57
1.44
0.11
0.67
1.53
0.08
0.77
1.60
0.05
0.85
1.66
0.04
0.88
1.82
0.03
0.93
2.32
0.01
0.97
k
2
3
4
6
9
12
18
36
time
emp.
exp.
time
emp.
exp.
time
emp.
exp.
time
emp.
exp.
time
emp.
exp.
time
emp.
exp.
time
emp.
exp.
time
emp.
exp.
BFS
0.62
sec.
0.62
sec.
0.63
sec.
0.62
sec.
0.62
sec.
0.62
sec.
0.63
sec.
0.63
sec.
MB
1199.67
52428.94
0.60
9.38
1170.14
1.14
2.67
170.44
1.56
2.25
24.31
2.46
2.87
6.37
3.89
3.72
3.12
5.36
5.86
1.41
8.32
16.24
0.49
17.75
SQ
1201.13
52428.94
0.60
9.39
1170.14
1.14
2.82
170.44
1.56
2.45
24.31
2.46
3.08
6.37
3.89
3.93
3.12
5.36
6.05
1.41
8.32
16.43
0.49
17.75
72
Worst-Case Performance
bits
4
log
4
k
1
15
18
24
30
35
42
49
time
emp.
exp.
time
emp.
exp.
time
emp.
exp.
time
emp.
exp.
time
emp.
exp.
time
emp.
exp.
time
emp.
exp.
time
emp.
exp.
time
emp.
exp.
time
emp.
exp.
BFS
0.62
sec.
0.63
sec.
0.62
sec.
0.62
sec.
0.62
sec.
0.62
sec.
0.62
sec.
0.62
sec.
0.62
sec.
0.62
sec.
SQ-A
1.37
0.33
0.04
1.50
1.60
0.80
1.51
3.20
0.80
1.73
9.00
1.14
1.78
18.14
1.14
1.98
21.11
1.56
2.18
23.00
2.00
2.32
46.27
2.00
2.55
48.92
2.46
2.80
50.87
2.93
73
Remarks on Experiments
74
75
Example
76
Bidirectional Algorithm
77
Bidirectional Example
78
3
2
s
4
b
x
t
4
79
Use of Preprocessing
If expect many different s, t queries on the same graph, can preprocess the graph (e.g., map graph). May be unable to store all
pairs of shortest paths.
80
A Search
AI motivation: search a small subset of a large space.
[Doran 67], [Hart, Nilsson, Raphael 68].
Similar to Dijkstras algorithm but:
Domain-specific estimates t(v) on dist(v, t) (potentials).
At each step pick a vertex with min. k(v) = ds(v) + t(v).
Scan a node on a path with the best length estimate.
In general, optimality is not guaranteed.
1111111
0000000
1111111
0000000
1111111
0000000
000000
111111
1111111
0000000
0
1
0
1
0
1
0
1
1111111
0000000
1111111
0000000
0
1
0
1
0
1
0
1
1111111
0000000
1111111
0000000
1111111
0000000
1111111
0000000
4
3
2
1
0
5
1111111
0000000
1111111
0000000
111111111111
000000000000
1111111
0000000
1111111
0000000
0
1
0
1
111111111111
000000000000
1111111
0000000
0
1
0
1
111111111111
000000000000
1111111
0000000
111111111111
000000000000
1111111
0000000
0
111111111111
000000000000
1111111
0000000
1111111
0000000
000000
111111
111111111111
000000000000
1111111
0000000
0
1
0
1
0
1
0
1
0
1
0
1
81
for fixed s.
Different order of vertex scans, different subgraph searched.
Lemma: If t is feasible and t(t) = 0, then t gives lower bounds
on distances to t.
Proof: Left as an exercise.
DIKU Summer School on Shortest Paths
82
Bidirectional A search
83
11111111111111
1111
0000
00000000000000
11111111111111
00000000000000
1111
0000
0
1
0
1
1111
11111111111111
0000
00000000000000
11111111111111
00000000000000
1111
0000
0
1
0
1
1111
11111111111111
0000
00000000000000
11111111111111
00000000000000
1111
0000
1111
11111111111111
0000
00000000000000
11111111111111
00000000000000
1111
0000
1111
11111111111111
0000
00000000000000
11111111111111
00000000000000
1111
0000
111111111111
000000000000
1111
11111111111111
0000
00000000000000
11111111111111
00000000000000
1111
0000
0
1
0
1
0
1
0
1
84
85
86
Active Landmarks
87
Landmark Selection
88
Demo
89
Memory-Efficient Implementation
90
Implementation (cont.)
distance,
91
92
Remarks
NSSSP almost solved in theory and in practice.
Big open question: linear-time algorithm (like [Thorup 99]
for undirected graphs).
Preprocessing may help locality.
P2P problem with preprocessing: significant recent progress
in algorithms for special graph classes. Theoretical work
trails behind.
SSSP problem: widely studied but open questions remain,
even for worst-case bounds: e.g., BFM is still the best stronglypolynomial algorithms.
Other topics include dynamic and all pair algorithms, sophisticated data structures, special networks (planar, small
lengths, . . .), etc.
DIKU Summer School on Shortest Paths
93