1 / 17

All-Pairs Shortest Paths

All-Pairs Shortest Paths. Problem Given a graph G = ( V , E ), where every edge e has weight w ( e ), find, for every pair of vertices u , v in V , a shortest path from u to v. Input: an adjacency matrix. Output in tabular form : the entry in u ’s row and v ’s

bruis
Download Presentation

All-Pairs Shortest Paths

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. All-Pairs Shortest Paths ProblemGiven a graph G = (V, E), where every edge e has weight w(e), find, for every pair of vertices u, v in V, a shortest path from u to v. Input: anadjacency matrix. Output intabular form: the entry in u’s row and v’s column is the length of the shortest path from u to v. Negative weights allowed. (Dijkstra’s algorithm no longer applicable.) No negative-weight cycles.

  2. n n matrix W = (w ) where ij 0 if i = j w = weight of (i, j) if i j & (i, j) in E  otherwise ij 0 3 8  -4  0  1 7  4 0  2  -5 0   6 0 An Example Input 2 4 3 8 3 1 7 1 2 -4 -5 5 4 6

  3. Path: 1- 5 - 4 - 3 - 2 = – 4 + 6 – 5 + 4 4 - 1- 5 5 - 4 - 1 Cont’d Output 2 4 3 8 3 1 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 7 1 2 -4 -5 5 4 6

  4. (m) d = weight of shortest path from i to j that uses at most m edges ij 0 if i = j  if i j (0) d = ij k’s (k≠ i, j) m  1 edges m  1 edges A Recursive Definition i j

  5. (m-1) (m) d = min { d + w } ij ik kj 1  k  n for k 1 ton do ifd > d + w thend  d + w k’s ij ik kj ij ik kj m  1 edges i j (n 1) (n) (n+1) no cycle on a shortest path d = d = d = ... m  1 edges ij ij ij Relaxation (k≠ i, j) Length of the shortest path from i to j is

  6. (n – 1) (1) 3 n  1 calls to ESP to compute D , …, D , each taking (n ). 4 (n )total time Dynamic Programming (l) (l) (l – 1) Extend-Shortest-Paths(n,W) // compute D = (d ) from D let D = (d ) be an nn matrix fori 1 ton do forj  1 ton dod fork 1 ton dod min (d , d + w ) returnD ij ij ij ij ij ik kj

  7. j = 1, …, n d = min (d , d+ w ) ij ij ik kj d = d+dw ij ij ik kj (m) (m – 1) As if D = D “” W m = 1, …, n –1 where 0  …   0  …  …  … 0 (0) D = (n – 1) n – 1 And D = W contains the shortest-path weights. Matrix Multiplication Extend-Shortest-Paths( ) is similar to matrix multiplication: correspondence

  8. Repeated squaring 2kkk W = WW Compute lg(n 1) 2 4 2 W, W , W , … W lg(n 1) + 1 3 (n lg n)time: (lg n) squarings each squaring (n ) time 3 An Improvement

  9. (m) d = weight of a shortest path from i to j with intermediate vertices in { 1, 2, …, m }. ij i mm m ... j (n) Then the length of shortest path from i to j is d ij Floyd-Warshall Algorithm Even faster by a factor of lgn!

  10. (0) d = w i j ij ij (k) (k-1) (k-1) (k-1) d = min ( d , d + d ) if k 1 ij ij ik kj (k-1) (k-1) d d k kj ik j i (k-1) d ij Recursive Definition of d

  11. The Procedure Floyd-Warshall(n, W) DW fork  1 ton do fori  1 ton do forj 1 ton d  min (d , d + d ) returnD (0) (k) (k-1) (k-1) (k-1) ij ij ik kj (n) 3 Running time (n )!

  12. (k)  j i ij (0)  = ij (k-1) (k-1) (k-1) (k-1)  if d  d + d ij kj ij ik (k)  = i ij (k-1) j  otherwise kj k Constructing Shortest Paths (k) (k) Predecessor matrix  = ( ) : ij shortest path from i to j with all intermediate vertices in {1, 2, …, k}. i, if (i, j)  E NIL, if (i, j)  E

  13. 0 3 5   0 1 6   0 2 4   0 (0) D (0) NIL 1 1 NIL NIL NIL 2 2 NIL NIL NIL 3 4 NIL NIL NIL  0 3 5   0 1 6   0 2 4 79 0 (1) NIL 1 1 NIL NIL NIL 2 2 NIL NIL NIL 3 4 11 NIL (1)  D An Example 3 1 2 1 5 6 2 3 4 4 With 1 as a possible intermediate vertex:

  14. 0 3 49  0 1 6   0 2 4 7 8 0 NIL 1 2 2 NIL NIL 2 2 NIL NIL NIL 3 412 NIL (2) (2)  D 0 3 4 6  0 1 3   0 2 4 7 8 0 NIL 1 2 3 NIL NIL 2 3 NIL NIL NIL 3 4 1 2 NIL (3) (3) D  NIL 1 2 3 4 NIL 2 3 4 1 NIL 3 4 1 2 NIL 0 3 4 6 7 0 1 3 69 0 2 4 7 8 0 (4) (4)  D An Example (cont’d)

  15. An Example (cont’d) 4 3 2 1 2 3 4 1 2 4 3 3 1 1 2 3 3 4 1 2 2 2 3 1 1 3 4 2 3 4

  16. Transitive Closure Thetransitive closureof a graph G = (V, E) is G* = (V, E*) such that (i, j) E* iff there is a path from i to j in G. G*: G: 1 1 5 2 2 5 3 4 3 4

  17. 1 if i = j or an edge between vertices i and j entry (i , j) = 0 otherwise if i j Floyd-Warshall algorithm, replacing x y min boolean OR + boolean AND i j 3 Running time(n ) Solution: Floyd-Warshall Adjacency matrix is initialized as (weights not needed) AND AND OR OR

More Related