1 / 21

Shortest-paths

Shortest-paths. 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:. Shortest paths tree rooted at s : 1. V’ is the set of vertices reachable from s in G.

helen
Download Presentation

Shortest-paths

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

  2. Shortest-paths problems : • G=(V,E):weighted, directed graph • w:E  R : weight function • P=<v0,v1,…,vk):path form v0 to vk. Shortest-path weight from u to v:

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

  4. v1 vi vk vj • Optimal substructure of a shortest path: • Lemma1(subpaths of shortest paths are shortest paths) Given a weighted, dircted graph G=(V,E) with weight function w:ER, let p=<v1,v2,…,vk> be a shortest path from v1 to vk and , for any i and j such that let Pij=<vi,vi+1,…,vj> be the subpath of p from vi to vj. Then, pij is a shortest path from vi to vj. Proof : Decompose p into Suppose there is a path p’ij from vi to vj with w(p’ij)<w(pij). Then W(p)>w(p1j)+w(p’ij)+w(pjk). P is not shortest !

  5. Proof : • Cor2: 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 • Lemma3 G(V,E):weighted, directed graph, S:source vertex. Then, for all edges ,we have By the above lemma, p’ is a shortest path froms to u. Thus, Proof : Shortest. S u v

  6. Relaxation • d[v]: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) Eg. { for each vertex u d[u] w(u,v) do{ d[v]= S v d[v] S 9 6 5 5 Relax(u,v,w) 2 2 u v u v { if then { Relax(u,v) 7 6 5 5 2 2 u v u v

  7. (1) • Lemma4: Immediately after relaxing edge(u,v) by executing Relax(u,v,w), we have • Lemma5 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 d[v] achieves its lower bound (2) Proof : (1) 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 (2) Suppose Then d[v] cannot decrease, since it reaches the lower bound. And it cannot increased, because relaxation steps do not increase d values.

  8. Cor6: 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. • Lemma7 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 : Proof : If at some point prior to relaxing edge (u,v), then this equality holds thereafter. After relaxing edge(u,v), we have Thus this equality is maintained thereafter.

  9. Shortest-paths trees: • Lemma8 G=(E,V):weighted, directed graph S:source vertex. W:weight function. Assume G no negative-weight cycles that are reachable from s. Then, after the graph is initialized by Initialize_Single-Source, the predecessor subgraph from s 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 . Initially, s is the only vertex in . S Consider a predecessor subgraph that arises after a sequence of relaxation steps. Claim: is acyclic. Vk=v0 By contradiction, suppose that some relaxation step creates a cycle in . Let the cycle be c=<vo,v1,….,vk>, where vk=v0. Then Vk-1 WLOG, assume it was Relax(vk-1,vk,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(vk-1,vk,w), we have for i=1,2,…,k-1. Thus, for i=1,2,…,k-1, the last update to d[vi-1] changed since then, it decreased. Therefore, just before the call Relax (vk-1,vk,w), we have (*)

  10. 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: pa pb pd pb x u x z v P1:S pa pd u z s v pa pc pd u y z v P2:S y pc Thus, exists a unique simple path in from s to v, and thus froms a rooted tree with root s.

  11. Lemma9 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 : Need to show the 3 properties of shortest-paths trees hold for (1) By definition, is finite iff v is reachable from s. Thus, d[v] is finite iff v is reachable from s. But, for , d[v] is finite iff Thus, are exactly those vertices reachable from s. vi-1 (2) forms a rooted tree with root s: By lemma 8. d[vi-1] vi d[vi] S (3) For al , 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.

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

  13. Analysis : Priority queue: Time: linear array binary heap Fibonacci heap

  14. v u v u 1 1 10 10 9 2 3 9 s 0 2 3 4 6 s 0 4 6 7 7 5 5 2 y 2 x y x v v u u 1 1 10 10 9 9 2 3 2 3 s 0 s 0 4 6 4 6 7 7 5 5 2 2 y y x x v u v u 1 1 10 10 9 9 2 3 2 3 s 0 s 0 4 6 4 6 7 7 5 5 2 2 y x y x

  15. Lemma10:(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. There must be a path Thus there is a shortest path p from s to u. y :the first one in V-S along p p1 u p2 p x S S u Claim: , when u is inserted into S. when x was inserted into S. When u is inserted into S, (x,y) had been relaxed, thus Since both u and y were in V-S. When u was chosen in the algorithm, we have Thus, Contradict the assumption that

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

  17. relax order:(u,v),(u,x),(u,y),(v,u),(x,v),(x,y),(y,v),(y,z),(z,u),(z,x) -2 v u -2 v u 6 5 6 5 -4 -4 -3 -3 8 8 s 0 s 0 7 7 2 2 7 7 9 9 y x y x -2 v -2 v u u 6 5 6 5 -4 -4 -3 -3 8 8 s 0 s 0 7 7 2 2 7 7 9 9 y y x x -2 v u 6 5 -4 -3 8 s 0 7 2 7 9 y x

  18. Proof : • Lemma 12: At the termination of Bellman-Ford, we have for all v that are reachable from s. • Corollary 13: For each vertex , there is a path from s to v iff Bellman-Ford terminates with when it is run on G. 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. (Vi-1,vi) is relaxed in the I-th pass, by lemma7 we have after the I-th pass. Proof : Similar to lemma12.

  19. (1) (2) • Thm14:(correctness of the Bellman-Ford algorithm) The algorithm returns TRUE, we have and the predecessor subgraph is a shortest=paths tree rotted at s. If G does maintain a negative-weight cycle reachable from s, then the algorithm returns False. Proof : (1) G has no negative cycle reachable from s: a. V:reachable from s. By lemma12, b. V:not reachable from s. By corllary6, With lemma9 is a shortest-paths tree. At termination, for all Thus, the algorithm return TRUE. (2) G has a negative cycle reachable form s. Assume the algorithm returned TRUE, i.e.

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

  21. Thm15: G:a dag with source s. At termination of the above procedure , for all is a shortest-paths tree. Proof : (1) v:not reachable from s:then (2) v: reachable from s: S=v0 v1 Vk-1 Vk=v By a simple induction using lemma7, shows at termination for I=0,1,…,k. By lemma9, is a shortest-paths tree.

More Related