6 d ynamic p rogramming ii
play

6. D YNAMIC P ROGRAMMING II weights or costs c vw , find cheapest - PowerPoint PPT Presentation

Shortest paths Shortest path problem. Given a digraph G = ( V , E ) , with arbitrary edge 6. D YNAMIC P ROGRAMMING II weights or costs c vw , find cheapest path from node s to node t . sequence alignment


  1. 
 
 
 
 
 
 
 
 Shortest paths Shortest path problem. Given a digraph G = ( V , E ) , with arbitrary edge 6. D YNAMIC P ROGRAMMING II weights or costs c vw , find cheapest path from node s to node t . ‣ sequence alignment ‣ Hirschberg's algorithm ‣ Bellman-Ford 1 3 -3 ‣ distance vector protocols 5 4 12 source s 0 -5 ‣ negative cycles in a digraph 8 9 7 2 7 9 -1 1 11 5 5 -3 13 6 4 10 destination t cost of path = 9 - 3 + 1 + 11 = 18 22 Shortest paths: failed attempts Negative cycles Dijkstra. Can fail if negative edge weights. Def. A negative cycle is a directed cycle such that the sum of its edge weights is negative. s 2 u 3 1 v -8 w 5 -3 -3 Reweighting. Adding a constant to every edge weight can fail. -4 4 u 5 5 2 2 � a negative cycle W : c ( W ) = c e < 0 t s e ∈ W 6 6 3 3 0 w v -3 23 24

  2. 
 
 
 
 
 
 
 
 
 Shortest paths and negative cycles Shortest paths and negative cycles Lemma 1. If some path from v to t contains a negative cycle, then there Lemma 2. If G has no negative cycles, then there exists a cheapest path does not exist a cheapest path from v to t . from v to t that is simple (and has ≤ n – 1 edges). Pf. If there exists such a cycle W , then can build a v ↝ t path of arbitrarily Pf. ・ Consider a cheapest v ↝ t path P that uses the fewest number of edges. negative weight by detouring around cycle as many times as desired. ▪ ・ If P contains a cycle W , can remove portion of P corresponding to W without increasing the cost. ▪ v v t t W W c(W) < 0 c(W) ≥ 0 25 26 Shortest path and negative cycle problems Shortest paths: dynamic programming Def. OPT ( i , v ) = cost of shortest v ↝ t path that uses ≤ i edges. 
 Shortest path problem. Given a digraph G = ( V , E ) with edge weights c vw and no negative cycles, find cheapest v ↝ t path for each node v . ・ Case 1: Cheapest v ↝ t path uses ≤ i – 1 edges. Negative cycle problem. Given a digraph G = ( V , E ) with edge weights c vw , - OPT ( i , v ) = OPT( i – 1, v ) 
 optimal substructure property find a negative cycle (if one exists). (proof via exchange argument) ・ Case 2: Cheapest v ↝ t path uses exactly i edges. - if ( v , w ) is first edge, then OPT uses ( v , w ) , and then selects best w ↝ t path using ≤ i – 1 edges 5 1 $ 0 ∞ if i = 0 -3 -3 5 -3 4 2 * OPT ( i , v ) = $ ' % min OPT ( i − 1, v ) , OPT ( i − 1, w ) + c vw { } otherwise % min ( * & ) & ( v , w ) ∈ E t 4 -4 negative cycle shortest-paths tree Observation. If no negative cycles, OPT ( n – 1, v ) = cost of cheapest v ↝ t path. Pf. By Lemma 2, cheapest v ↝ t path is simple. ▪ 27 28

  3. 
 
 
 Shortest paths: implementation Shortest paths: implementation Theorem 1. Given a digraph G = ( V , E ) with no negative cycles, the dynamic programming algorithm computes the cost of the cheapest v ↝ t path for 
 each node v in Θ ( mn ) time and Θ ( n 2 ) space. S HORTEST -P ATHS ( V , E , c , t ) _________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________ Pf. F OREACH node v ∈ V ・ Table requires Θ ( n 2 ) space. M [0, v ] ← ∞ . ・ Each iteration i takes Θ ( m ) time since we examine each edge once. ▪ M [0, t ] ← 0. F OR i = 1 TO n – 1 Finding the shortest paths. F OREACH node v ∈ V ・ Approach 1: Maintain a successor ( i , v ) that points to next node on M [ i , v ] ← M [ i – 1, v ]. cheapest v ↝ t path using at most i edges. F OREACH edge ( v, w ) ∈ E ・ Approach 2: Compute optimal costs M [ i , v ] and consider only edges M [ i , v ] ← min { M [ i , v ], M [ i – 1, w ] + c vw }. with M [ i , v ] = M [ i – 1, w ] + c vw . _________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________ 29 30 Shortest paths: practical improvements Bellman-Ford: efficient implementation Space optimization. Maintain two 1d arrays (instead of 2d array). ・ d ( v ) = cost of cheapest v ↝ t path that we have found so far. B ELLMAN -F ORD ( V , E , c , t ) _________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________ ・ successor ( v ) = next node on a v ↝ t path. F OREACH node v ∈ V d ( v ) ← ∞ . Performance optimization. If d ( w ) was not updated in iteration i – 1 , 
 successor ( v ) ← null . then no reason to consider edges entering w in iteration i . d ( t ) ← 0. F OR i = 1 TO n – 1 F OREACH node w ∈ V I F ( d ( w ) was updated in previous iteration) F OREACH edge ( v, w ) ∈ E 1 pass I F ( d ( v ) > d ( w ) + c vw ) d ( v ) ← d ( w ) + c vw . successor ( v ) ← w . I F no d ( w ) value changed in iteration i, S TOP . _________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________ 31 32

Recommend


More recommend