Chapter 25: All-Pairs
Shortest-Paths
1
Some Algorithms
When no negative edges
– Using Dijkstra’s algorithm: O(V3)
– Using Binary heap implementation: O(VE lg V)
– Using Fibonacci heap: O(VE + V2log V)
• When no negative cycles
– Floyd-Warshall [1962]: O(V3) time
• When negative cycles
– Using Bellman-Ford algorithm: O(V2 E) = O(V4 )
– Johnson [1977]: O(VE + V2log V) time based on a
clever combination of Bellman-Ford and
Dijkstra
2
A dynamic programming
approach
1. characterize the structure of an
optimal solution,
2. recursively define the value of an
optimal solution,
3. compute the value of an optimal
solution in a bottom-up fashion.
3
The structure of an optimal
solution
• Consider a shortest path p from
vertex i to vertex j, and suppose that
p contains at most m edges. Assume
that there are no negative-weight
cycles. Hence m ≤ n-1 is finite.
4
The structure of an optimal
solution (cont.)
• If i = j, then p has weight 0 and no edge
• If i≠j, we decompose p into i ~ k -> j
where p’ contains at most m-1 edges.
p'
• Moreover, p’ is a shortest path from i
to k and δ(i,j) = δ(i,k) + wkj , where δ(i,j)
dente the shortest weight path from i
to j
5
Recursive solution to the allpairs shortest-path problem
• Define: lij(m) = minimum weight of any
path from i to j that contains at most
m edges.
0 if i = j
• lij(0) =
∞ if i ≠ j
• Then lij(m) = min{ lij(m-1), min1≤k≤n {lik(m-1) +
wkj}} = min1≤k≤n {lik(m-1) + wkj}
(why?)
6
Recursive solution to the allpairs shortest-path problem
• Since shortest path from i to j contains
at most n-1 edges,
δ(i,j) = lij(n-1) = lij(n) = lij(n+1) = …
• Computing the shortest-path weight
bottom up:
– Compute L(1) ,L(2) ,…., L(n-1) where
L(m)=(lij (m)) for all i and j
– Note that L(1) = W.
7
Example:
0 3 8 4
0 1 7
W = L(1) 4 0
2 5 0
6 0
2
4
3
8
1
3
7
1
-4
-5
2
L( 2)
5
4
6
8 2 4
0 3
3 0 4 1 7
4
0 5 11
2 1 5 0 2
8 1 6 0
L(2) = L(1) x W
8
L(3)
0 3 3 2 4
3 0 4 1 1
7 4
0 5 11
2 1 5 0 2
8 5
1
6
0
L(3) = L(2) x W
L( 4)
0 1 3 2 4
3 0 4 1 1
7 4
0 5 3
2 1 5 0 2
8 5
1
6
0
L(4) = L(3) x W
9
EXTENDED-SHORTESTPATHS(L, W)
•
1
2
3
4
5
6
7
8
Given matrices L(m-1) and W return L(m)
n <- L.row
Let L’ = (l’ij) be a new n x n matrix
for i = 1 to n
for j = 1 to n
l’ij = ∞
for k = 1 to n
l’ij = min(l’ij, lik + wkj)
return L’
10
Matrix Multiplication
Let l(m-1) -> a
w
-> b
l(m) -> c
min -> +
+ -> ‧
Cij = k=1 to n aik‧bkj
lij(m) = min1≤k≤n {lik(m-1) + wkj}
time complexity : O(n3)
11
SLOW-ALL-PAIRSSHORTEST-PATHS(W)
L(1) = L(0) ‧ W = W
L(2) = L(1) ‧ W = W2
L(3) = L(2) ‧ W = W3
‧
‧
‧
L(n-1) = L(n-2) ‧W = Wn-1
12
SLOW-ALL-PAIRSSHORTEST-PATHS(W)
1 n = W.rows
2 L(1) = W
3 for m = 2 to n-1
4
let L(m) be a new n x n matrix
5
L(m) = EXTENDED-SHORTESTPATHS( L(m-1), W )
6 return L(n-1)
Time complexity : O(n4)
13
Improving the running time
L(1) = W
L(2) = W2 =W.W
L(4) =W. 4 = W2.W2
..
L(n) = Wn =Wn/2.Wn/2
i.e., using repeating squaring!
Time complexity: O(n3lg n)
14
FASTER-ALL-PAIRS-SHORTESTPATHS
FASTER-ALL-PAIRS-SHORTEST-PATHS(W)
1. n =W.row
2. L(1) =W
3. m =1
4. while m < n-1
5.
let L(2m) be a new n x n matrix
6.
L(2m) = Extend-Shorest-Path(L(m), L(m))
7.
m = 2m
8. return L(m)
15
The Floyd-Warshall algorithm
• A different dynamic programming
formulation
‧The structure of a shortest path:
Let V(G)={1,2,…,n}. For any pair of
vertices i, j єV, consider all paths from i
to j whose intermediate vertices are
drawn from {1, 2,…,k}, and let p be a
minimum weight path among them.
16
The structure of a shortest path
• If k is not in p, then all intermediate
vertices are in {1, 2,…,k-1}.
• If k is an intermediate vertex of p, then
p1
p
p can be decomposed into i ~ k ~ 2 j
where p1 is a shortest path from i to k
with all the intermediate vertices in
{1,2,…,k-1} and p2 is a shortest path
from k to j with all the intermediate
vertices in {1,2,…,k-1}.
17
A recursive solution to the allpairs shortest path
• Let dij(k) =the weight of a shortest path
from vertex i to vertex j with all
intermediate vertices in the set
{1,2,…,k}.
dij(k) = wij
if k = 0
= min(dij(k-1), dik(k-1) + dkj(k-1)) if k ≥ 1
D(n) = (dij(n)) if the final solution!
18
FLOYD-WARSHALL(W)
1. n = W.rows
2. D(0)= W
3. for k = 1 to n
4. Let D(k) = (dij(k)) be a new n x n matrix
5.
for i = 1 to n
6.
for j = 1 to n
7.
dij(k) = min (dij(k-1), dik(k-1) + dkj(k-1))
8. return D(n)
Complexity: O(n3)
19
Example
D (0)
0 3 8 4
0 1 7
4 0
2 5 0
6 0
(0)
N
N
N
4
N
1
1
N
N
N
2
3
N
N
N
4
N
N
N
5
1
2
N
N
N
D (1)
0 3 8 4
0 1 7
4 0
2 5 5 0 2
6 0
(1)
N
N
N
4
N
1
1
N
N
N
2
3
N
N
1
4
N
N
N
5
1
2
N
1
N
20
D (2)
(2)
4 4
1 7
5 11
0 2
6 0
0 3 8
0
4 0
2 5 5
N
N
N
4
N
1
1
2
N
N
2
3
N
2
1
4
N
N
N
5
1
2
2
1
N
D ( 3)
(3)
8
0 3
0
4
0
2 1 5
N
N
N
4
N
4 4
1 7
5 11
0 2
6 0
1
1
2
N
N
2
3
N
2
3
4
N
N
N
5
1
2
2
1
N
21
D (4)
0 3 1 4 4
3 0 4 1 1
7 4
0 5 3
2 1 5 0 2
8 5
1
6
0
(4)
N
4
4
4
4
1
4
2
N
4
2
3
N
2
3
4
N
3
4
5
1
1
1
1
N
D (5)
0 1 3 2 4
3 0 4 1 1
7 4
0 5 3
2 1 5 0 2
8 5
1 6 0
(5)
N
4
4
4
4
3
4
5
N
4
2
3
N
2
3
4
N
3
4
5
1
1
1
1
N
22
Constructing a shortest path
• π(0), π(1),…, π(n)
• πij(k) : is the predecessor of the vertex j on
a shortest path from vertex i with all
intermediate vertices in the set {1,2,…,k}.
πij(0) = NIL if i=j or wij = ∞
= i
if i ≠j and wij < ∞
πij(k) = πij(k-1) if dij(k-1) ≤ dik(k-1) + dkj(k-1)
= πkj(k-1) if dij(k-1) > dik(k-1) + dkj(k-1)
23
Transitive closure of a directed
graph
• Given a directed graph G = (V, E) with V =
{1,2,…, n}
• The transitive closure of G is G*= (V, E*) where
E*={(i, j)| there is a path from i to j in G}.
Modify FLOYD-WARSHALL algorithm:
tij(0) = 0 if i≠j and (i,j) ∉ E
1 if i=j or (i,j) є E
for k ≥ 1
tij(k) = tij(k-1) (tik(k-1) tkj(k-1))
24
Transitive-Closure of a
Directed Graph
• Given a directed graph G=(V, E) with V=
{1, 2, …n}
• We might wish to determine whether G
contains a path from I to j for all
vertex pairs i, j єV.
• We define the transitive closure of G
as the graph G* = (V, E*), where E* =
{(i, j): there is a path from vertex i to
vertex j in G}
25
TRANSITIVE-CLOSURE(G)
1 n = |G.V|
2 Let T(0) = (tij(0)) be a new n x n matrix
3 for i = 1 to n
4
for j =1 to n
5
if i == j or (i, j) ∈ G.E
6
tij(0) = 1
7
else tij(0) = 0
Time complexity: O(n3)
8 for k =1 to n
9
Let T(k) = (tij(k)) be a new n x n matrix
10
for i = 1 to n
11
for j =1 to n
12
tij(k) = tij(k-1) (tik(k-1) tkj(k-1))
26
123 return T(n)
Example
①
④
T (0)
1
0
0
1
0 0 0
1 1 1
1 1 0
0 1 1
T (2)
1
0
0
1
0 0 0
1 1 1
1 1 1
0 1 1
T (4)
1
1
1
1
0 0 0
1 1 1
1 1 1
1 1 1
②
③
T (1)
1
0
0
1
T (3)
1
0
0
1
0 0 0
1 1 1
1 1 0
0 1 1
0 0 0
1 1 1
1 1 1
1 1 1
27
Some Algorithms
When no negative edges
– Using Dijkstra’s algorithm: O(V3)
– Using Binary heap implementation: O(VE lg V)
– Using Fibonacci heap: O(VE + V2log V)
When no negative cycles
– Floyd-Warshall [1962]: O(V3) time
• When negative cycles
– Using Bellman-Ford algorithm: O(V2 E) = O(V4 )
– Johnson [1977]: O(VE + V2log V) time based on a
clever combination of Bellman-Ford and
Dijkstra
28
Johnson’s algorithm for sparse
graphs
• If all edge weights in G are nonnegative,
we can find all shortest paths in O(V2lg V
+ VE) by using Dijkstra’s algorithm with
Fibonacci heap
• Bellman-Ford algorithm takes O(V2E)
• Using reweighting technique
29
Reweighting technique
• If G has negative-weighted edge, we
compute a new set of nonnegative weight
that allows us to use the same method.
The new set of edge weight ŵ satisfies:
• 1. For all pairs of vertices u, v єV, a
shortest path from u to v using weight
function w is also a shortest path from u
to v using the weight function ŵ
• 2. ∀(u,v) є E(G), ŵ(u,v) is nonnegative
30
Lemma: (Reweighting doesn’t
change shortest paths)
• Given a weighted directed graph G = (V, E) with
weight function w:E→R , let h:V →R be any
function mapping vertices to real numbers. For
each edge (u,v) є E, ŵ(u,v) = w(u,v) + h(u) – h(v)
• Let P=<v0,v1,…,vk> be a path from vertex v0 to vk
Then w(p) = δ(v0,vk) if and only if ŵ(p) = ˆ (v0,vk)
Also, G has a negative-weight cycle using weight
function w iff G has a negative weight cycle using
weight function ŵ.
• Question: how to setting the value of h(vi) for 31all
i?
Example:
2
3
4
8
1
3
2
1
-4
-5
7
5
6
4
32
h(v) = δ(s, v)
ŵ(u, v) = w(u, v) + h(u) – h(v)
3
1
2
2
-1
4
4
-5
2
1
5
-4
3
0
0
0
10
0
-4
4
-5
2
0
6
3
13
-5
7
-4
0
1
8
0
-1
5
2
0
4
33
Proof
• ŵ(p) = w(p) + h(v0) – h(vk)
ŵ(p) =
=
=
k
ŵ(vi-1 ,vi)
i 1
k
(w(vi-1 ,vi) + h(vi-1)-h(vi))
i 1
k
i 1
w(vi-1 ,vi) + h(v0)-h(vk)
= w(p) + h(v0) – h(vk)
34
Proof
• Because h(v0) and h(vk) do not depend on
the path, if one path from v0 to vk is
shorter than another using weight
function w, then it is also shorter using ŵ.
Thus,
w(p) = δ(v0,vk) if and only if ŵ(p) = (v0,vk)
35
Proof
• G has a negative-weight cycle using w
iff G has a negative-weight cycle using
ŵ.
• Consider any cycle C=<v0,v1,…,vk> with
v0=vk . Then ŵ(C) = w(C) + h(v0) – h(vk) =
w(C) .
Question: how to setting the value of
h(vi) for all i?
36
Producing nonnegative
weight by reweighting
• Given a weighted directed graph G = (V,
E)
• We make a new graph G’= (V’,E’), V’ = V ∪
{s}, E’ = E ∪{(s,v): v є V} and w(s,v) = 0,
for all v in V
• Let h(v) = δ(s, v) for all v V
• We have h(v) ≤ h(u) + w(u, v) (if there is
no negative weight cycle) (why?)
• ŵ(u, v) = w(u, v) + h(u) – h(v) ≥ 0
37
Example:
2
3
4
8
1
3
2
1
-4
-5
7
5
6
4
38
2
0
0
4
3
1
8
S
0
0
3
2
1
0
0
-5
7
-4
5
6
4
39
0
0
S
0
0
0
0
3
1
0
2
-1
4
8
-5
1
-5
2
3
7
-4
5
-4
6
0
4
40
h(v) = δ(s, v)
ŵ(u, v) = w(u, v) + h(u) – h(v)
5
1
0
S 0
4
2
-1
0
1
13
0
-5
2
0
4
0
3
0
10
0
-4
5
2
0
4
41
JOHNSON algorithm
1 Computing G’, where G’.V = G.V ∪ {s}
and G’.E= G.E ∪{(s, v): v є G.V} and w(s, v) = 0.
2 if BELLMAN-FORD(G’, w, s)= FALSE
3 print “the input graph contains negative weight
cycle”
4 else for each vertex v є G’.V
5 set h(v) to be the value of δ(s, v) computed by
the BF algorithm
6 for each edge (u, v) є G’.E, ŵ(u, v) = w(u, v) + h(u)
– h(v)
42
JOHNSON algorithm
7 Let D = (duv) be a new n x n matrix
8 for each vertex u є G.V
run DIJKSTRA (G, ŵ, u) to computeˆ (u, v)
for all v є V[G] .
10 for each vertex v є G.V
11
duv = ˆ (u, v) + h(v) – h(u)
12 return D
Complexity: O(V2lgV + VE)
43
2
2/1
4
2
0
13
1 0/0
2
0
10
0
0/-4
5
2
4
3
1
2/-3
2/3
0
3
13
0/-4
2
0
0
10
0
2/2
2/-1
4
2
2/7
5
0/4
4
1
0/0
0
0
0/1
2
4
3
13
0/0
0
0
2
10
0
2/3
5
2
0/5
4
44
2
1
2
0/-1
4
2/2
0
13
3
1
0/-5
4/8
2
0
0
3
13
2/1
0
0
2
0
10
2/-2
5
2/5
4
2
0/0
4
0
0
10
0/0
5
2
2/6
4
45
Homework
• Practice at home: 25.1-1, 25.1-4, 25.1-6
• Exercises: 25.1-10, 25.2-6, 25.2-8
(Due: Jan. 3)
• Practice at home : 25.3-2, 25.3-4 25.36
• Exercises: 25.3-5 (Due: Jan. 3)
• Bonus: Write a program to find all pairs
shortest path of a graph G. There may
exist negative weight cycle on graph G.
46
© Copyright 2026 Paperzz