1 / 39

All-Pairs Shortest Paths

All-Pairs Shortest Paths. Find the distance between every pair of vertices in a weighted graph G. From A one may reach C faster by reaching by traveling from A to B and then from B to C. All-Pairs Shortest Paths. We can make n calls to Dijkstra’s algorithm O(n(n+m)log n) time.

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 • Find the distance between every pair of vertices in a weighted graph G.

  2. From A one may reach C faster by reaching by traveling from A to B and then from B to C. All-Pairs Shortest Paths • We can make n calls to Dijkstra’s algorithm • O(n(n+m)log n) time • Alternatively ... Use dynamic programming: The Floyd’s Algorithm • O(n3) time.

  3. Floyd’s Shortest Path Algorithm • Idea #1: Number the vertices 1, 2, …, n. • Idea #2: Shortest path between vertex i and vertex j without passing through any other vertex is weight(edge(i,j)). • Let it be D0(i,j). • Idea #3: If Dk(i,j) is the shortest path between vertex i and vertex j using vertices numbered 1, 2, …, k, as intermediate vertices, then Dn(i,j) is the solution to the shortest path problem between vertex i and vertex j.

  4. Uses only vertices numbered 1,…,k (compute weight of this edge) i j Uses only vertices numbered 1,…,k-1 Uses only vertices numbered 1,…,k-1 k Floyd’s Shortest Path Algorithm • Assume that we have Dk-1(i,j) for all i, and j. • We wish to compute Dk(i,j). • What are the possibilities? Choose between • a path through vertex k. • Path Length: Dk(i,j) = Dk-1(i,k) + Dk-1(k,j). • (2) Skip vertex k altogether. • Path Length: Dk(i,j) = Dk-1(i,j).

  5. Floyd’s Shortest Path Algorithm Uses only vertices numbered 1,…,k (compute weight of this edge) i j Uses only vertices numbered 1,…,k-1 k Uses only vertices numbered 1,…,k-1

  6. Floyd’s Shortest Path Algorithm Example: 8 D0 1 2 4 3 1 3 2 1 1 5 4 8

  7. Floyd’s Shortest Path Algorithm Example: 8 D1 1 2 4 3 1 3 2 1 1 5 4 8

  8. Floyd’s Shortest Path Algorithm Example: 8 D2 1 2 4 3 1 3 2 1 1 5 4 8

  9. Floyd’s Shortest Path Algorithm Example: 8 D3 1 2 4 3 1 3 2 1 1 5 4 8

  10. Floyd’s Shortest Path Algorithm Example: 8 D4 1 2 4 3 1 3 2 1 1 5 4 8

  11. Floyd’s Shortest Path Algorithm Example: 8 D5 1 2 4 3 1 3 2 1 1 5 4 8

  12. Floyd’s Shortest Path Algorithm AlgorithmAllPair(G) {assumes vertices 1,…,n} for allvertex pairs (i,j) ifi= j d0[i,i] 0 else if (i,j)is an edge in G d0[i,j] weight of edge (i,j) elsed0[i,j] +  fork 1 to n do fori 1 to n do forj 1 to n do dk[i,j] min(dk-1[i,j], dk-1[i,k]+dk-1[k,j]) O(n+m) or O(n2) O(n3)

  13. Floyd’s Shortest Path Algorithm Observation: dk [i,k] = dk-1[i,k] dk[k,j] = dk-1[k,j]

  14. Floyd’s Shortest Path Algorithm AlgorithmAllPair(G) {assumes vertices 1,…,n} for allvertex pairs (i,j) ifi= j d[i,i] 0 else if (i,j)is an edge in G d[i,j] weight of edge (i,j) elsed[i,j] +  fork 1 to n do fori 1 to n do forj 1 to n do d[i,j] min(d[i,j], d[i,k]+d[k,j])

  15. Floyd’s Shortest Path Algorithm AlgorithmAllPair(G) {assumes vertices 1,…,n} for allvertex pairs (i,j) path[i,j] null ifi= j d[i,i] 0 else if (i,j)is an edge in G d[i,j] weight of edge (i,j) elsed[i,j] +  fork 1 to n do fori 1 to n do forj 1 to n do if (d[i,k]+d[k,j] < d[i,j]) path[i,j] vertexk d[i,j] d[i,k]+d[k,j]

  16. Floyd’s Shortest Path Algorithm • Can be applied to Directed Graphs • Can accept –ve edges as long as there are no –ve cycles.

  17. Transitive Closure • Transitive Relation: A relation R on a set A is called transitive if and only if for any a, b, and c in A, whenever <a, b>  R , and <b, c>  R ,<a, c> R

  18. D E B C A Transitive Closure D E • Given a graph G, the transitive closure of G is the G* such that • G* has the same vertices as G • if G has a path from u to v (u  v), G* has a edge from u to v B G C A The transitive closure provides reachability information about a graph. G*

  19. If there's a way to get from A to B and from B to C, then there's a way to get from A to C. Computing the Transitive Closure • We can perform BFS starting at each vertex • O(n(n+m)) Alternatively ... Use dynamic programming: Warshall’s Algorithm

  20. Floyd-Warshall Transitive Closure • Idea #1: Number the vertices 1, 2, …, n. • Idea #2: Consider paths that use only vertices numbered 1, 2, …, k, as intermediate vertices: Uses only vertices numbered 1,…,k (add this edge if it’s not already in) i j Uses only vertices numbered 1,…,k-1 Uses only vertices numbered 1,…,k-1 k

  21. Warshall’s Transitive Closure Example: A0 1 2 3 4

  22. Warshall’s Transitive Closure Example: A1 1 2 3 4

  23. Warshall’s Transitive Closure Example: A2 1 2 3 4

  24. Warshall’s Transitive Closure Example: A3 1 2 3 4

  25. Warshall’s Transitive Closure Example: A4 1 2 3 4

  26. Warshall’s Algorithm AlgorithmFloydWarshall(G) Inputgraph G Outputtransitive closure G* of G i 1 for all v  G.vertices() denote v as vi i  i+1 G0G for k 1 to n do GkGk -1 for i 1 to n (i  k)do for j 1 to n (j  i, k)do if Gk -1.areAdjacent(vi, vk)  Gk -1.areAdjacent(vk, vj) if Gk.areAdjacent(vi, vj) Gk.insertEdge(vi, vj , k) return Gn • Warshall’s algorithm numbers the vertices of G as v1 , …, vn and computes a series of graphs G0, …, Gn • G0=G • Gkhas a edge (vi, vj) if G has a path from vi to vjwith intermediate vertices in the set {v1 , …, vk} • We have that Gn = G* • In phase k, graph Gk is computed from Gk -1 • Running time: O(n3), assuming areAdjacent is O(1) (e.g., adjacency matrix)

  27. Shortest Path Algorithm for DAGs • Use Dijkstra’s algorithm • Time: O((n+m) log n) • Use Topological sort based algorithm • Time: O(n+m).

  28. Directed Graph With –ve weight $170 PVD -$100 ORD $28  SFO   LGA -$100 -$200 $261 $68 $277  $511 HNL $210 $246 LAX $224 DFW  MCO   0

  29. Directed Graph With –ve weight $170 PVD 3 -$100 ORD 4 $28  SFO 8   LGA 2 -$100 -$200 $261 $68 $277  $511 HNL 7 $210 $246 LAX 6 $224 DFW 5  MCO 1   0

  30. Directed Graph With –ve weight $170 PVD 3 -$100 ORD 4 $28 SFO 8 $224   LGA 2 -$100 -$200 $261 $68 $277 $511 HNL 7 $210 $210 $246 LAX 6 $224 DFW 5  MCO 1  $224 0

  31. Directed Graph With –ve weight $170 PVD 3 -$100 ORD 4 $28 SFO 8 $261   LGA 2 -$100 -$200 $261 $68 $277 $511 HNL 7 $210 $210 $246 LAX 6 $224 DFW 5  MCO 1  $224 0

  32. Directed Graph With –ve weight $170 PVD 3 -$100 ORD 4 $28 SFO 8 $238   LGA 2 -$100 -$200 $261 $68 $277 $511 HNL 7 $210 $210 $246 LAX 6 $224 DFW 5  MCO 1  $224 0

  33. Directed Graph With –ve weight $170 PVD 3 -$100 ORD 4 $28 SFO 8 $238 $408  LGA 2 -$100 -$200 $261 $68 $277 $511 HNL 7 $210 $210 $246 LAX 6 $224 DFW 5  MCO 1  $224 0

  34. Directed Graph With –ve weight $170 PVD 3 -$100 ORD 4 $28 SFO 8 $238 $408 LGA 2 -$100 $308 -$200 $261 $68 $277 $511 HNL 7 $210 $210 $246 LAX 6 $224 DFW 5  MCO 1 $208 $224 0

  35. Directed Graph With –ve weight $170 PVD 3 -$100 ORD 4 $28 SFO 8 $238 $408 LGA 2 -$100 $308 -$200 $261 $68 $277 $511 HNL 7 $210 $210 $246 LAX 6 $224 DFW 5  MCO 1 $208 $224 0

  36. Directed Graph With –ve weight $170 PVD 3 -$100 ORD 4 $28 SFO 8 $238 $408 LGA 2 -$100 $268 -$200 $261 $68 $277 $511 HNL 7 $210 $210 $246 LAX 6 $224 DFW 5 $719 MCO 1 $208 $224 0

  37. Directed Graph With –ve weight $170 PVD 3 -$100 ORD 4 $28 SFO 8 $238 $408 LGA 2 -$100 $268 -$200 $261 $68 $277 $511 HNL 7 $210 $210 $246 LAX 6 $224 DFW 5 $719 MCO 1 $208 $224 0

  38. Directed Graph With –ve weight $170 PVD 3 -$100 ORD 4 $28 SFO 8 $238 $408 LGA 2 -$100 $268 -$200 $261 $68 $277 $511 HNL 7 $210 $210 $246 LAX 6 $224 DFW 5 $719 MCO 1 $208 $224 0

  39. DAG-based Algorithm AlgorithmDagDistances(G, s) for allv  G.vertices() v.parent null ifv= s v.distance 0 else v.distance  Perform a topological sort of the vertices foru 1 to n do{in topological order} for eache  G.outEdges(u) w  G.opposite(u,e) r  getDistance(u) + weight(e) ifr< getDistance(w) w.distance  r w.parent  u • Works even with negative-weight edges • Uses topological order • Doesn’t use any fancy data structures • Is much faster than Dijkstra’s algorithm • Running time: O(n+m).

More Related