You are on page 1of 5

Algorithms

Dana Shapira Lesson #8: All-Pairs Shortest Paths

All-Pairs Shortest Paths


2

Given: Directed graph G = (V, E) Weight function w : E R Compute:


1
-4

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)

All-Pairs Shortest Paths Solutions


Run BELLMAN-FORD once from each vertex: O(|V|2|E|), which is O(|V|4) if the graph is dense If no negative-weight edges, could run Dijkstras algorithm once from each vertex: O(|V|3) if the graph is dense. O(|V||E|lg|V|) if the graph is sparse.

All-Pairs Shortest Paths

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

dij(0) = wij dij(k) = min {dij(k-1) , dik(k-1) + dkj(k-1) }

Dik(k-1)
i
7

Dkj(k-1)
j

Dijk

intermediate nodes in {1, 2, ..., k}


8

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)

The Structure of a Shortest Path


k is not an intermediate vertex of path p
k i j

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

k is an intermediate vertex of path p p1 is a shortest path from i to k p2 is a shortest path from k to j


i p1 k p2 j

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

dij(k) = min {dij(k-1) , dik(k-1) + dkj(k-1) }

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

Summary- Single Source Shortest Path


E G Type of Graph No weights Non negative weights Weighted graph E Algorithm BFS Dijkstra Belman-Ford Time O(|V|+|E|) O(|V|2)/O(|E|log| V|) O(|V||E|)

14

Summary- All-Pairs Shortest Paths


Type of Graph No weights Non negative weights Weighted graph Weighted graphs No negative cycles Algorithm |V| times BFS |V| times Dijkstra |V| times Belman-Ford Floyd-Warshall Time O(|V|(|E|+|V|)) O(|V|3)/O(|V||E|log| V|) O(|V|2|E|) O(|V|3)

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

Requirements for reweighting:


Non-negativity: for each (u,v), w(u,v) 0 Shortest-path equivalence: for all pairs of vertices u and v, a path p is a shortest path from u to v using weights w if and only if p is a shortest path from u to v using weights w.

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

Choosing reweighting function


How to choose function h?
The idea of Johnson: 1. Augment the graph by adding vertex s and edges (s,v) for each vertex v with 0 weights.
0 0 1 3 -1 0 -1

s
0

-2

Then: w(p)= (v1, vk) w(p)= (v1, vk)

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

What is the running time of Johnsons?

20

You might also like