1 / 26

Introduction to Algorithms

Introduction to Algorithms. Single-Source Shortest Paths My T. Thai @ UF. Single-Source Shortest Paths Problem. Input : A weighted, directed graph G = ( V , E ) and a source vertex s Weight (length) of a path is the total weight of edges on the path

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 Single-Source Shortest Paths My T. Thai @ UF

  2. Single-Source Shortest Paths Problem • Input: A weighted, directed graph G = (V, E) and a source vertex s • Weight (length) of a path is the total weight of edges on the path • Output: Shortest-path tree from s to each reachable destinations (similar to BFS tree) • Value at each vertex: the length of shortest path from s to it • Shaded edges show shortest paths My T. Thai mythai@cise.ufl.edu

  3. Outline • Main properties • Bellman-Ford algorithm • DAG algorithm • Input is directed acyclic graph • Dijkstra’s algorithm • No negative weight edges My T. Thai mythai@cise.ufl.edu

  4. Negative-weight edges and cycles • If we have a negative-weight cycle, we can just keep going around it and the total weight is -∞ • If the negative-weight cycle is not reachable from the source, it is fine • Shortest paths cannot contain cycles • Positive-weight ⇒we can get a shorter path by omitting the cycle • Zero-weight: no reason to use them ⇒ assume that our solutions won’t use them My T. Thai mythai@cise.ufl.edu

  5. Optimal substructure Proof: • Decompose p into then: • Assume the path from vi to vj such that: • The path has weight less than My T. Thai mythai@cise.ufl.edu

  6. Corollary: Let p = Shortest path from s to v, where p = s u v. Then, δ(s, v) = δ(s, u) + w(u, v) My T. Thai mythai@cise.ufl.edu

  7. Relaxation • v.d: upper bound (shortest-path estimate)on the weight of a shortest path from source s to v • v.: previous vertex of v on the path from s to v • These values are changed when an edge (u, v) is relaxed My T. Thai mythai@cise.ufl.edu

  8. Properties of Relaxation Proof: (by induction) • Initially true. • If a call to Relax(u, v, w) changes v.d, then : v.d = v.u + w(u, v)  (s, u) + w(u, v) (by the inductive hypothesis)  (s, v)(by the triangle inequality) My T. Thai mythai@cise.ufl.edu

  9. Proof: • After relaxing edge (u, v): v.du.d + w(u, v) ; Relax algorithm = (s, u) + w(u, v) ; u.d = (s, u) holds. = (s, v) ; by Lemma 24.1. • By Lemma 24.11, v.d(s, v) =>v.d=(s, v) My T. Thai mythai@cise.ufl.edu

  10. Proof: • Using induction to show that vi.d = δ(s, vi ) after (vi−1, vi ) is relaxed • Basis: i = 0. v0.d = 0 = δ(s, v0) = δ(s, s) • Assume vi−1.d = δ(s, vi−1). • Relax (vi−1, vi ) => vi.d = δ(s, vi ) and vi.d never changes. My T. Thai mythai@cise.ufl.edu

  11. Bellman-Ford algorithm • Allows negative-weight edges • Computes v.d and v.π for all v ∈ V • Returns TRUE if no negative-weight cycles reachable from s, FALSE otherwise My T. Thai mythai@cise.ufl.edu

  12. Bellman-Ford algorithm • Time: O(VE) • V – 1 for loop in line 2, each takes O(E) time My T. Thai mythai@cise.ufl.edu

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

  14. Proof of correctness • Case 1: G contains no negative cycles reachable from s • Let v be reachable from s , and let p = <v0, v1, …, vk> be a shortest path from s to v, where v0 = s and vk = v • p is acyclic => has ≤ |V| − 1 edges=> k ≤ |V| − 1 • Each iteration of the for loop relaxes all edges: • First iteration relaxes (v0, v1) • Second iteration relaxes (v1, v2) • kth iteration relaxes (vk−1, vk) • By path-relaxation property, v.d = vk.d = δ(s, vk ) = δ(s, v) • For all (u, v) ∈ E: v.d = δ(s, v)  δ(s, u) + w(u, v) = u.d + w(u, v)  return True My T. Thai mythai@cise.ufl.edu

  15. Proof of correctness • Case 2: G contains negative cycles reachable from s • Suppose there exists a cycle c= ‹v0, v1, …, vk›, where v0 = vkreachable from s • Suppose (for contradiction) that BELLMAN-FORD returns TRUE  • v0 = vk   Contradiction My T. Thai mythai@cise.ufl.edu

  16. Shortest paths in directed acyclic graphs • Time: • Correctness: • Order of edges on any path is an order of vertices in topologically sorted order  Edges on any shortest path are relaxed in order By path-relaxation property, correct My T. Thai mythai@cise.ufl.edu

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

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

  19. Dijkstra’s algorithm • Assumption: No negative-weight edges • a weighted version of breadth-first search, such as Prim’s algorithm • Have two sets of vertices: • S = vertices whose final shortest-path weights are determined • Q = priority queue = V − S (key isv.dfor each v ∈ Q) • Repeatedly selects u in V–S with minimum shortest path estimate (greedy choice) My T. Thai mythai@cise.ufl.edu

  20. Dijkstra’s algorithm • Time: • O(V2) using linear array for priority queue • O((V + E) lg V) using binary heap • O(V lg V + E) using Fibonacci heap My T. Thai mythai@cise.ufl.edu

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

  22. Correctness • Loop invariant: At the start of each iteration of the while loop, v.d =δ(s, v) for all v ∈ S • Initialization: Initially, S = ∅, so trivially true. • Termination: At the end, Q = ∅ ⇒ S = V ⇒ v.d = δ(s, v) for all v ∈ V My T. Thai mythai@cise.ufl.edu

  23. Maintenance:we show that u.d = δ(s, u) when u is added to S in each iteration • Suppose not, let u be the first vertex such that u.d (s, u) when inserted in S • s.d = (s, s) = 0 when s is inserted, so u  s  S   before u is inserted • There must be some path from s to u, otherwise u.d = δ(s, u) = ∞ byno-path property My T. Thai mythai@cise.ufl.edu

  24. The shortest path from s to u shown in the figure • y the first vertex after the path leaves S • x∈ S  x.d = (s, x) • (x, y) was relaxed before  y.d = (s, y)  But u is inserted before y. Thus: u.d = y.d = (s, u). Contradiction My T. Thai mythai@cise.ufl.edu

  25. Variants of shortest paths problem • Single-source: Find shortest paths from a given source vertex s ∈ V to every vertex v ∈ V • Single-destination: Find shortest paths to a given destination vertex (use single source algorithm on reversed graph) • Single-pair: Find shortest path from u to v. No way known that is better in worst case than solving single-source • All-pairs: Find shortest path from u to v for all u, v ∈ V (Next lecture) My T. Thai mythai@cise.ufl.edu

  26. Summary • Shortest path problem has optimal substructure • Bellman-Ford algorithm uses dynamic programming approach • Can detect negative weight cycle reachable from the source • Time: O(VE) • On Directed Acyclic Graphs: use topologically sorted order to reduce time to • Dijkstra’s algorithm is a weighted version of breadth-first search with a greedy choice • Require all edges to have nonnegative weight • Time: O((V + E) lg V) using binary heap My T. Thai mythai@cise.ufl.edu

More Related