210 likes | 233 Views
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 <v[1],...,v[k]> 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
E N D
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 <v[1],...,v[k]> 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 pathproblem asks for a u-v shortest path for some pair of vertices • All pairs shortest-paths problem will be next time
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 v x <0
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)
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
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
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)
d 3 17 b c e 13 9 12 6 22 11 7 a 12 f 14 0 8 10 8 4 20 i h g 10 8 For each v V do d[v] <- ; d[s] <- 0S <- , Q <- V 1 j
d 3 17 b c e 12 13 9 12 6 22 11 7 a 12 f 14 0 8 10 8 4 20 8 i h g 10 8 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)} 1 j
d 3 17 b c e 12 13 9 12 6 22 11 7 a 12 f 14 0 8 10 8 4 28 20 8 i h g 10 8 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)} 1 j
d 3 17 b c e 12 13 25 9 12 6 22 11 7 a 12 f 14 0 8 10 8 4 19 20 i 8 h g 10 8 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)} 1 j
d 3 17 b c e 12 13 25 9 12 6 22 11 7 a 12 f 27 14 0 8 10 8 4 23 19 20 8 i h g 10 8 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)} 1 29 j
d 3 17 b c e 12 13 25 9 12 6 22 11 7 a 12 f 27 14 0 8 10 8 4 23 19 20 8 i h g 10 8 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)} 1 29 j
d 28 3 17 b c e 12 13 25 34 9 12 6 22 11 7 a 12 f 27 14 0 8 10 8 4 23 19 20 8 i h g 10 8 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)} 1 29 j
d 28 3 17 b c e 12 13 25 34 9 12 6 22 11 7 a 12 f 27 14 0 8 10 8 4 23 19 20 8 i h g 10 8 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)} 1 29 j
d 28 3 17 b c e 12 13 25 34 9 12 6 22 11 7 a 12 f 27 14 0 8 10 8 4 23 19 20 8 i h g 10 8 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)} 1 29 j
d 28 3 17 b c e 12 13 25 34 9 12 6 22 11 7 a 12 f 27 14 0 8 10 8 4 23 19 20 8 i h g 10 8 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)} 1 29 j
d 28 3 17 b c e 12 13 25 34 9 12 6 22 11 7 a 12 f 27 14 0 8 10 8 4 23 19 20 8 i h g 10 8 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)} 1 29 j
d 28 3 17 b c e 12 13 25 34 9 12 6 22 11 7 a 12 f 27 14 0 8 10 8 4 23 19 20 8 i h g 10 8 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)} 1 29 j
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)
Dijkstra’s Correctness (25.2/24.3) • 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] S u s y Q x