1 / 24

Introduction to Algorithms

Introduction to Algorithms. All-Pairs Shortest Paths My T. Thai @ UF. Single-Source Shortest Paths Problem. Input : A weighted, directed graph G = ( V , E ) Output : An n × n matrix of shortest-path distances δ . δ ( i , j ) is the weight of a shortest path from i to j.

Download Presentation

Introduction to Algorithms

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. Introduction to Algorithms All-Pairs Shortest Paths My T. Thai @ UF

  2. Single-Source Shortest Paths Problem • Input: A weighted, directed graph G = (V, E) • Output: An n × n matrix of shortest-path distances δ. δ(i, j) is the weight of a shortest path from i to j. My T. Thai mythai@cise.ufl.edu

  3. Can use algorithms for Single-Source Shortest Paths • Run BELLMAN-FORD once from each vertex • Time: • If there are no negative-weight edges, could run Dijkstra’s algorithm once from each vertex • Time: My T. Thai mythai@cise.ufl.edu

  4. Outline • Shortest paths and matrix multiplication • Floyd-Warshall algorithm • Johnson’s algorithm My T. Thai mythai@cise.ufl.edu

  5. Recursive solution • Optimal substructure: subpaths of shortest paths are shortest paths • Recursive solution: Let = weight of shortest path from i to j that contains ≤ m edges. Where wij: My T. Thai mythai@cise.ufl.edu

  6. Computing the shortest-path weights bottom up • All simple shortest paths contain ≤ n − 1 edges • Compute from bottom up: L(1), L(2), . . . , L(n-1). • Compute L(i+1) from L(i)by extending one more edge Time: My T. Thai mythai@cise.ufl.edu

  7. Time: My T. Thai mythai@cise.ufl.edu

  8. Shortest paths and matrix multiplication • Extending shortest paths by one more edge likes matrix product: L(i+1)= L(i).W • Compute L(1), L(2), L(4) . . . , L(r)with Time: My T. Thai mythai@cise.ufl.edu

  9. Example My T. Thai mythai@cise.ufl.edu

  10. Floyd-Warshall algorithm • For path p = <v1, v2, . . . , vl> , v2 … vl-1areintermediate vertices fromv1to vl • Define = shortest-path weight of any path from i to j with all intermediate vertices in {1, 2, . . . , k} • Consider a shortest path with all intermediate vertices in{1, 2, . . . , k}: • If k is not an intermediate vertex, all intermediate vertices in{1, 2, . . . , k -1} • If k is an intermediate vertex: mythai@cise.ufl.edu

  11. Floyd-Warshall algorithm • Recursive formula: • Time: Note:since we have at most n vertices, return My T. Thai mythai@cise.ufl.edu

  12. Constructing a shortest path • is the predecessor of vertex j on a shortest path from vertex i with all intermediate vertices in the set {1, 2, . . . , k} (Use vertex k) My T. Thai mythai@cise.ufl.edu

  13. Example My T. Thai mythai@cise.ufl.edu

  14. My T. Thai mythai@cise.ufl.edu

  15. My T. Thai mythai@cise.ufl.edu

  16. Johnson’s algorithm • Reweighting edges to get non-negative weight edges: • For all u, v ∈ V, p is a shortest path using w if and only if p is a shortest path using • For all (u, v) ∈ E, • Run Dijkstra’s algorithm once from each vertex My T. Thai mythai@cise.ufl.edu

  17. Reweighting My T. Thai mythai@cise.ufl.edu

  18. Proof of lemma 25.1 • First, we prove • With cycle , My T. Thai mythai@cise.ufl.edu

  19. Producing nonnegative weights • Construct • Since no edges enter s, has the same set of cycles as G • has a negative-weight cycle if and only if G does • Define: • Claim: • Proof: Triangle inequality of shortest paths My T. Thai mythai@cise.ufl.edu

  20. Johnson’s algorithm Time: My T. Thai mythai@cise.ufl.edu

  21. Example My T. Thai mythai@cise.ufl.edu

  22. My T. Thai mythai@cise.ufl.edu

  23. Summary • Dynamic-programming algorithm based on matrix multiplication • Define sub-optimal solutions based on the length of paths • Use the technique of “repeated squaring” • Time: • Floyd-Warshall algorithm • Define sub-optimal solutions based on the set of allowed intermediate vertices • Time: My T. Thai mythai@cise.ufl.edu

  24. Johnson’s algorithm • Reweight edges to non-negative weight edges • Run Dijkstra’s algorithm once from each vertex • Time: • Faster than Floyd-Warshall algorithm when the graph is dense E = o(V2) My T. Thai mythai@cise.ufl.edu

More Related