07 - Algorithms Dynamic Programming Dynamic Programming II...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Algorithms – Dynamic Programming Dynamic Programming II Design and Analysis of Algorithms Andrei Bulatov
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Algorithms – Dynamic Programming 7-2 Shortest Path Suppose that every arc e of a digraph G has length (or cost, or weight, or …) len(e) But now we allow negative lengths (weights) Then we can naturally define the length of a directed path in G, and the distance between any two nodes The s-t-Shortest Path Problem Instance : Digraph G with lengths of arcs, and nodes s,t Objective : Find a shortest path between s and t
Background image of page 2
Algorithms – Dynamic Programming 7-3 Shortest Path: Difficulties Negative Cycles. s t <0 t t 3 Greediness fails Adding constant weight to all arcs fails s t 1 2 -6
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Algorithms – Dynamic Programming 7-4 Shortest Path: Observations Assumption There are no negative cycles Lemma If graph G has no negative cycles, then there is a shortest path from s to t that is simple (i.e. does not repeat nodes), and hence has at most n – 1 arcs Proof If a shortest path P from s to t repeat a node v, then it also include a cycle C starting and ending at v. The weight of the cycle is non-negative, therefore removing the cycle makes the path shorter (no longer). QED
Background image of page 4
Algorithms – Dynamic Programming 7-5 Shortest Path: Dynamic Programming We will be looking for a shortest path with increasing number of arcs Let OPT(i,v) denote the minimum weight of a path from v to t using at most i arcs Shortest v – t path can use i – 1 arcs. Then OPT(i,v) = OPT(i – 1,v) Or it can use i arcs and the first arc is vw. Then OPT(i,v) = len(vw) + OPT(i – 1,w) v t w )}} ( ) , 1 ( { min ), , 1 ( min{ ) , ( vw len w i OPT v i OPT v i OPT V w + - - =
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Algorithms – Dynamic Programming 7-6 Shortest Path: Bellman-Ford Algorithm Shortest-Path(G,s,t) set n:=|V| /*number of nodes in G array M[0. .n-1,V] set M[0,t]:=0 and M[0,v]:= for each v V-{t} for i=1 to n-1 do for v V do set M[i,v]:=min{M[i-1,v],min {M[i-1,w]+len(vw)}} endfor endfor return M[n-1,s] w V
Background image of page 6
Algorithms – Dynamic Programming 7-7 Example 0 1 2 3 4 5 0 -3 0 -3 0 0 0 -4 -6 -6 0 t a a b d v -4 -1 6 -3 -2 4 M[i,v] = min{ M[i – 1, v], min { M[ i – 1, w] + len(vw) }} 3 4 2 0 3 3 0 -2 3 3 0 -2 3 2 0 -2 3 0 0 b c d e c e 8 -3 2 3 w V
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Programming 7-8 Shortest Path: Soundness and Running Time Theorem The ShortestPath algorithm correctly computes the minimum cost of an s-t path in any graph that has no negative cycles, and runs in O( ) time 3 n Proof . Soundness follows by induction from the recurrent relation for the optimal value. DIY.
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 28

07 - Algorithms Dynamic Programming Dynamic Programming II...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online