Professional Documents
Culture Documents
3 2 7
4 8 1 6
3
-5
The shortest paths between all pairs of vertices in a graph Representation of the result: an n n matrix of shortest-path distances (u, v)
2
4 8 1
3 Assume the graph (G) is given as adjacency matrix of weights 1 2 W = (wij), n x n matrix, |V| = n -4 7 Vertices numbered 1 to n 5 6 0 if i = j wij = weight of (i, j) if i j , (i, j) E if i j , (i, j) E Output the result in an n x n matrix D = (dij), where dij = (i, j) Note: negative weights are allowed but for now no negative cycles.
3
3
-5
Dynamic Programming
dij(m) = weight from i to j with m edges dij(0) = 0 if i = j and dij(0) = if i j dij(m) = min(dij(m-1), mink{dik(m-1) + wkj})= = mink{dik(m-1) + wkj} (Why?)
m-1
Matrix Multiplication
Similar: C = A B, two n n matrices cij = k aik bkj O(n3) operations cij= mink {aik + bkj} D(1)=W D(m) = D(m-1) W=Wm Time is O(n n3 ) = O(n4 ) Repeated squaring: W2n = Wn Wn Compute W, W2 , W4 ,..., W2k , k= log n, O(n3 log n)
Runtime = O(n4)
i
m-1
Floyd-Warshall Algorithm
Vertices in G are given by V = {1, 2, , n} Consider a path p = v1, v2, , vk An intermediate vertex of p is any vertex in the set {v2, v3, , vk-1}
Floyd-Warshall Algorithm
dij(k) = length of shortest path from i to j with intermediate vertices from {1, 2, ..., k}: (i, j)= dij(n)
Dynamic Programming: recurrence
Dik(k-1)
i
7
Dkj(k-1)
j
Dijk
Example
dij(k) = the weight of a shortest path from vertex i to vertex j with all intermediary vertices drawn from {1, 2, , k} d13(0) = d13(1) = d13
(2)
Shortest path from i to j with intermediate vertices from {1, 2, , k} is a shortest path from i to j with intermediate vertices from {1, 2, , k - 1}
7 1 2 2 3
3 1 4 1
d13(3) = d13(4) =
k is not intermediary vertex of p1, p2 p1 and p2 are shortest paths from i to k with vertices
9
from {1,2,,k-1}
10
FLOYD-WARSHALL(W)
1. 2. 3. 4. 5. 6.
Example 3 1 -4 5 D(2) 1 2 3 4
11
2 4 8
D(0) = W 1 3 -5 2 3 4 5 4 4 1 5 -4 7 D(3) 0 2
1 0 2 3 0 4
2 3 0 4 8 0
3 8 0 -5 4 1 5 0 6
4 1 0 6 -4 7 11 -2 0
5 -4 7 0 0 3 7 2 8
D(1) 1 2 3 4 5 D(4)
1 0 2
2 3 0 4 5
3 8 0 -5
D(0) W for k 1 to n do for i 1 to n do for j 1 to n do dij(k) min (dij(k-1), dik(k-1) + dkj(k-1)) return D(n) Running time: (n3)
-4 1 0 6 -4 -1 3 -2 0 7 -2 0
2 7 6 3 8 0 -5 1 4
1 0 2
2 3 0 4 5
3 0 4
-1 -4 0
4 1 5 0 6
5 11 0 6 -2 0
-1 -5
-1 -5 5 1
Question
A Transitive Closure of a graph G =(V,E) is the graph G*=(V,E*) where E*={(i,j)/ there exist a path from i to j in G.} Given a graph G, use FloydWarshalls algorithm to compute the closure of G D B C A D B C A G*
13
14
Sparse graphs
What if the graph is sparse?
If no negative edges run repeated Dijkstras If negative edges let us somehow change the weights of all edges (to w) and then run repeated Dijkstras
15
16
Reweighting theorem
Rweighting does not change shortest paths
Let h: V R be any function For each (u,v)E, define w(u,v) = w(u,v) + h(u) h(v). Let p = (v1, v2, , vk) be any path from v1 to vk
s
0
-2
2. Compute the shortest paths from s in the augmented graph (using Belman-Ford). 3. Make h(v) = (s, v)
17 18
Johnsons algorithm
Why does it work?
By definition of the shortest path: for all edges (u,v), h(v) h(u) + w(u,v) Thus, w(u,v) + h(u) h(v) 0
Example, Analysis
Do the reweighting on this example:
0 0 1 3 -1 0
Johnsons algorithm:
1. 2.
s
0
-2
-1
3.
4.
Construct augmented graph Run Bellman-Ford (possibly report a negative cycle), to find h(v) = (s, v) for each vertex v Reweight all edges: w(u,v) w(u,v) + h(u) h(v). For each vertex u: Run Dijkstras from u, to find (u, v) for each v For each vertex v: D[u][v] (u, v) + h(v) h(u)
19
20