CS DESIGN ND NLYSIS OF LGORITHMS DYNMIC PROGRMMING Dr. Dasy Tang
Dynamc Programmng Idea: Problems can be dvded nto stages Soluton s a sequence o decsons and the decson at the current stage s based on the results o the prevous stage
Example: Fbonacc Numbers F() F() F( n) F( n ) F( n ) n ssume n = 5 F(5) F () F() F () F () F () F() F () F () F () F () F() F() F() F()
I We Use Dvde-and-Conquer Dvde the problem nto subproblems. Solve each subproblem ndependently and then combne the solutons. For example F() s rst calculated n the let subtree and t wll be calculated agan n the rght subtree Subproblems are solved ndependently
But Dynamc Programmng 5 Works on phases 5 8... Based on the results n prevous phase we can calculate result or the current phase Can be solved n a or-loop F[] = F[] = For to n F[] = F[-] + F[-];
Greedy vs. Dynamc Programmng 6 Consder shortest path problem Suppose we wsh to nd a shortest path rom vertex to vertex j. Let be the vertces adjacent rom vertex. Whch o the vertces n should be the second vertex on the path? There s no way to make a decson at ths tme and guarantee that uture decsons leadng to an optmal sequence can be made. I on the other hand we wsh to nd a shortest path rom vertex to all other vertces n G then at each step a correct decson can be made based on the prncple o optmalty.
Greedy vs. Dynamc Programmng 7 Greedy: For problems that an optmal soluton can be ound by makng the decsons one at a tme and never makng an erroneous decson [e.g. Knapsack] Consder only decson sequence Dynamc programmng: For problems that t s not possble to make stepwse decsons based only on local normaton Consder many decson sequences Try all possbltes but elmnates mpossble cases based on the prncple o optmalty
Prncple o Optmalty 8 Prncple o optmalty: n optmal sequence o decsons has the property that whatever the ntal decson s the remanng decsons must consttute an optmal decson sequence wth regard to the state resultng rom the rst decson elmnates not optmal subsequences
Examples 9 The shortest path problem satses the Prncple o Optmalty Subpath o a shortest path s also a shortest path The longest path problem does not satsy the Prncple o Optmalty Consder a rng graph The subpath o a longest path mght not be a longest path
. Multstage Sngle-Source Sngle- Destnaton Shortest Path Problem The Problem: Gven: m columns (or stages) o n nodes each a source node a destnaton node. ssume edges only exst between stages. Goal: to nd the shortest path rom source ( ) to the destnaton (m- ) Stage number Node number o that stage
Straghtorward vs. Dynamc Programmng Straghtorward way: Try all possble paths Complexty: O( n m ) Dynamc programmng: Let M( j) = length o the shortest path rom the source () to ( j) c( k j) = dstance rom ( k) to (+ j) want to nd M(m- ) Dene unctonal equaton: n M ( j) mn k M () M ( k) c( Shortest path rom source () to destnaton (m-) Dene the relaton between current stage and the result o prevous stage k j)
Example 5 5 Source 5 Destnaton
M () mn M () mn M () mn 5 Remember the prevous node o ths shortest path M () mn 5 7 M () mn 5 5 M () mn M ( ) mn7 5 7 Can be used or constructng the shortest path at the end The shortest path s: ( ) () () () ()
M: node( j) \ stage( ) 7 7 5 lgorthm: assgn ntally M()= M() = M() = M()=; or = to (m ) // stages or j = to n // nodes M ( j) mn k calculate and return M(m ); n M ( k) c( k j)
Tme Complexty 5 There are O(mn) M( j) s to compute each M( j) takes O(n) computaton. So total complexty s O(mn ). total # o nodes n the graph Let N = m n nodes max # o stages max # o nodes n each stage mn = mn n = N n Note: use the Djkstra s algorthm the complexty s O(N )
Revst Dynamc Programmng 6 The problem can be dvded nto stages. Need to make decsons at each stage. The decson or the current stage (based on the results o the prevous stage) s ndependent rom that o the prevous stages. e.g. node o the multstage shortest path problem t s calculated rom m() m() and m() but t s ndependent rom how node ( or ) makes ts decson. Note that we nd shortest paths rom the source to all three nodes and and but some o them may not be used on the shortest path at all. But every decson must be optmal so that the overall result s optmal. Ths s called Prncple o Optmalty.
Problem : ll Pars Shortest Paths 7 The problem: Gven G = (V E) a drected graph wth n nodes Cost a cost adjacency matrx or G: Cost = Cost j = cost o <j> <j> E(G) otherwse Goal: to determne a matrx s.t. j = the length o the shortest path rom to j j n
Use Djkstra s algorthm call t or n tmes: Tme complexty s O(n ) The Dynamc Programmng way: Let jk = the length o the shortest path rom to j gong through no vertex o ndex greater than k Want to nd j n Observe that j = Cost Dene unctonal equaton: jk = mn { j k- k k- + kj k- } 8
9 Example: v v v 6 6 7 mn mn mn 6 mn mn mn 7 6
7 6 6 7 mn mn mn 5 mn 6 mn mn 7 5 6 7 mn mn mn 6 mn 6 mn mn
lgorthm: procedure llpars (n Cost ) // n Cost are nput s output { or to n do or j to n do (j) Cost(j); or k to n do or to n do or j to n do (j) mn {(j) (k)+ (kj)}; } There are O(n) k s to compute each takes O(n ) computaton total complexty o llpar s O(n ).
In order to prnt shortest paths or all pars o vertces we mody the algorthm: procedure llpaths (n Cost P) // n Cost are nput and P are output // P(j) contans the ntermedate node rom to j { or = to n do or j = to n do (j) = Cost(j); P(j) = j; or k = to n do or = to n do or j = to n do ((j) > (k)+ (kj) ) then P(j) = k; (j) = (k)+ (kj) ; or = to n do or j = to n do prnt(); Call PrntOut(P j); } procedure PrntOut (P j) { P(j) = j } then prnt(j); else let k = P(j); call PrntOut(P k); call PrntOut(P k j);
In-Class Exercse # Solve the all pars shortest path problem or the graph wth the ollowng weght matrx. Fnd and P and shortest paths or all pars o vertces. Show steps. = 5 5
Problem : Chaned Matrx Multplcaton The problem: Gven M M... M r (r matrces) [d d ] [d d ] [d r- d r ] Goal: nd the mnmum cost o multplyng r matrces
5 The dea: Dene C( j) = mnmum cost o computng M M +... M j C( ) = C ( ) d d d Want to nd C(r) Consder matrces rom to j : k = M M M...... (the total # o elementary multplcatons )...... M j M j k: dvde-pont k j We can dvde the sequence o matrces rom to j nto two parts wth dvdng pont k where k j. j
6 We dvde t nto two parts wth dvde-pont k j k d k d ] [ j k d d Dene the unctonal equaton: B j k j k d d d j k C k C j C ) ( ) ( mn ) ( Cost o multplyng and B k k k d d d d M M M... j j k k j k d d d d M M...
7 Example: 5 5 8 8 M M M M sze C ) ( sze 5 () 8 5 8 () 8 8 () C C C
sze C() mn ( C() C() 8 5)( C() C() 5 k 78 Smallest C() mn( C() C() 8 )( C() C() 8 5 k Smallest 8 + + ( C() C() 8 ) sze C() mn ( C() C() ) k ( () () 5 C C 8 It has the smallest cost when k =. Remember ths k-value and t can be used or obtanng the optmal soluton. The optmal multplcaton sequence or ths nstance s (M M ) (M M ).
9 lgorthm: procedure MnMult (r D P) // r D are nputs P s output { or to r do C( ) = or d to (r ) do or to (r d) do P C( r); } ; ) ( ) ( ) ( ) ( ) ( mn ) ( ; j D k D D j k C k C j C d j j k
nalyss: Matrx product o # o C(j) s wth ths sze Cost o computng C(j) sze.e. j = + r sze.e. j = + r............ sze r j = + (r ) r Total complexty s O(r ) Total # o C( j) s to compute s O(r ) each C( j) takes O(r) tme n the worst case
Problem : / Knapsack The problem: Gven n objects p p p n prot w w w n weght M = the capacty o the knapsack Goal: nd x x x n s.t. n p s maxmzed subject to x where x = or n w x M
The straght orward way: Try all possbltes. The complexty s O( n ). Example: 6 5 M w w w p p p n x x x x w p x 5 6 6 5 soluton
Does Greedy work? Ths problem can t be solved by Greedy (P / W order) Counterexample: M = 6 P = 5 W = Greedy X = () total prot = 5. Optmal Y = () total prot = 6. The Dynamc Programmng way: Let (X ) = max prot generated rom subject to the capacty X want to nd n (M) Dene unctonal equaton: ( X ) max ( X ) p ( X ) ( X W x x x x... ) x
Example: n = 6 p p p p p5 p6 w w w w w5 w6 5 7 M 65 To nd 6 (65) : 6 (65) max 5 (65) max (65) max (65) max (65) max (65) max (5) max (5) max...... 5 (6 ) max...... 5 (65 ) (65 ) (65 ) (65 ) (65 ) (65) (5) (5 ) (6 ) 5 (6 ) max{6 6} 6 (58) 7 max{6 57} 6 (55 ) max{5 6} 6 (5) max{5 } 5 (5) 5 max{5} 5 (65) (5) (95) 5 max{ 5} (55 ) 7 max{657} 6
The result: x x x x x x 5 6 6 5 5......... Thereore the complexty o / Knapsack s O( n ) 5
M+ n+ j-w j M - - (j-w ) - (j) n +p (j) (M+)(n+) entres and constant tme each O(Mn) 6
Example: M = 6 Object Object Object Object p 8 5 w 7 5 6 +8 7 7 7 7 7+5 8 7 9 Max prot s By tracng whch entres lead to ths max-prot soluton we can obtan the optmal soluton - Object and (or and ).
Problem 5: Travelng Salesman Problem (TSP) 8 The problem: Gven a drected graph G = (V E) wth edge cost tour (or Hamltonan Crcut) o G s dened as drected cycle rom a vertex to tsel that passes through every other vertces exactly once. Goal: nd a tour wth mnmum cost
Example: G: C 5 6 8 8 5 9 9 Tour : [ ] Cost = + 9 + + 8 = 9 Tour : [ ] Cost = 5 + + + 8 = 6 9
The straghtorward way: Ths s a permutaton problem. There are (n-)*(n-)* * combnatons o tours so the complexty s O(n!). The Dynamc Programmng way: ssume startng vertex s wthout loss o generalty. Let g(s) = length o the shortest path startng at vertex gong through all the vertces n S and termnatng at vertex Want to nd g(v {}) Dene unctonal equaton: g(s) = C S = g(s) = C S + C S S = g(s) = mn{ C k + g(k S {k}) } ks S
Use the above example: g g g g mnc g( ) C g C g k mn k mn C g C g k mn 9 8 9 6 5 k mn C g C g k mn 8 8 5 5 k mn C g C g k mn 8 9 6 9 5 k Tour: Cost: 5 5 55 5 There are O(n n ) g(s) s to compute each takes O(n) computaton. Thereore the complexty o TSP by dynamc programmng s O(n n ).
lgorthm: procedure TSP (C mnlength); //assume startng vertex s { or to n do g() = C(); or k to (n-) or all subsets S V {} contanng k vertces or ( such that and S) g(s) = mn ( C(j)+g(jS {j}) ); js path(s) = value o j that gave the mnmum; } g(v {}) mn ( C(j)+g(jV {j}) ); jn path(v {}) value o j that gave the mnmum; mnlength g(v {});