1 / 18

All-pairs Shortest Paths

All-pairs Shortest Paths. The structure of a shortest path: All subpaths of a shortest path are shortest paths. p : a shortest path from vertex i to vertex j with at most m edges. Decompose p as : i  k  j p’ has at most m-1 edges . (i, j) = (i, k) + w kj

maeve
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

  2. The structure of a shortest path: All subpaths of a shortest path are shortest paths. p : a shortest path from vertex i to vertex j with at most m edges. Decompose p as : i  k  j p’ has at most m-1 edges. (i, j) = (i, k) + wkj • A recursive sol. to the all-pairs shortest-paths problem : minimum weight of any path from vertex i to vertex j that contains at most m edges. P’

  3. Computing the shortest-path weights bottom up: W=(wij) = wij Let D(m)= ( ) Given D(m-1) and W, return D(m)

  4. All-pairs Shortest Paths: Input : G = (V,E), |V|= n W = (wij), Output : nn matrix D = (dij) di,j : the weight of a shortest path from vertex i to vertex j di,j = (i, j) at termination (Let (i, j) denote the shortest-path weight from vertex i to vertex j )  = (ij) : predecessor matrix No negative-weight cycle Adjacency-matrix j i ij

  5. Print-All-Pairs-Shortest-Path(, i, j) { if i=j then print I else if ij = NIL then print “no path from” i “to” j “exists” else Print-All-Pairs-Shortest-Path(, i, ij) print j } • Dynamic Programming: • Characterize the structure of an optimal sol. • Recursively define the value of an optimal sol. • Compute the value of an optimal sol in a bottom-up fashion

  6. Extend-Shortest-Paths(D,W) { n  rows[D] Let D’ = (dij’) be an nn matrix for i=1 to n for j=1 to n dij’   for k=1 to n dij’ = min (dij’ , dik + wkj) return D’ } • Slow-All-Pairs-Shortest-Paths(W) { n = rows[W] D(1) = W for m=2 to n-1 D(m) = Extend-Shortest-Paths(D(m-1) , W) return D(n-1) } Very similar to matrix-multiplication (n4)

  7. Faster-All-Pairs-Shortest-Paths(W) { n = rows[W] D(1) = W m = 1 While n-1>m D(2m) = Extend-Shortest-Paths(D(m), D(m)) m = 2m return D(m) } (n3logn) (Repeated squaring)

  8. Floyd-Warshall algorithm Structure of a shortest path: • Intermediate vertex of a simple path p=<v1 , …, vk> is any vertex of p other than v1 or vk, that is, any vertex in the set {v2 , …, vk-1} • Let V={1, …, n} and a subset {1, 2, …, k} for some k. For any pair of vertices i, j V, consider all paths from i to j whose intermediate vertices are all drawn from {1, 2, …, k} and let p be a minimum-weight path from among them.

  9. all int. vertices in {1, 2, …, k-1} all int. vertices in {1, 2, …, k-1} p1 p2 k i j p : all int. vertices in {1, 2, …, k} k : intermediate vertex of p  If k is not an intermediate vertex of path p, then all intermediate vertices of path p are in the set {1, 2, …, k-1}. Thus a shortest path from vertex i to vertex j with all intermediate vertices in the set {1, 2, …, k-1} is also a shortest path from i to j with all intermediate vertices in the set {1, 2, …, k} 

  10. A recursive solution to the all-pairs shortest-paths problem dij(k) : the weight of a shortest path from i to j with all intermediate vertices in the set {1, 2, …, k} • Computing the shortest-path weights bottom up Floyd-Warshall(W) { n = rows[W] D(0) = W for k=1 to n for i=1 to n for j=1 to n return D(n) } (n3)

  11. Constructing a shortest path: ij(k) : the predecessor os vertex j on a shortest path from i with all intermediate vertices in the set {1, 2, …, k}

  12. Transitive closure of a directed graph G=(V,E): • G*=(V, E*), E*={ (i, j): there is a path from i to j in G} • For i, j , k = 1, 2, …, n , define tij(k)=1, if  a path in G from i to j with all intermediate vertices in {1, 2, …, k}; otherwise tij(k)=0

  13. TC(G) { n = | G.V |; for i=1 to n for j=1 to n if i=j or (i, j)  G.E tij(0) = 1 else tij(0) = 0 for k=1 to n for i=1 to n for j=1 to n return T(n) }

  14. Preserving shortest paths by reweighting: , which must satisfy 2 properties: • For all pairs of vertices u, vV, a shortest path from u to v using weight function w is also a shortest path from u to v using weight function • For all edges (u, v), is non-negative

  15. Lemma: (Reweighting does not change shortest paths) Given G=(V, E) weighted, directed graph with weight function w: E  R , let h: V  R be any function mapping vertices to real numbers. For each (u,v) E, define Let p=<v0, …, vk>  Then  Also G has a negative cycle with w iff G has a negative cycle with pf:

  16.  Suppose there is a shorter path p’ from v0 to vk with Thus  Consider any cycle c=<v0, v1, …, vk> , where v0 = vk Thus c has negative weight using w iff it has negative weight using • Producing non-negative weights by reweighting: Given G=(V,E) with weight function w: ER , make G’=(V’, E’) , where V’=V{s} , E’=E{(s,v): vV} Extend w s.t. w(s,v)=0 for all vV Define h(v)=(s,v) for all vV’ For all edges (u,v)E’ , we have h(v)  h(u)+w(u,v) Thus

  17. Johnson’s algorithm for sparse graphs: Input : sparse graph in adj. list representation Output : either returns a matrix of shortest-path weights for all pairs or reports that the input graph contains a negative- weight cycle

  18. Johnson(G) { compute G’, where G’.V= G.V{s} and G’.E= G.E {(s,v):v G.V} if Bellman-Ford(G’, w, s)=False print “ a negative-weight cycle” else for each vertex v  G’.V set h(v)=(s,v) computed by the Bellman-Ford algo. for each edge (u,v)  G’.E for each vertex u  G.V run Dijkstra(G, , u) to compute for all v G.V for each v  G.V return D } O(V2logV+VE) Fibonacci heap O(VElogV) binary heap

More Related