Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Shortest-paths

2 p2. 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 台北 新竹 台中 嘉義 高雄 台東 花蓮 宜蘭 台南 埔里

4 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= be a shortest path from v 1 to v k and, for any i and j such that let Pij= be the subpath of p from v i to v j. Then, p ij is a shortest path from vi 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 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 Proof : By the above lemma, p’ is a shortest path froms to u. Thus, Proof : S uv Shortest.

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

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

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. 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 d[v i-1 ] 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

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

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 : Thus, d[v] 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, d[v] is finite iff Thus, are exactly those vertices reachable from s. (2) forms a rooted tree with root s : By lemma 8. S v i-1 vivi d[v i-1 ] d[v i ] (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 p12. 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 p13. Analysis : Priority queue : Time : linear array binary heap Fibonacci heap

14 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

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

16 p16. 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 0s u x v y relax order : (u,v),(u,x),(u,y),(v,u),(x,v),(x,y),(y,v),(y,z),(z,u),(z,x) s u x v y s u x v y s u x v y s u x v y

18 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. 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. (Vi-1,vi) is relaxed in the I-th pass, by lemma7 we have after the I-th pass. Proof : Similar to lemma12.

19 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 : (2) (1) 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 p20. 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. Do for each vertex 5. Do Relax(u,v,w).

21 p21. Thm15 : 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 lemma7, shows at termination for I=0,1,…,k. By lemma9, is a shortest-paths tree. (2) v : reachable from s : S=v 0 v1v1 V k-1 V k =v


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

Similar presentations


Ads by Google