Presentation is loading. Please wait.

Presentation is loading. Please wait.

Single-Source Shortest- paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=

Similar presentations


Presentation on theme: "Single-Source Shortest- paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=

1 Single-Source Shortest- paths

2 p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P= { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/4168377/13/slides/slide_1.jpg", "name": "p2.", "description": "Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=

3 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 台北 新竹 台中 嘉義 高雄 台東 花蓮 宜蘭 台南 埔里 30 100 80 100 70 30 50 100 90 140 60 90

4 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 1j )+w(p’ ij )+w(p jk ). P is not shortest !

5 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,

6 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 2 5 6 uv 2 5 6 uv 2 v.d w(u,v)u.d S v u

7 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.

8 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.

9 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) }

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

11 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!

12 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.

13 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).

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

15 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

16 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) { } }

17 0s u x v y 10 5 23 1 9 2 7 460s u x v y 5 23 1 9 2 7 46 0s u x v y 5 23 1 9 2 7 46 0s u x v y 5 23 1 9 2 7 46 0s u x v y 5 23 1 9 2 7 46 0s u x v y 5 23 1 9 2 7 46

18 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

19 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.

20 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.

21 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.

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

23 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!

24 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.

25 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.

26 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.

27 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. 

28 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

29 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.

30 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.


Download ppt "Single-Source Shortest- paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=

Similar presentations


Ads by Google