Professional Documents
Culture Documents
Adc 11
Adc 11
Course 11
Overview
All-Pairs Shortest Paths (APSP)
Using SSSP Algorithms
Simple DP Algorithms
Floyd-Warshall Algorithm
Johnson’s Algorithm
Transitive Closure of a Graph
All-Pairs Shortest Paths (APSP)
G(V, E) (un)directed, connected and weighted graph
The weight (cost) function w: E → R
w(u, v) = the weight of the edge (u, v)
But
L(1)[i, j] = mink=1..n(L(0)[i, k] + w(k, j))
= L(0)[i, i] + w(i, j) (the only non-INF value)
= w(i, j)
Simple DP Algorithm for APSP (2)
There are at most n – 1 edges on each shortest path
Therefore, we want to compute L(n-1)
Afterwards, the matrix should not change anymore:
L(n-1) = L(n) = L(n+1) = …
δ(i, j) = L(n-1)[i, j] = L(n)[i, j] = L(n+1)[i, j]= …
Start from L(1) = W
Compute the solution in a bottom-up manner
L(1), L(2), …, L(k), …, L(n-1)
Simple DP Algorithm – Pseudocode
SLOW-APSP(G, W)
n = |V[G]|
L[1] = W
FOR (m=2; m < n; m++)
L[m] = EXPAND(L[m-1], W, n)
RETURN L[n-1]
EXPAND (L, W, n)
L’ = new matrix(n, n)
FOR (i=1; i <= n; i++)
FOR (j=1; j <= n; j++)
L’[i][j] = INF
FOR (k=1; k <= n; k++)
L’[i][j] = min(L’[i][j], L[i][k] + w[k][j])
RETURN L’
Time complexity:
EXPAND - (n3)
SLOW-APSP - (n4) not very good! Same as Bellman-Ford - n times!
Space complexity: uses n matrices - (n3) can be reduced by using the same matrix L
Improved Simple DP Algorithm
Improve the way L(n-1) is computed
Instead of computing:
L(1), L(2), …, L(k), …, L(n-1)
Why not compute?
L(1), L(2), L(4), …, L(2^k), …, L(2^r)
Stop when r = ceiling(log(n-1)) >= n-1, but this is ok!
Time complexity:
EXPAND - (n3)
FAST-APSP - (n3 * logn) Still not very good! But better than Bellman-Ford - n times!
Space complexity: uses n matrices - (n3) can be reduced by using the same matrix L
Floyd-Warshall Algorithm
Use another DP formulation
Given a path p = <v1, v2, … , vj>
Any vertex except v1 and vj are intermediate vertices
Recursive formulation:
D(k)[i, j] = min(D(k-1)[i, j], D(k-1)[i, k] + D(k-1)[k, j])
Choose between:
The shortest path between i and j that contains intermediate
vertices in {1, 2, … , k-1}
The sum of the shortest paths from i to k and from k to j that
contain intermediate vertices in {1, 2, … , k-1}
Floyd-Warshall – Recursive Formulation
The recursive formulation can be proved by
induction
On whiteboard
Can also compute P(k)
How?
Compute the solution in a bottom-up fashion
D(0), D(1),…, D(k),…, D(n)
Floyd-Warshall – Pseudocode
FLOYD-WARSHALL(G, W)
n = |V[G]|
D[0] = W
FOR (i = 1; i <= n; i++)
FOR (j = 1; j <= n; j++)
IF (w(i, j) != INF)
P[0][i][j] = i
ELSE
P[0][i][j] = NIL
FOR (k = 1; k <= n; k++)
FOR (i = 1; i <= n; i++)
FOR (j = 1; j <= n; j++)
IF (D[k-1][i][j] < D[k-1][i][k] + D[k-1][k][j])
D[k][i][j] = D[k-1][i][j]
P[k][i][j] = P[k-1][i][j]
ELSE
D[k][i][j] = D[k-1][i][k] + D[k-1][k][j]
P[k][i][j] = P[k-1][k][j]
RETURN D[n]
Time complexity: (n3) good for dense graphs and for graphs with negative weights
Space complexity: (n3) can be reduced to (n2) by using a single D matrix and a single P matrix
Example (1)
0 3 8 ∞ -4
2
3 4 ∞ 0 ∞ 1 7
7
8
D(0) = ∞ 4 0 ∞ ∞
1 3 2 ∞ -5 0 ∞
-4 2
1 ∞ ∞ ∞ 6 0
-5
5 4
6 nil 1 1 nil 1
nil nil nil 2 2
P(0) = nil 3 nil nil nil
4 nil 4 nil nil
nil nil nil 5 nil
Prove that w1(u, v) = w(u, v) + h(u) – h(v) >= 0 for all (u,
v)E
Use triangle inequality for edge (u, v)E
In G’, we have h(v) = δ(s, v) <= h(u) + w(u, v) = δ(s, u) + w(u, v)
Therefore h(u) + w(u, v) – h(v) >= 0
Johnson’s Algorithm – Pseudocode
JOHNSON(G, W)
G’ = (V’,E’);
V’ = V {s}; // add new source s
E’ = E (s,u), uV; w’(s,u) = 0;
IF (BF(G’, W’) == FALSE) // run BF on G’
PRINT “Error! Negative cycle was found!”
ELSE
FOREACH (vV)
h(v) = δ(s,v); // computed by Bellman Ford
FOREACH ((u,v)E)
w1(u,v) = w(u,v) + h(u) - h(v) // compute new positive weights
FOREACH (uV)
Dijkstra(G,w1,u) // run Dijkstra for each vertex as a source using w1
FOREACH (vV)
d(u,v) = δ1(u,v) + h(v) - h(u) // we need to switch back from w1 to w
0 -1
2 0 2
3 4 3 4
7 0 0 7
0 8 -5
8 s 1 3
1 3
1 1
2 0 -4 2
-4 -5 -5
5 4 5 4
6 0 6 0
Add s and run -4
B-F on the
new graph.