260 likes | 386 Views
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
E N D
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 • 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
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
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
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
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
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
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
Proof: • After relaxing edge (u, v): v.du.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
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
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
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
Example My T. Thai mythai@cise.ufl.edu
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
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
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
Example My T. Thai mythai@cise.ufl.edu
My T. Thai mythai@cise.ufl.edu
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
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
Example My T. Thai mythai@cise.ufl.edu
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
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
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
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
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