Presentation is loading. Please wait.

Presentation is loading. Please wait.

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])

Similar presentations


Presentation on theme: "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])"— Presentation transcript:

1 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]) Single-source shortest-paths: find a shortest path from a source s to every vertex v  V Single pair shortest path problem asks for a u-v shortest path for some pair of vertices All pairs shortest-paths problem will be next time

2 Shortest Paths (25/24) Predecessor subgraph for restoring shortest paths Shortest-paths tree rooted at source s Th: Subpath of a shortest path is a shortest path Triangle Inequality:  (u,v)   (u,x) +  (x,v) Well defined: some paths may not exist if there is a negative-weight cycle in graph u x v <0

3 Bellman-Ford (25.3/24.1) Most basic (BFS based) algorithm, shortest paths (tree) easy to reconstruct. for each v  V do d[v]   ; d[s]  0 Relaxation for i =1,...,|V|-1 do for each edge (u,v)  E do d[v]  min{d[v], d[u]+w(u,v)} Negative cycle checking for each v  V do if d[v]> d[u] + w(u,v) then no solution Finally d[v]=  (s,v)

4 Bellman-Ford Analysis (25.3/24.1) Runtime = O(VE) Correctness –Lemma: d[v]   (s,v) Initially true Let d[v] = d[u] +w(u,v) by triangle inequality for first violation d[v] <  (s,v)   (s,u)+w(u,v)  d(u)+w(u,v) –After |V|-1 passes all d values are  ’s if there are no negative cycles s  v[1]  v[2] ...  v (some shortest path) After i-th iteration d[s,v[i]] is correct and final

5 Dag Shortest Paths (25.4/24.2) DAG shortest paths –Bellman-Ford = O(VE) –Topological sort O(V+E) (DFS) –Will never relaxed edges out of vertex v until have done all edges in v –Runtime O(V+E) Application: PERT ( program evaluation and review technique ) –Critical path is the longest path through the dag –Negating the edge weights and running dag shortest paths algorithm

6 Dijkstra’s Shortest Paths (25.2/24.3) Better than BF since non-negative weights. Like BFS but uses priority queue. for each v  V do d[v]   ; d[s]  0 S   ; Q  V While Q   do u  Extract-Min(Q) S  S + u for v adjacent to u do d[v]  min{d[v], d[u]+w(u,v)} (relaxation = Decrease-Key)

7 0          8 10 20 1 8 4 12 7 13 3 9 6 22 14 8 10 12 17 11 For each v  V do d[v] <-  ; d[s] <- 0 S <- , Q <- V a b g c d f e i h j

8 0  8       12 8 10 20 1 8 4 12 7 13 3 9 6 22 14 8 10 12 17 11 a b g c d f e i h j While Q   do U Extract-Min(Q) S S + u for v adjacent to u do d[v] min{d[v], d[u]+w(u,v)}

9 0  8  28     12 8 10 20 1 8 4 12 7 13 3 9 6 22 14 8 10 12 17 11 a b g c d f e i h j While Q   do U Extract-Min(Q) S S + u for v adjacent to u do d[v] min{d[v], d[u]+w(u,v)}

10 0  8  19 25    12 8 10 20 1 8 4 12 7 13 3 9 6 22 14 8 10 12 17 11 a b g c d f e i h j While Q   do U Extract-Min(Q) S S + u for v adjacent to u do d[v] min{d[v], d[u]+w(u,v)}

11 0 27 8 29 19 25 23   12 8 10 20 1 8 4 12 7 13 3 9 6 22 14 8 10 12 17 11 a b g c d f e i h j While Q   do U Extract-Min(Q) S S + u for v adjacent to u do d[v] min{d[v], d[u]+w(u,v)}

12 0 27 8 29 19 25 23   12 8 10 20 1 8 4 12 7 13 3 9 6 22 14 8 10 12 17 11 a b g c d f e i h j While Q   do U Extract-Min(Q) S S + u for v adjacent to u do d[v] min{d[v], d[u]+w(u,v)}

13 0 27 8 29 19 25 23 34 28 12 8 10 20 1 8 4 12 7 13 3 9 6 22 14 8 10 12 17 11 a b g c d f e i h j While Q   do U Extract-Min(Q) S S + u for v adjacent to u do d[v] min{d[v], d[u]+w(u,v)}

14 0 27 8 29 19 25 23 34 28 12 8 10 20 1 8 4 12 7 13 3 9 6 22 14 8 10 12 17 11 a b g c d f e i h j While Q   do U Extract-Min(Q) S S + u for v adjacent to u do d[v] min{d[v], d[u]+w(u,v)}

15 0 27 8 29 19 25 23 34 28 12 8 10 20 1 8 4 12 7 13 3 9 6 22 14 8 10 12 17 11 a b g c d f e i h j While Q   do U Extract-Min(Q) S S + u for v adjacent to u do d[v] min{d[v], d[u]+w(u,v)}

16 0 27 8 29 19 25 23 34 28 12 8 10 20 1 8 4 12 7 13 3 9 6 22 14 8 10 12 17 11 a b g c d f e i h j While Q   do U Extract-Min(Q) S S + u for v adjacent to u do d[v] min{d[v], d[u]+w(u,v)}

17 0 27 8 29 19 25 23 34 28 12 8 10 20 1 8 4 12 7 13 3 9 6 22 14 8 10 12 17 11 a b g c d f e i h j While Q   do U Extract-Min(Q) S S + u for v adjacent to u do d[v] min{d[v], d[u]+w(u,v)}

18 0 27 8 29 19 25 23 34 28 12 8 10 20 1 8 4 12 7 13 3 9 6 22 14 8 10 12 17 11 a b g c d f e i h j While Q   do U Extract-Min(Q) S S + u for v adjacent to u do d[v] min{d[v], d[u]+w(u,v)}

19 Dijkstra’s Runtime (25.2/24.3) Extract-Min executed |V| times Decrease-Key executed |E| times Time = |V|  T(Extract-Min) (find+delete = O(log V)) + |E|  T(Decrease-Key) (delete+add =O(log V)) = Binary Heap = E  log V (30 years ago) = Fibonacci Heap = E + V  log V (10 years ago) Optimal time algorithm found 1/2 year ago. It runs in time O(E) (Mikel Thorup)

20 The same Lemma as for BF d[v]   (s,v) Th: Whenever u is added to S, d[u] =  (s,u) Let u be first s.t. d[u] >  (s,u) Let y be first  V-S on actual shortest s-u path  d[y] =  (s,y) For y’s predecessor x, d[x] =  (s,x) when put x in S d[y] gets  (s,y) d[u] >  (s,u) =  (s,y) +  (y,u) = d[y] +  (y,u)  d[y] Dijkstra’s Correctness (25.2/24.3) s x y u S Q


Download ppt "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])"

Similar presentations


Ads by Google