Single-Source Shortest- paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=<v 0,v 1,…,v k ) : path.

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
November 14, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Aalborg University
CS138A Single Source Shortest Paths Peter Schröder.
Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=
 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: CLRS “Intro.
chapter Single-Source Shortest Paths Problem Definition Shortest paths and Relaxation Dijkstra’s algorithm (can be viewed as a greedy algorithm)
1 Chapter 22: Elementary Graph Algorithms IV. 2 About this lecture Review of Strongly Connected Components (SCC) in a directed graph Finding all SCC (i.e.,
Length of a Path The weight (length) of a path is the sum of the weights of its edges. adcbe Path p: Edge weights: w(a, b) = 7, w(b, c) = 2, w(c,
Introduction to Algorithms 6.046J/18.401J/SMA5503
Chapter 25: All-Pairs Shortest-Paths
Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints.
CS420 lecture twelve Shortest Paths wim bohm cs csu.
CSE 780 Algorithms Advanced Algorithms Shortest path Shortest path tree Relaxation Bellman-Ford Alg.
Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
Shortest Path Problems
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
Shortest Paths Definitions Single Source Algorithms
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
Analysis of Algorithms CS 477/677 Shortest Paths Instructor: George Bebis Chapter 24.
1 Graph Algorithms Single source shortest paths problem Dana Shapira.
DAST 2005 Tirgul 13 (and more) sample questions. DAST 2005 (back to previous week) We’ve seen that solving the shortest paths problem requires O(VE) time.
Lecture 8 Shortest Path Shortest-path problems
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
CS 473 All Pairs Shortest Paths1 CS473 – Algorithms I All Pairs Shortest Paths.
TECH Computer Science Graph Optimization Problems and Greedy Algorithms Greedy Algorithms  // Make the best choice now! Optimization Problems  Minimizing.
SINGLE-SOURCE SHORTEST PATHS
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Topological Sorting and Least-cost Path Algorithms.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
David Luebke 1 9/10/2015 CS 332: Algorithms Single-Source Shortest Path.
David Luebke 1 9/10/2015 ITCS 6114 Single-Source Shortest Path.
David Luebke 1 9/13/2015 CS 332: Algorithms S-S Shortest Path: Dijkstra’s Algorithm Disjoint-Set Union Amortized Analysis.
COSC 3101NJ. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.995x R 2 = Old Mark New Mark.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
1 Shortest Path Problems How can we find the shortest route between two points on a road map? Model the problem as a graph problem: –Road map is a weighted.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
Introduction to Algorithms Jiafen Liu Sept
The single-source shortest path problem (SSSP) input: a graph G = (V, E) with edge weights, and a specific source node s. goal: find a minimum weight (shortest)
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.
Lecture 13 Algorithm Analysis
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
Introduction to Algorithms All-Pairs Shortest Paths My T. UF.
Single Source Shortest Paths Chapter 24 CSc 4520/6520 Fall 2013 Slides adapted from George Bebis, University of Reno, Nevada.
Shortest paths Given: A single source vertex (given s) in a weighted, directed graph. Want to compute a shortest path for each possible destination. (Single.
Single-Source Shortest Paths (25/24) HW: 25-2 and 25-3 p. 546/24-2 and 24-3 p.615 Given a graph G=(V,E) and w: E   weight of is w(p) =  w(v[i],v[i+1])
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
David Luebke 1 11/21/2016 CS 332: Algorithms Minimum Spanning Tree Shortest Paths.
Algorithms and Data Structures Lecture XIII
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
CS 583 Analysis of Algorithms
Advanced Algorithms Analysis and Design
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
CSC 413/513: Intro to Algorithms
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Advanced Algorithms Analysis and Design
CS 3013: DS & Algorithms Shortest Paths.
Advanced Algorithms Analysis and Design
Presentation transcript:

Single-Source Shortest- paths

p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=<v 0,v 1,…,v k ) : path form v 0 to v k. Shortest-path weight from u to v :

p3. Shortest-paths tree rooted at s : 1. V’ is the set of vertices reachable from s in G. 2. G’ forms a rooted tree with root s. 3. the unique simple path from s to v in G’ is a shortest path from s to v in G. Predecessor subgraph 台北 新竹 台中 嘉義 高雄 台東 花蓮 宜蘭 台南 埔里

Optimal substructure of a shortest path : Lemma 1 (Subpaths of shortest paths are shortest paths) Given a weighted, dircted graph G=(V,E) with weight function w:E  R, let p= be a shortest path from v 1 to v k and, for any i and j such that let p ij = be the subpath of p from v i to v j. Then, p ij is a shortest path from v i to v j. v1v1 vkvk vivi vjvj Proof : Decompose p into Suppose there is a path p’ ij from v i to v j with w(p’ ij )<w(p ij ). Then w(p)>w(p 1j )+w(p’ ij )+w(p jk ). P is not shortest !

Cor : Let p be a shortest path from s to a vertex v. p ’ is a subpath of p. Then the shortest path from s to v has weight Proof : By the above lemma, p’ is a shortest path froms to u. Thus,

p6. Relaxation : v.d : upper bound on the weight of a shortest path from source s to v. : the predecessor of v along a shortest path from s to v. Initialize-Single-Source(G,s) { for each vertex do{ v.d= Relax(u,v,w) { if { Eg. 5 9 S uv 2 Relax(u,v) 5 7 uv uv uv 2 v.d w(u,v)u.d S v u

p7. Properties of shortest paths and relaxation : Triangle inequality : For all edge (u, v) in E,  (s, v)   (s, u) + w(u, v) Upper-bound property: v.d ≥  (s, v) for all v and once v.d =  (s, v) it never changes.  No-path property: If there is no path from s to v, then v.d =  (s, v) = .  Convergence property: If s ---  u  v is a shortest path in G, and if u.d =  (s, u) prior to relaxing (u, v), then v.d =  (s, v) afterward.

p8. Properties of shortest paths and relaxation :  Path-relaxation property: p= is a shortest path from s=v 0 to v k, and the edges of p are relaxed in the order (v 0, v 1 ), (v 1, v 2 ),…,(v k-1, v k ), then v k.d=  (s, v k ).  Predecessor-subgraph property: Once v.d =  (s, v) for all v, the predecessor subgraph is a shortest-paths tree rooted at s.

p9. Bellman-Ford algorithm : Allow edge weights to be negative Return TRUE iff the graph contains no negative cycle that are reachable from s. Bellman-Ford(G,w,s) {Initialize-Single-Source(G,s) }

p10. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Lemma 2 : At the termination of Bellman-Ford, we have for all v that are reachable from s. Corollary 3 : For each vertex, there is a path from s to v iff Bellman-Ford terminates with when it is run on G. Proof : Let v be a vertex reachable from s. By induction, for i=0,…,k, we have after the i-th pass and this equality is maintained thereafter. Note that there are |V|-1 passes. Basis : Inductive step : Assume after the (i-1)st pass. (v i-1,v i ) is relaxed in the i-th pass, by the path-relaxation property we have after the i-th pass. Proof : Similar to lemma 2. Exercise!

Thm 4 : (Correctness of the Bellman-Ford algorithm) The algorithm returns TRUE and we have and the predecessor subgraph is a shortest-paths tree rooted at s.---- when no negative cycle. If G does contain a negative-weight cycle reachable from s, then the algorithm returns False. Proof : (1) G has no negative cycle reachable from s : (2) (1) a. v : reachable from s. By lemma 2, b. v : not reachable from s. With lemma 17 is a shortest-paths tree (Predecessor-subgraph property). At termination, for all Thus, the algorithm returns TRUE. (2)G has a negative cycle reachable from s. Assume the algorithm returned TRUE, i.e.

p13. Single-source shortest paths in DAGs : DAG-Shortest-Paths(G,w,s) 1.Topologically sort the vertices of G. 2.Initialize-Single-Source(G,s). 3.For each vertex u taken in topologically sorted order 4. for each vertex 5. Relax(u,v,w).

p14. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

p15. Thm 5 : G : a dag with source s. At termination of the above procedure, for all is a shortest-paths tree. Proof : v : not reachable from s : then (1) By a simple induction using lemma 15 (Path-relaxation property), shows at termination for i=0,1,…,k. By lemma 17 (Predecessor-subgraph property), is a shortest-paths tree. (2) v : reachable from s : S=v 0 v1v1 v k-1 v k =v

p16. Dijkstra’s algorithm : Algorithm : G=(V,E), Q : priority queue with d as the key. Dijsktra(G,w,s) {Initialize-Single-Source(G,s) u=Extract-Min(Q) { } }

0s u x v y s u x v y s u x v y s u x v y s u x v y s u x v y

Theorem 6 : (Correctness of Dijkstra’s algorithm) If we run Dijkstra ’ s algorithm on a weighted, directed graph G=(V,E) with nonnegative weight function w and source s, then at termination, for all vertices Proof : Want to show : for each at the time when u is inserted into S, and maintains thereafter. By contradiction, let u be the first vertex for which when it is inserted into S., when u is inserted into S. Claim :, when u is inserted into S. Since both u and y were in V-S. When u was chosen in the algorithm, we have Thus, There must be a path Thus there is a shortest path p from s to u. s p u s y x u p1p1 p2p2 : the first one in V-S along p when x was inserted into S. When u is inserted into S, (x,y) had been relaxed, thus Contradict the assumption that

p19. Analysis : Priority queue : Time : Linear array Binary heap Fibonacci heap Corollary 7: If we run Dijkstra ’ s algorithm on a weighted, directed graph G=(V,E) with nonnegative weight function w and source s, then at termination, the predecessor subgraph is a shortest-paths tree rooted at s.

p20. Difference constraints and shortest paths : Linear programming problem: Given an m x n matrix A, an m-vector b and an n-vector c, we wish to find a vector x of n elements that maximizes the object function ∑ c i x i subject to the m constraints given by Ax  b. Systems of difference constraints: Each row of A has one 1 and one – 1, and all other entries of A are 0.

p21. Lemma 8: Let x = (x 1, x 2,…,x n ) be a solution to a system Ax ≤ b of difference constraints, and let d be any constant. Then x+ d = (x 1 +d, x 2 +d,….,x n +d) is a solution to Ax ≤ b as well. Why? Constraint graphs: Given an m x n matrix A, and a system Ax  b of difference constraints, the corresponding constraint graph is a weighted, directed graph G=(V, E), where V={v 0, v 1, …,v n } and E={(v i, v j ) : x j - x i ≤ b k is a constraint}  {(v 0, v i ): i= 1 … n}. If x j - x i ≤ b k is a constraint, then the weight of (v i, v j ) is b k. The weight of each edge leaving v 0 is 0.

p22. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Theorem 9 Given a system Ax ≤ b of difference constraints, let G=(V,E) be the corresponding constraint graph. (1) If G contains no negative-weight cycle, then x = (  (v 0, v 1 ),  (v 0, v 2 ), …,  (v 0, v n )) is a feasible solution for the system. (2) If G contains a negative-weight cycle, then there is no feasible solution for the system. Pf: (1) For any (v i, v j ) in E,  (v 0, v j ) ≤  (v 0, v i ) + w(v i, v j ), which is  (v 0, v j ) -  (v 0, v i ) ≤ w(v i, v j ). Let x i =  (v 0, v i ) and x j =  (v 0, v j ). (2) Suppose there is a negative cycle c=. x 2 - x 1 ≤ w(v 1, v 2 ), x 3 - x 2 ≤ w(v 2, v 3 ), …, x k - x k-1 ≤ w(v k-1, v k ). Sum them up, we have 0 ≤ w(c) < 0. A contradiction!

Proofs of shortest-paths properties: Lemma 10 (Triangle inequality) G(V,E) : weighted, directed graph, s: source vertex. Then, for all edges,we have Proof : S uv Shortest.

Lemma 13 : Immediately after relaxing edge(u,v) by executing Relax(u,v,w), we have Lemma 11 (Upper-bound property) Given G=(V,E),w,s, let the graph be initialized by Initialize-Single-Source(G,s). Then, for all and this invariant is maintained over any sequence of relaxation steps on the edges of G. Moreover, once v.d achieves its lower bound Proof : (1) (2) Clearly,after, initialization. By contradiction, let v be the first vertex for which a relaxation step of an edge (u,v) cause Therefore, we have (1) (2)Suppose Then v.d cannot decrease, since it reaches the lower bound. And it cannot be increased, because relaxation steps do not increase d values.

Cor 12 (No-path property) Suppose no path connects a source vertex s to a given vertex Then after Initialize-Single-Source(G,s), we have, and this equality is maintained as an invariant over any sequence of relaxation steps on the edges of G. Lemma 14 (Convergence property) Let be a shortest path in G for some vertices Suppose that G is initialized by Initialize-Single-Source(G,s) and then a sequence of Relaxation steps that includes the call Relax(u,v,w) is executed on the edges of G. If at any time prior to the call, then at all times after the call. Proof : If at some point prior to relaxing edge (u,v), then this equality holds thereafter. Proof : After relaxing edge(u,v), we have Thus this equality is maintained thereafter.

Lemma 15 (Path-relaxation property) Let be any shortest path in G from s = v 0 to v k. Suppose that G is initialized by Initialize-Single-Source(G,s) and then a sequence of Relaxation steps that includes, in order, the edges (v 0, v 1 ), …., (v k-1, v k ), then v k.d =  (s, v k ) after these relaxations and at all times afterward. Pf: By induction, we show that after the i-th edge of path p is relaxed, we have v i.d =  (s, v i ). For the basis i=0: s.d=0=  (s, s), which never changes after initialization. For the induction step: Assume v i-1.d =  (s, v i-1 ) and exam Relax(v i-1, v i ). By the convergence property, after this relaxation, we have v i.d =  (s, v i ). This equality is maintained at all times thereafter. 

Relaxation and shortest-paths trees : Lemma 16 G=(E,V) : weighted, directed graph S : source vertex. W : weight function. Assume G has no negative-weight cycles that are reachable from s. Then, after the graph is initialized by Initialize-Single-Source, the predecessor subgraph forms a rooted tree with root s, and any sequence of relaxation steps on edges of G maintains this property as an invariant. Proof : Initially, s is the only vertex in. Consider a predecessor subgraph that arises after a sequence of relaxation steps. Initially, s is the only vertex in. Claim : is acyclic. By contradiction, suppose that some relaxation step creates a cycle in. Let the cycle be c=, where v k =v 0. Then WLOG, assume it was Relax(v k-1,v k,w) that created the cycle in Each vertex on c has a non-NIL predecessor and so was assigned a finite shortest-path estimate, which implies each vertex on c is reachable from s. Just before the call Relax(v k-1,v k,w), we have for i=1,2,…,k-1. Thus, for i=1,2,…,k-1, the last update to v i-1.d was by Relax(v i, v i-1,w) and if changed since then, it decreased. Therefore, just before the call Relax (v k-1,v k,w), we have (*) V k =v 0 V k-1 S

is changed by the call, immediately beforehand we also have the strict inequality. (**) (*)+(**) Why? a negative-weight cycle reachable form s. Thus, in, there is no cycle. By induction, show that a path exists from s to all vertices in. (exercise!) Claim : for any vertex, there is at most one path from s to v in By contradiction, Suppose that there are 2 simple paths from s to some vertex v : s u x y z v P1:SP1:S uxzv papa pbpb pcpc pdpd papa pbpb pdpd P2:SP2:S uyzv papa pcpc pdpd Thus, exists a unique simple path in from s to v, and thus froms a rooted tree with root s.

Lemma 17 (Predecessor-subgraph property) G=(E,V) : weighted, directed graph with weight function w and source vertex G has no negative cycle reachable from s. Let us call Initialize-Single-Source(G,s) and then execute any sequence of relaxation steps on edges of G that produces Then, the predecessor subgraph is a shortest-paths tree rooted at s. Proof : Thus, v.d is finite iff v is reachable from s. By definition, is finite iff v is reachable from s. Need to show the 3 properties of shortest-paths trees hold for (1) But, for, v.d is finite iff Thus, are exactly those vertices reachable from s. (2) forms a rooted tree with root s : By lemma 16. S v i-1 vivi v i-1.d v i.d (3) For all, the unique simple path is a shortest path from s to v in G. Let 0= Thus, p is a shortest path from s to v.