1 / 22

6.006- Introduction to Algorithms

6.006- Introduction to Algorithms. Lecture 16 Alan Deckelbaum. a. j. h. e. f. i. c. d. b. g. Lecture overview. 5. Shor test paths III Bellman-Ford on a DAG (CLRS 24.2) Dijkstra algorithm for the case with non-negative weights (CLRS 24.3). v 1. v 4. 7. 6. 2. -4. 3. 5.

armina
Download Presentation

6.006- Introduction to Algorithms

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. 6.006- Introduction to Algorithms Lecture 16 Alan Deckelbaum

  2. a j h e f i c d b g Lecture overview 5 Shortest paths III • Bellman-Ford on a DAG (CLRS 24.2) • Dijkstra algorithm for the case with non-negative weights (CLRS 24.3) v1 v4 7 6 2 -4 3 5 5 5 2 1 2 v3 v2 8 8 1 6 6 2 5 4 4

  3. This graph has a special structure: DAG. How to use it within Bellman-Ford? 5 E={(v1,v2); (v1,v4); (v2,v3); (v4,v2)} v1 v4 2 -4 2 v3 v2

  4. ... first use topological sorting ... 5 5 v1 v1 v4 v2 2 2 -4 -4 2 2 v3 v4 v3 v2 E={(v1,v2); (v1,v4); (v2,v3); (v4,v2)} E={(v1,v2); (v1,v3); (v2,v3); (v3,v4)}

  5. ... Bellman-Ford ... 5 E={(v1,v2); (v1,v3); (v2,v3); (v3,v4)} v1 v2 ∞ 0 2 -4 ⁄ 5 2 ∞ v4 ∞ v3 end of first iteration 1 ⁄ 2 ⁄ ⁄ 3 and we are done ! the shortest paths from v1

  6. d[s] 0; π[s] s • for each vÎV – {s} • dod[v] ¥; π[v]  nil initialization do for each edge (u, v) ÎE do ifd[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) π[v] u one iteration ofrelaxation steps Bellman-Ford algorithm on DAG topologically sort the vertices V ( f: V → {1, 2, …, |V |} such that (u,v) ∈ E ⇒ f (u) < f (v)) O(n+m) arrange E in lexicographical order of (f(e.a), f(e.b)) O(n) O(m) for each edge (u, v) ÎE do ifd[v] > d[u] + w(u, v) then report a negative cycle final steps not needed

  7. ... why does this work? ... • there are no cycles in a dag => even with negative-weight edges, there are no negative-weight cycles ... • topological ordering implies a linear ordering of the vertices; every path in a dag is a subsequence of topologically sorted vertex order; processing vertices in that order, an edge can’t be relaxed more than once ...

  8. Proof of Correctness • Let tbe an arbitrary vertex. Suffices to show that we compute d[t] properly. • Let s=s0, s1, s2, …, sk=tbe a shortest path to t. Show by induction that we compute each d[si] correctly. • d[si-1] computed correctly by inductive hypothesis. • (si-1,si) relaxed AFTER d[si-1] computed.

  9. Review of Dijkstra(Non-negative Edge Weights) Problem: Given a directed graph G = (V, E) with edge-weight functionw : ER+, and a node s, find the shortest-path weight d(s, v) (and a corresponding shortest path) from sto each v in V. • Greedy iterative approach • maintain a set S of vertices whose shortest-path distances from s are known. • at each step add to S the vertex vÎV – Swhose distance estimate from s is minimal. • update distance estimates of vertices adjacent to v.

  10. Dijkstra’s algorithm • d[s]  0 • for each vÎV – {s} • dod[v] ¥ • S • QV initialization • whileQ ¹ • dou EXTRACT-MIN(Q) • SSÈ {u} • for each vÎAdj[u] • doifd[v] > d[u] + w(u, v) • then d[v] d[u] + w(u, v) (Qmin-priority queue maintainingV – S) relaxation steps (Implicit DECREASE-KEY)

  11. (,-) 7 (,-) 6 initialization (,-) 3 5 5 5 2 (,-) (0,*)+ 1 (,-) a 8 (,-) 8 1 6 6 f e h i g c d b j 2 5 (,-) 4 4 (,-) (,-) Dijkstra: Example Q =V, a = EXTRACT-MIN(Q)

  12. (,-) 7 (3,a)+ 6 1st iteration (,-) 3 5 5 5 2 (,-) (0,*)+ 1 (,-) a 8 (8,a) 8 1 6 6 g j i j h e f i d g c h e d b f c 2 5 (,-) 4 4 (,-) (5,a) (10,b) 7 (3,a)+ 2nd iteration 6 b (,-) 3 5 5 5 2 (,-) (0,*)+ 1 (,-) a 8 (8,a) 8 1 6 6 2 5 (,-) 4 4 (5,a)+ (,-) Dijkstra: Example

  13. (10,b) 7 (3,a)+ 6 3rd iteration b (,-) 3 5 5 5 2 (,-) (0,*)+ 1 (,-) a 8 (7,d)+ 8 1 6 6 g f j i h e j g c i e h f 2 5 d (,-) 4 4 (5,a)+ (9,d) (10,b) 7 (3,a)+ 4th iteration 6 b (,-) 3 5 5 5 2 (15,c) (0,*)+ 1 (,-) a c 8 (7,d)+ 8 1 6 6 2 5 d (,-) 4 4 (9,d)+ (5,a)+ Dijkstra: Example

  14. (10,b)+ 7 (3,a)+ 6 5th iteration b (,-) 3 5 5 5 2 (15,c) (0,*)+ 1 (,-) a c 8 (7,d)+ 8 1 6 6 j e f e h h i j i 2 5 d g (13,g) 4 4 (5,a)+ (9,d)+ (10,b)+ 7 (3,a)+ 6th iteration 6 b f (16,f) 3 5 5 5 2 (15,c) (0,*)+ 1 (,-) a c 8 (7,d)+ 8 1 6 6 2 5 d g (13,g)+ 4 4 (9,d)+ (5,a)+ Dijkstra: Example

  15. (10,b)+ 7 (3,a)+ 6 7th iteration b (16,f) 3 5 5 5 2 (14,i)+ (0,*)+ 1 (19,i) a c 8 (7,d)+ 8 1 6 6 h j i h f j e 2 5 i d g (13,g)+ 4 4 (5,a)+ (9,d)+ (10,b)+ 7 (3,a)+ 8th iteration 6 b f (15,e)+ 3 5 5 5 2 (14,i)+ (0,*)+ 1 (19,i) a c e 8 (7,d)+ 8 1 6 6 2 5 d g (13,g)+ 4 4 (9,d)+ (5,a)+ Dijkstra: Example

  16. (10,b)+ 7 (3,a)+ 6 9th iteration b (15,e)+ 3 5 5 h 5 2 (14,i)+ (0,*)+ 1 a c e (17,h)+ 8 (7,d)+ 8 1 6 6 g a f e d f j h b i j c 2 5 i d g (13,g)+ 4 4 (5,a)+ (9,d)+ (10,b)+ 7 (3,a)+ Shortest-path tree (15,e)+ 3 2 (14,i)+ (0,*)+ 1 (17,h)+ (7,d)+ 1 2 5 (13,g)+ 4 4 (5,a)+ (9,d)+ Dijkstra: Example

  17. d[u] u w(u, v) s v d[v] Correctness — Part I Lemma. Initializing d[s]  0 and d[v] ¥for all vÎV – {s} establishes d[v] ³d(s, v) for all vÎV, and this invariant is maintained over any sequence of relaxation steps. Proof. Recall relaxation step: ifd[v] > d[u] + w(u, v)set d[v] d[u] + w(u, v)

  18. Correctness — Part II Theorem.Dijkstra’s algorithm terminates with d[v] = d(s, v) for all vÎV. • Proof. • It suffices to show that d[v] = d(s, v) for every vÎVwhen v is added to S • Suppose u is the first vertex added to S for which d[u]>d(s, u) . Let y be the first vertex in V – Salong a shortest path from s to u, and let x be its predecessor: u s S, just before adding u. x y

  19. S s x u Correctness — Part II (continued) Case 1: y =u • Since u is the first vertex violating the claimed invariant, we have d[x] = d(s, x) at the time xwas added to S. • Just after xwas added to S, we therefore set d[u] = d(s,u) • This is a contradiction, since d[u] is never increased by edge relaxation.

  20. S u s x y Correctness — Part II (continued)Case 2: y != u • Since u is the first vertex violating the claimed invariant, we have d[x] = d(s, x) • Since subpaths of shortest paths are shortest paths, it follows that d[y] was set to d(s, x) + w(x, y) = d(s, y) just after x was added to S • Consequently, we have d[y] = d(s, y) £d(s, u)<d[u] • But, d[y] ≥ d[u] since the algorithm chose ufirst => a contradiction

  21. |V|times degree(u) times DECREASE-KEY Analysis of Dijkstra • whileQ ¹ • dou EXTRACT-MIN(Q) • SSÈ {u} • for each vÎAdj[u] • doifd[v] > d[u] + w(u, v) • then d[v] d[u] + w(u, v) Time=Q(n)·TEXTRACT-MIN + Q(m)·TDECREASE-KEY

  22. array O(n) O(1) O(n2) binary heap O(lgn) O(lgn) O(mlgn) Fibonacci heap O(lgn) amortized O(1) amortized O(m+nlgn) worst case Analysis of Dijkstra (continued) Time=Q(n)·TEXTRACT-MIN + Q(m)·TDECREASE-KEY Q TEXTRACT-MIN TDECREASE-KEY Total

More Related