Lecture 31: All-Pair Shortest Paths

advertisement
CSE 431/531: Analysis of Algorithms
Spring 2016
Lecture 31: All-Pair Shortest Paths
Apr 4, 2016
Lecturer: Shi Li
All-pair shortest paths:
• Given: G = (V, E), directed, w : E → R.
• Output: shortest path from u to v, for every u, v ∈ V
Use single source shortest paths as a black box:
• for every s ∈ V , compute shortest paths from s to every v ∈ V , using any single source shortest path
algorithm.
Running time:
O(n × (n lg n + m)) = O(n2 lg n + nm)
• Weights are non-negative: Dijkstra’s Algorithm
• Weights may be negative: Bellman-Ford Algorithm
O(n × nm)) = O(n2 m)
Focus of today: weights may be negative.
“Matrix-Multiplication”-style algorithm for all-pair shortest paths: A is a n × n matrix and consider the
following divide-and-conquer algorithm to compute Am efficiently:

if m = 1

A
2
m
m/2
A =
A
if m ≥ 2 and m is even

 bm/2c 2
A
A if m ≥ 2 and m is odd
• Running time of D&C algorithm: O(lg m × time for multiplying two matrices of size n × n)
• time for multiplying two matrices = O(n3 ) (unfortunately we can not use the O(nlg2 7 )-time algorithm)
N
For two matrices A and B of size n × n, define C = A B be a matrix of size n × n such that
Ci,j =
min
{Ai,k + Bk,j }.
k=1,2,3,··· ,n
P
If we change min to
and + to ×Nin the above definition, then we obtain the definition of C = AB. Similar
to the matrix multiplication, the
operation satisfies associativity.
N
N
N N
Exercise 31.1 For 3 matrices A, B, C of size n × n, we have (A B) C = A (B C).
N N N
N
Thus, we can compute A A A · · · A (m matrices) in O(lg m · n3 ) time.
Define W to be the following matrix of size n × n such that


w(i, j) if (i, j) ∈ E
Wi,j = 0
.
if i = j


∞
if i 6= j and (i, j) ∈
/E
For any integer t ≥ 1, define W (t) = W
N
W
N
···
N
W (t times).
(t)
Exercise 31.2 For every t ≥ 1, Wi,j is either ∞ or the length of some path from i to j.
31-1
31-2
Lecture 31: All-Pair Shortest Paths
Lemma 31.3 For any t ≥ 1, W (t) is at most the length of the shortest path from i to j that uses at most t
edges.
Proof: By induction: lemma holds for t = 1. Suppose lemma holds for t − 1. Then consider the shortest
path P from i to j that uses at most t edges. If P uses at most t − 1 edges, then W (t−1) is at most the
(t)
(t−1)
(t−1)
length of P by induction. Then, Wi,j ≤ Wi,j + Wj,j = Wi,j . If P uses exactly t edges let k be the last
(t−1)
vertex before j in P ; let P 0 be the segment of P from i to k. By induction hypothesis, Wi,k
0
the length of P . Then
(t)
Wi,j
≤
(t−1)
Wi,k
is at most
0
+ Wk,j ≤ length of P + w(k, j) = length of P .
(n−1)
Corollary 31.4 If there are no negative cycles in G, then Wi,j
to j.
is the length of the shortest paths from i
Floyd-Warshall Algorithm: (Assume V = {1, 2, 3, · · · , n}. )
let matrix F ← W initially
for k ← 1ton
for i ← 1 to n
for j ← 1 to n
if Fi,k + Fk,j < Fi,j then Fi,j ← Fi,k + Fk,j
• Running time = O(n3 )
Again, it is easy to see that at any time Fi,j is either ∞, or the length of some path from i to j.
Lemma 31.5 Let F (k) be the F matrix after the k-th iteration of the Floyd-Warshall Algorithm. Assume
(k)
there are no negative cycles in G. Then Fi,j is at most the length of the shortest path from i to j that only
uses {1, 2, 3, · · · , k} as intermediate vertices.
Proof: This is an homework problem.
Download