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 [email protected] 2 Outline Main properties Bellman-Ford algorithm DAG algorithm Input is directed acyclic graph Dijkstra’s algorithm No negative weight edges My T. Thai [email protected] 3 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 [email protected] 4 Optimal substructure Proof: Decompose p into Assume the path then: from vi to vj such that: The path has weight less than My T. Thai [email protected] 5 Corollary: Let p = Shortest path from s to v, where p' p= s u v. Then, δ(s, v) = δ(s, u) + w(u, v) My T. Thai [email protected] 6 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 [email protected] 7 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 [email protected] 8 Proof: After relaxing edge (u, v): v.d u.d + w(u, v) = (s, u) + w(u, v) = (s, v) ; Relax algorithm ; u.d = (s, u) holds. ; by Lemma 24.1. By Lemma 24.11, v.d (s, v) => v.d = (s, v) My T. Thai [email protected] 9 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 [email protected] 10 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 [email protected] 11 Bellman-Ford algorithm Time: O(VE) V – 1 for loop in line 2, each takes O(E) time My T. Thai [email protected] 12 Example My T. Thai [email protected] 13 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 [email protected] 14 Proof of correctness Case 2: G contains negative cycles reachable from s Suppose there exists a cycle c = ‹v0, v1, …, vk›, where v0 = vk reachable from s Suppose (for contradiction) that BELLMAN-FORD returns TRUE v0 = vk My T. Thai [email protected] Contradiction 15 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 [email protected] 16 Example My T. Thai [email protected] 17 My T. Thai [email protected] 18 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 is v.d for each v ∈ Q) Repeatedly selects u in V–S with minimum shortest path estimate (greedy choice) My T. Thai [email protected] 19 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 [email protected] 20 Example My T. Thai [email protected] 21 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 [email protected] 22 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) = ∞ by no-path property My T. Thai [email protected] 23 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 [email protected] 24 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 [email protected] 25 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 [email protected] 26
© Copyright 2026 Paperzz