0% found this document useful (0 votes)
41 views

Dynamic Programming

Uploaded by

Aryan Neelam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
41 views

Dynamic Programming

Uploaded by

Aryan Neelam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 43
ture, tions mes uM, at x. NK tion stil INTRODUCTION 41 : for oe rogramming approach for the a] pyaiations to sub-problems, much like the rence is that in case of divide-and. i oo ‘ bP and-conquer algorithm does mor, tpproblems. In contrast to thie, « just once and then saves its an te the same sub-sub-problem Programming, algo swer in a table, is encountered, Good news: Dynamic Programming (DP); ten have a number of; - inization problem may he ofs 4 fhan optimal value (maximum/minimum), Sndwe look for a sotution vi rithm solves every sub- "and avoids recomputing the anoncy a from financial models and operat; Applications range i S and operations research to bi i ggsithm research. So understanding Dac Profitable. eae eimanas 1. Characterize the structure of an optimal solution. 2, Recursively defines the value of an optimal solution. 4. Compute the value of an optimal solution in a bottom-up fashion. 4. Construct an optimal solution from computed information. Step 1-3 form the basi: s of a dynamic-programming solution to a problem. Step 4 can be ‘mitted ifonly the values of an optimal solution is required. When we do perform step 4, we 241 Th EEE Design and Analy’ n during the computation sometimes maintain additional informa construction of an optimal solution. Dynamic Programming (DP) is © Big idea: hard yet simple ; * Powerful algorithmic design technique ; | pee eae tial problems have a polynomial solution (gg, + Large class of seemingly exponenti DP ‘i shortest paths) * Particularly for optimization problems (min/max) (e.6 © * DP- “controlled brute force” * DP- recursion + re-use 12.2, MATRIX-CHAIN MULTIPLICATION Matrix-chain multiplication problem involves to determining ie bsniien retuence for performing a series of operations. This class of problem is ae will discuse the er design fy. code optimization and in databases for query optimization. eearecariee ta Problem in, very restricted instance, where the dynamic programming issues are easiest to See. Suppo we are given a sequence (chain) A,, Ap... A, of 2 matrices that are to be multiplied, ang is wish to compute the product AjAg Ay A product of matrices is said to be fully parenthesized if it is either a single matrix the product of two fully parenthesized matrix products, surrounded by Parentheses. Matyi, multiplication is associative, and so all will yield the same result. For example, ifthe gin chain of matrices is Ay, Ay, Ag, Ay We can fully parenthesize the A, A, A, A, in five disting ways: AAJA, AD), a (AA AD), (AANA, A), (AAA) AD, (AAA) A,) ‘The way we parenthesize a chain of matrices may have a dramatic impact on the costal evaluating the product. ofa Suppose we use the naive matrix multiplication algorithm. Then a multiplic P xq matrix by aq xr matrix takes pgr operations, Matrix multiplication is associative, #0 We can evaluate the product in any order. However, some orders are cheaper than others Find the cost of the cheapes: »ne, The naive algorithm multiplication is given by the following pseudo-code. MATRIX-MULTIPLY (A, B) 1. if columns [A} + rows [6] 2 then error “incompatible dimensions” Dynamic Programming for 7— 1 to rows [A] else go for j— 1 to columns (8) 5 do Cli, JJ — 0 i for k — 1 to columns cy . do Cli, : Ae cy, A+ at, kK. Bk, te 5) Fo sa.ipwe multiply from right to ter, ye 12: M=M,.(M,. : oe 1° Mz. (M,. My) [ox20 | [20x50] [0x1] Fxceo 5 0x 400 Cost= 50% 1 x 100 = 5009 20710 y 0 Cost = 20 » 50 x 100 = 100,000 10x 10 x 100 Cost = 10 « 20 x 100 = 20,000 Fig. 12.4. Cost = 50 x 1 x 100 = 5000 Cost = 20 x 50 x 100 = 100,000 - Cost = 10 x 20 x 100 = 20,000 ruta cost is 5, 000 + 100, 000 + 20,000 = 125,000, a However, ifwe begin in the middle: M = M,.4,.M,).M, 10x20 | [ 20x50] |_s0x1 ][+x100] 20x14 Cost = 20 « 50 x 1= 1000 10x14 Cost = 10 x 20 « 1 = 200 10 x 100 Cost = 10 1 x 100 = 1000 Fig. 12.2. Cost = 20 x 50 x 1= 1000 Cost = 10 x 20 x 1= 200 Cost = 10 x 1 x 100 = 1000 Toialcost is 1000 + 200 + 1000 = 2200 a = Design and Analysis of Algorithms Thus, computing the product according to the second parenthesizing is much fay. than the first one. i not actually multiplying matrices. 0, ‘The matrix-chain multiplication problem, is not act eee Tere cost, OM al is only to determine an order for multiplying m2‘ Counting the number of parenthesizations ; Let P(n) denote the number of alternative parenthesizations of a sequence of x ma When n = 1, there is just one matrix and therefore, only way to a ly Parenthesi matrix product. When n > 2, a fully parenthesized matrix product ae a Le luct of tw. parenthesized matrix subproducts, and the split between the two subproducts ma; trices, 20 the © fully Y Oceur between the At and (i+ 1)" matriess for any k= 1, 2, «1. Thus, we obtain the recurrence, fT ifn P(n) = : P(k) Pah) if N22 «) ke This is related to a famous function in combinatories called the Catalan numbers (whig, in turn is related to the number of different binary trees on n nodes). In particular P(n) . C(x — 1), where C(n) is the nth Catalan number: 1 Qn Ci) nt+i({n Applying Stirling’s formula (which is given in our text), we find that C(n) Q(4"n’), Since 4” is exponential and n®” is just polynomial, the exponential will dominate, implying that function grows very fast. Chain Matrix Multiplication Problem Given a sequence of matrices A,, Ay «.. A, and dimensions Pg, P; -.-P,» Where A, is of dimension p, _, x p, determine the order or multiplication that minimizes the number of operations. Dynamic Programming Approach This problem, like other dynamic programming problems involves determining a structure (in this case, a parametrization). We want to break the problem into sub-problems, whose solutions can be combined to solve the global problem. As is common to any DP solution, we need to find some way to break the problem into smaller sub-problems, and we need to determine a recursive formulation, which represents the optimum solution to each problem in terms of solutions to the sub-problems. Recursive solution We will store the solutions to the sub-problems in a table, and build the table in a bottom up manner. For 1 < i [is the chain length, 5. doforic iton-/+1 6. dojeiti -1 7. mi,jlee 8 fork itoj-1 %. do gq — mfi, kK) + mk + LH + Py Py By 10, if q < mi, J) l1. then mi, J] — 4 12, sli, le k 13. return m and s ~~ | - 286, = Design and Analysis ‘of Algorithms = ate the minimum costs for ehains of eng, 1 * First computes mii, i] <0 for i= 1, 2, % 2. It uses recurrence 2 to co; * During the first execution of the loop in lines 4 ~ 12. It ee Terrie second te mi, 1] fori = 1, 2, v1, the minimum costs fo" chal os me time through the loop, it computes mli, i + 2] for # = 1, 2s -» Sts for chains of length 8, and so on. * Ateach step, the mli, jl cost computed depend mili, k] and mik +1, J]. is only on already computed table entre, Constructing an optimal solution oe i tion of A,, A, ‘The following recursive procedure prints an optimal Pere acess taal 4 1, \s given the s table computed by MATRIX-CHAIN-ORDER an ea eeneeer a he initial | call PRINT-OPTIMAL-PARENS\s, 1, 2) prints an optimal paren’ vA AL, | PRINT-OPTIMAL-PARENS (s, i, j) 1 if j | 2 then print “A,” 3. else print *(” 4 PRINT-OPTIMAL-PARENS (s, i, s[i, j]) 5. PRINT-OPTIMAL-PARENS (s, $ [i, j] + 1, 3) 6. print ")” EXAMPLE 12.2. Find an optimal parenthesization of a matrix-chain product whose | sequence of dimensions is <5, 4, 6, 2, 7>, i.e., the matrices are as follows: AMS x 4) A,(4 x 6) A,(6 x 2) AQ2xD Solution: Initialization Step 1: Computing m[1, 2] By definition m[1, 2] = min (mfi, A] + mUk + 1,1 + p,_ pp) = m[1, 1] + m[2, 2) + ppypy = 0+0+5.4.6= 120 and k= = Dynamic Programming = 247 2: Computing m[2, 3] By definition step 2 m[2, 8] = min (mfi, b] + m{k + 155] +2.) = m[2, 2] + m[3, 3) + p,p.p, 0+0+4.6.2=48 andk=2 Step 3: Computing m[3, 4] By definition m[3, 4] = min (m[i, k] + m{k +1, J] + p;_ P,P) m[3, 3] + m[4, 4] + popsp, = 0+0+6.2.7=84andk=3 Step 4: Computing m[1, 3] By definition mL, 3] = min (ml, A+ mUk +1, 1+ p,_ypyp) 1,1. ket = min {rr T+ ml2,3] + poppy | m[l, 2] + m3, 31+ popy py ithms = 288. ss Dosign and Analysis of Algorithms 0448 45.4.2 aed te 4045.62 88 = mii = 88and k= = nin, = 98 Step 5: Computing m[2, 4] By definition ‘m[2, 4] = min (m[i, k] + m[k + 1, f+ P)- PP) hed . {re 2] + m[3, 4] + al min m2, 3] + m[4, 4] + PyP3P4 J pag « [0+8444.6.7 mn 484044.2.7 252 = mi = dk=3 min fe 104 ani Patalatat Po Py P2 Ps Pa Step 6: Computing m[1, 4] By definition m(1, 4] = min (m[i, k] + m[k + 1, f+ p;_ P,P) m1, 1] + m[2, 4] + Pop, P, | k = min jm[1, 2] + m[3, 4] + pop.p, 2 m{1, 3] + m[4, 4] + popsp, | =3 (0+104 45.4.7 = min }120+84+5.6.7 88+0+5.2.7 = Dynamic Programming » 244 min |414 = = 15 = isa 7 8 and k=3 Pa Po Ps Pa optimal; cost is 158. al Parenthesization we apply the procedure. rperefore the ow, for optim PRINT-OPTIMAL-PARENS (S, 1,4) g © Print &(* PRINT-OPTIMAL-PARENS (5, 1,3) __PRINTOPTIMAL-PARENS (S. 4,4) Print Ay Print" @ @ Prints (* PRINT-OPTINAL-PARENS (S, 2,3) PRINT-OPTIMAL-PARENS (S, 1, 1) ee Print Ay @ © PRINT-OPTIMAL-PARENS (S, 3, 3) ri Print (* PRINT-OPTIMAL-PARENS (S, 2, 2) Print Print A Prin” Print* 12.3, LONGEST COMMON SUBSEQUENCE . y,)) we say that ¥ isa Subsequence i) (isi, s Given two sequences X=(x, Xpy Xm) and Y= (yp Jy subsequence of X if there is a strictly increasing sequence of k indices (i, ip, i,<... Oand x; max (efi-1, j],clé,j-1) if i, j>Oand x, +5, When x, =y) wecan and should consider the sub-problem of finding the LCS of X,_, and Y,_ ,. Otherwise, we instead consider the two sub-problems of finding the LCS of X; and Y,_, and of X,_, and Y,. Computing the length of an LCS ‘The procedure for computing the length of an LCS is given below: LCS -LENGTH (X, Y) 1. m & length [x] 2. 1 © length [Y] 3. forij-—1 tom 4. do c[i, 0] — 0 5. for j— 0 ton ee = Dynamic Programming » % then cl, JJ -— cl -1,j-1) +1 10. bli, "SN" ue else if cf - 1, j] 2 efi, j - 1] 2 then cfi, j] © cli - 1, 9] 1B * bt, at 14 else cli, j] © cli, j - 1] 1 Fs bli, i} oe" a return c and b ow it works « It takes two sequences X'= (x1, x9, ...x,,) and Y= ( Jy Yoy ++ X,) as inputs. « Itstores the cli, /] values in a table c{0 ... m, 0 ... n] whose entries are computed in row- major order, i.e., The first row of. is filled in from left to right, then the second row, and 00. + Italso maintains the table b[1..., 1 ..n] to simplify construction of an optimal solution. Intuitively, b[i, /] points to the table entry corresponding to the optimal sub-problem solution chosen when computing cli, jl. + The procedure returns the b and c tables; elm, n] contains the length of an LOS of X and Y. Constructing an LCS The b table returned by LCS-LENGTH can be used to quickly construct an LCS of X= (ppp oe Xp, ANA Y = (4, Joy oes Yn )s PRINT-LCS (b, X, i,j) i Lifi= 0 orj=0 2 then return 3. if [i,j] = *N" 4. then PRINT-LCS (6, x, /- 1, j - 1) 5. print x; 6. else if b[i, j] = “tT” 7. then PRINT-LCS (b, X, i - 1, j) 8. else PRINT-LCS (b, X, ir J - 1) The running time of the procedure is O(m + n), since each ompute ach table entry takes O(1) time to Xow, it works * ‘The initial invocation is PRINT-LOS (6, x, tengtnpxy I. , length[y]), 282 = Design and Analysis of Al * We simply bi tre we encounter a“\” in entry bli, jl, in at b[m, n] and trace throu it implies EXAMPLE 12.3. Determine an LCS of (B, A, ©; Dy Solution: x= (B,A,C,D,B) y = (B,D, C,B) m = length [x] length [¥1 GOK) n=4 “2B yy=B ays A yy=D xy=C x,=D x,=B Now, for 0to5 cli, 0] = 0 and for Oto4 f0,j)= 0 Next, fori = 1andj=1 x, and y,, we get x, =; ~* \gorithms = rough the table following the arrows, Wh, that x; y;is an element of the Log B) and @, D, C, B). That is B=B eli, l= cli-1,f-U+1 ef, 1) = ef1-1, 1-1) +1=cl0, 0} +1 cll, = 0+1=1 and 6[1, 1] = “\” Next, for i= 1 andj =2 x, and yo, we get x; # Yo That is, B+D eli-1,/] = ef1-1, 2] =cl0, 21=0 eli, j-1) = cll, 2-1) =efl, =1 That is, e[1, 1] > c[0, 2] So, ell, 2)=1 b[1, 2] = “ee” Next fori = 1andj=3 x, and yg, we get x, # ys That is, B+ C imic Programming atis, ell, 21> (0, 3] ell, 3) =1 hi So b 11,3] =“ Next, fori=1andj=4 sz, and Jy W BEX, =, A qhatis B=B el 15-1 tet 3,444 = 610, 3)+1=04124 es ell, 4) = 1 b U1, 4] => Next fori =2andj=1 x, and yy, We get x, ty, That is A +B cL j= cl2—1, 1) <0, yaa elt, J— 11 = ef2, 1-0) = 12, 0) =1 ‘That is e[1, 1] > c[2, 0} So, ef2, 1] =1 b[2, 1) = “tT Next, for i= 2 andj =2 x,and yp, We get x, # yy That is, A#D eli-1,j] = ef1, 2] =1 cli, 7-1) = cl2, =1 That is, e(1, 2] = cl2, 1) So, ef2, 2) =1 b[2, 2] = «1 Next, for i = 2 andj = 3 mand ys, we get x, #y, That isA#C eli-1, f= elt, 8) =1 cli, j— 11 = cl2, 21-1 That is ell, 3] = el2, 21 So el2,3])=1 253 Design and 254 \\ b2, Next for i u 2andj=4 %, and ¥4, we wet x, 7 yy That isA +B eli-1,/) = ell, 41 eli, j— 1) = el2, % That is ell, 4] = cl2, 3) So cl2, 4] = 1 b12, 4] = “1” Next, for i= 3 andj =1 x, and y,, we get x,y, That isC 2B cli-1, J cl2, = 1 cli,j—1) 13, 0) =0 That is cl2, 1] > el8, 0) So ef3, = 1 (3, 1] = “1” Next for i= 3 andj = 2 x, and y», we get x, #y, That is C+D cli-1, fl = cl2,2)=1 eli, j- 1] = cf8, 1} =1 That is el2, 2] = cf3, 1) So, c[3, 2) = 1 [3,2] = “<1” Next for i = 3 andj =3 x, and y,, we get x, = y, that is C=C cli, l= So, 13, 3] = B13, 3] = “\" Next fori = andj=4 x, and y,, we get XFY, That is Cz B ~N=cl2. 241514122 = Dynamic Programmi 18, 8) > cl2, 4] qhat cf3. 4] = 2 go. 613, 4] 1 Ny efi-1, J] = e[3, 1] =1 eli, j-1] = cl4, 0] =0 e[3, 1] > c[4, 0} fae B cl, =. So, bl4, =“ yest for#=4 and j=2 g,and yp We get x, = 3, That is D =D eli, J] = cli-1,j-1)+1=03,1)41=141=2 Ps cl4, 2] = 2 b[4, 2] = “> Next, fori= 4 andj=3 x and yy, We get x, # yg That is D# C Cli-1, j] = C[3, 3]=2 Cli, j- 1) = Cf4, 2] =2 That is C[3,3] = Cl4, 2] $0, Cl4, 3] = 2 bf4 3] = “1 Next for i= 4 and j sand y,, we get x,=y, That is B= B efi, j] = cli-1,j- 1] +1=c[3,3])+1=2941=3 So, cl4, 4] = 3 b[4, 4] = “nN” ae ' x=BACDB y=BDCB 2 LCS=BCB 3 with Length 3 4 o]0 as o J —— Start here 12.4. 0/1 KNAPSACK PROBLEM A thief goes into a jewellry store to steal jewellery items. He has a speak (a bag) thath, would like to fill up, The bag has a limit on the total weight of the ol jects placed in it. Th, thiefs goal is to put items in the bag such that the value of the items is maximized and the weight of the items does not exceed the weight limit of the bag. Another limitation is that an, item can either be put in the bag or not—fractional items are not allowed. The problem is what jewllery should the thief choose that satisfy the constraints? Formally, the problem can be stated as follows: Give a knapsack with maximum capacity W, and a set S consisting of n items each item i has some weight w; and value v, (all w, yj and W are integer values) How to pack the knapsack to achieve maximum total value of packed items? The problem is called a “0 - 1” problem, because each item must be entirely accepted or rejected. How do we solve the problem? We could try the brute-force solution: * Since there are n items, there are 2” possible combinations of the items (an item either chosen or not). * We go through all combinations and find the one with the most total value and with total weight less or equal to W. Clearly the running time of such a brute-force algorithm will be O(2"). But we can do better by using an algorithm based on dynamic programming 0/1 Knapsack Problem: Dynamic programming approach * Ifitems are labelled /..., then a subproblem would be to find an optimal solution for S,, = litems labelled 1, 2, ... k} * Lets add another parameter: w, which will represent the exact weight for each subset of items. The subproblem then will be to compute BI&, w]. * We construct a matrix B (0... , 0... WI. For 1<=k<=n, and 0<=we=W,B th, wl il store the maximum value of any set of objects (1, 2..., #) that can fit into a knapsack weight w.BIn, W] will contain the maximum value of all n objects that can fit into the entire knapsack of weight W. Dynamic Programming 257 7 mute ontries of B we will imply an inductive approach. As a basis, B10, w a Tocite Wand B i, 01 = 0 for L<=i< =n, since if'we have no items then we oe We consider two cases: Leave object k If'we choose to not take object /t, then the optimal value will come veyut by considering how te fill a knapsack of size w with the remaining objeets (1, 2... fn i}. This is just BU — 1, wh. irake object fe If we take object £, then we gain a value of b,. But we use up w, of our capacity. With the remaining w Ww, eapacity in the knapsack, we can fill it in the best pssible way with objects (1, 2, .. ~ 1). po qhis is by + BUe ~ 1, w ~w,]. This is only possible if w, <= 0. ighis leads to the following recursive formulation: Bik -1, wl if wy >w Blk, wl = ee ee ata =1, Blk - 1, w — wy) + by) else Itmeans, that the best subset of S, that has total weight w is: 1, The best subset of S,_, that has total weight w, or 2, The best subset of S, _ , that has total weight w —w, plus the item k ‘The dynamic programming approach for knapsack 0/1 is given below; KNAPSACK (n, W) 1. for w = 0 to W 2. Blo, w] = 0 3. fori=iton 4 Bi, 0) = 0 5. fori=iton 6. for w = 0 to W 7 ifw item i can be part of the solution 8. if by + Bli - 1, w- wi] > Bli- 1, w) 9. Bi, w] = b + Bll - 1, w- w) 10. else a. BLi, w] = BEI - 1, w] 12. else B[i, w] = Bfi - 1, wl bw, > Ww The time complexity is clearly O (n.W). It must be cautioned that as n and W get large. both time and space complexity become significant. How to find actual Knapsack Items + Bln, W1is the maximal value of items that can be placed in the Knapsack * Leti=nandk=W if Bli, k] + Bli— 1, h] then mark the i" item as in the knapsack 258 = Dosign and Analysis of Algorithms # isi-1Lk=k-w, jt item is not in the knapsack i-1 p Assume the .n the optimally packed knapsack? > Could it be i EXAMPLE 12.4. Find the optimal solution for the knapsack 0/1 problem by using 4, dynamic problem approach? Consider, n= 4,W=5, (Wy Wy Wy Wy) = (2,3, 4 5) and (by by by by) = (3, 4, 5, 6). oon =o p iten 9{1 2 3 4 Solution: a [3 4 5 6 w, |2 34 5 Step 1: For w <0 to W BIO, w] = 0 iw 21 2 4 5 of oo ole 1 ie, BUI[]= 2| 3 4 Step 2: Fori<1ton Bii,0]1=0 w_o_4 5 of o[o olo ilo ie, BII[]= 2] 0 3] 0 alo Step 3:Foritem1 w, b, 2/3 Now for B[1, 1] TW if (w, < W) > FALSE So, Bli, W] = BG -1, W) BU, 1) = BU -1,1)=B (0,1) Bi, u=0 = Dynamic Programming m= o 12345 Cad —— of o[jo]olololo a[ 0 [to |2 |" | oO = a Buu > s[o Bit, 2) por Pip = True sow, if wy <= W) = wit@,+B &-1, W-w,] > Bl -1, w) wa+B [1-1 2—2]>B [0, 2) +3 0, 0) > BIO, 2] @+0>0) if > 0) > TRUE im, Bli, WI = ,+Bi-1,W-w) Bil, 2] = 3+ Blo,0] =3 iww,O 41 2 3 4 5 Fololojolofo t}olofs 2lo BUulll= alo alo For B[1, 3] Vi yi Now if (w, < W) = TRUE and if (6; + Bli—1, W—w) > Bli—1, Ww) if(8 + BIO, 11 > 3 [0, 31) if (3 +0) > 0) if > 0) TRUE Then Bli, W] = b,+ BE -1, W-w)) Bil, 3] = 34+ Blo, 1)=340 =3 259 i ms 20) Design and Analysis of Algor 5 o 12 iw ofopolo{o| ol? 3 polo]? ° 0 0 BUlv= a ww For B[1, 4) © vi ti Now if (w, <= W) (2<=4) => TRUE and if (b, + Bli— 1, Ww, > Blé- 1, W)) if (8 + BUL-1,4-2))>Bi1-1, 4] if (3 + BIO, 2] > BIO, 4) if (8 +0>0) if(8>0) => TRUE Then Bli, W] = b, + Bli—1, W-w,] =3 + BIO, 2) =3+0=3 mw 0.1 2 3 4 5 of ofo]ofofo]fo 1Jo}o]}] 3] 3-3 2[o Biltl= alo alo For BU, §] Tw Now, if (w, <= W) =(2<=5)—= TRUE and if (6; + Bli- 1, W-w,] > Bli-1, W] if (3 + BIO, 3] > BIO, 51 if(8+0>0) if (3 > 0) =» TRUE Then Bli, W) = b, + BE -1, W-w] =3 + Bl0, 3) =3+0=3 BUS 2 For item 2 pt Td W, b; por BIZ 1 ser tor BEST = W)= FALSE Bli, W) = Blé-1, W) = Bi, 1) =0 w 0 1 23 4 5 ofo]o]olo]o]o sPofolalstals 2| 0 [to Buus alo 4} 0 ate 2) fu, <=") #(3<=2) = FALSE Bli, W) = Bli-1, W] = BU, 2] =3 mo 2 sts ofofojo]o + 4 sfofo}3}3]} a] 3 oper BUU= oe | 4] o For Bia, a) tw tw, <=) = Design and Analysis of Algorithms (8<=3) > TRUE and if (6; + Bli—1, W-w > B(i-1, W) if (4 + BI2-1, 0] > BI2-1, 3] if [4 + BI, 0] > BI, 3] if(4+0>3) if (4 > 3) > TRUE Then Bli, W) = 6; + Bi-1, W-w] Bl2, 3) = 4+ BUI, 0)=4+0=4 w 0 1 2°93 4 5 Oo} o;ojofojojo tJolol3}3}3a]3 2}0]}0]3.4 Biltl= 3} 0 4; 0 Now, for B 12,41 ? i if (w, <= W) (8<=4) => TRUE and if (©; + Bli—1, W—w] > BG —1, W) if (4+ BI, 1)) > BIA, 4) if (4+0)>3) if(4>3) > TRUE Then Bli, W) = ,+ Bli-1, Ww, Bl2, 4] = +Bll, Y=44+0=4 iw _o 1 2 3.04 5 O}ofoto O;ofo 1} 0 0 3 3 3 3 Bulls ate eM alis 3} 0 4] o T | For B[2, 5) id a if wy <= W) @<=5)= TRUE and if, + Blé-1, W—w) > Bli-1, w) 6: For item 3 st yor for BIG, 11 te 2) flu < “41-2 FALSE Then, Bl, WI = Bi~1, wy BI3, = 0 oO 1 2 “EEE 3 0 9 Jol a[afs = Bl, 1) ofa sfofola BREE 0 | al BUllj= 1 2 3 4 Pop 8 2 iw io, <= W) 4<=2)-3 Pargp Then, Bli, W) = Bli-1,W) = Bl2, 2] =3 28m = Design and Analysis of Algorithms # 3 4 5 “repepolede |? apolo|s[s[3|2 apofofs|[4|4|7 BUU= STs af o For Big if Ww; <= W) (4 <=3) => FALSE Then, Bli, W) = Bli-1, W) = Bl2, 3) = 4 ie, BI3,3)= 4 wot 2 4.5 of o[o]o oo afofol|s|s3]3]3 afol[ol[s|al4[7 BUlll= slolote ta alo For 2g 4) if(w,<=W) (4<=4)= TRUE and if (6; + Bli— 1, W-w] > Bli-1, W) if (5 + B [2,0] > BI2, 4]) if(6 +0>4) if (5 > 4) => TRUE Then, Bl, WI = 6, + Bli-1, W—w) BIS, 4] = 5 + Bl2, 01=5+0 BI3, 4] = 5 w 01 2 3 4 5 ofolo]o]ololo ifofo|3{[s]als euus eetetstel? 3} o}o]3 [as 4) 0 Dynamic P in namic Programming _ For BI 51 ri if, <= W) (4<=5)= TRUE ond if @; + Bl -1, Ww) > Bu 1, yy if (6 +B (2, 1] > BI2, 5)) if +0>7) if(6 > 7) => FALSE ‘Then, Bli, W) = Bu~1, wy = Bl2, 5) =7 ie, B(3,5)=7 wo 41 2 3 4 5 otolofo]lolo Yolols[alals Sfolslalaly Slolsfalslr 0 Step 6: For item 4 Now for B[4, 1] ii tw if <= W) =(5<=1)> FALSE Then, - Bi, Wl = Bi-1, BI4, 1) = Bla- 1, 1) BI3, 1) = ie, Bl4,1] =0 iw _0 2093 4 5 Oololo O}o}o olo}sa}3 3]3 | * 2lolo sfalz] Blltl= oO 1° Si 4}s|7 4 o [to (Bs For Bl, 2] iv 0, <= W) =< 29) 5 FALSE Then, Bl, W) = Bu-1, w) = BI3, 2)=3 288 = Design and Analysis of Algorithms ie, Bl4,2)=3 wo 1 2 3 4 8 of ofofo]ol{o]2 afolo[s]{3]3]% ajofo{s}4|4}7 - 3 . nos Jo of {4|s|7 alo | o/s For B o- BI4 iw if w,<=W) (5 <=3) = FALSE Then, Bli, W)= Bi'-1,1 = BI3,3] =4 ie, Bl4,3)=4 wo 1 2 3 4 5 * ofofo}o}o}o}o 4Jo}o}3a}3}3]3 o}ol3}4a]4]7 BUultl= ofols {al s}7 alo] ol] [ts For B[4, 4] it tw if (w; <= W) (5 <= 4) > FALSE Then, Bli, W] = Bli—1, WI = BIS, 4] =5 ie, Bl4,4]=5 wo 1 2 3 4 5 o}o}ojo}]ojojo 1fofo}3]3]a]a 2}o}o}3a}4a]4]7 BUull= s}ofo}|s}4|is}7 alolo|3|4 |'s Dynamic Programming [267 For B[4, 5] ftw, <=) (6 <= 5) = TRUE and if 6, + BU-1, W-w) > Bie, wy if (6 + B (3, 0] > BIS, 5)) if(6+0>7) if (6 > 7) = FALSE Then, Bl, W1 = Bli-1, W) = B13, 5) =7 ie, BI4,5) =7 iw HOME ORES IERAEar olofofofo]olo fofols{slalsa alofolalalalz BUll= sfofofalals|7 stololsfals |r Method to find the actual knapsack items = The value of Bl4, 5] = 7 which is the max possible value that can be carried in the knapsack. Step 1: iw 0 1 2 3 4 5 O}ofoflololo oO Yofol3isa}s3 3 2)0;o};3]a}a4 7 Btltl= 3 Olol3/als5 7 4fofols}4]s | rt stattere For BI4, 5] i 3.4 oO Poy;os3s]/3]fa}3 2zfolo}3l/alal7 Bll(l= s}ofols|ats 7 alololatats led 2085N Design and Analysis of Algorithms: Bli, kl 4 Bi 1, bl BIA, 5l + B13, 5) => FALSE So, the 4th item is not selected then ie i-1 ie, i 8 Now i= 3, k=5, wj=4 wo 1 2,3 4 5 ofoJo]o{o|e]? sfofo[ 3] 3} 3] 2 atololal4[4i™) BUll= soPoTa ele lWa alo{o]s|4|5|7 Bii, kh) # Bli-1, 2) BIB, 5] + BI2, 5] > FALSE So, the 3rd item is not selected then iei-1 ie, iced Now, i=2, k=5, w,=3 wo 12 3 4 5 ofofo]o}o}o}o afofof ata] 3 |e) re OBE a7) afo|o|s|4]s5|7 afo}o}3a]4|s5]7 Bli, k]) + Bli-1, A) Bl2, 5) + BU, 5] = TRUE So, the 2*4 item is selected and i@i-landkek-w; ie, ic Landke2 Now here i= 1,k=2,w,=2 1 ofolfo 1}olo olo BUltl= e solo 4}olo Bli, hl # Bli-1, pj Bil, 2] # BIO, 2) = TRUE go, the 1"* item is selected. and 6 i-1kek-w, ie, £-0,ha-O mw Oo 42 3 4 5 of ololmfolo]o Qo] “sls [a ls enn. U2 sfololslalsl7 sfololala]sl7 Thus, the selected item, which are carried by knapsack are item 1 and item 2. 12.5. THE RESOURCE ALLOCATION PROBLEM + Aresource allocation problem in which m resources are to be allocated to n projects. If Jtesources are allocated to project i then the profit is Pl * The problem is to allocate the resource to the n projects in such a way as to maximize total next profit. * This problem can be formulated as an n + 1 stage graph problem as follows. Stage ‘,0 golutior Po= 30,P, = 1, Py= 40, P, = 10, P, 95 m1, 2) = m{1, 1) + mia, 21 +P) P,P, = 04+04+30x1x10 = 1200 andk=1 m[2, 3) = ml, 2] + mi3, 3) 4 P, P,P, 0+041x 40x10 = 400 andk =2 m(3, 4] = mis, 3] + ml4, 4] +P. PsP, = 04+0+40x10 x95 = 10,000 and k= 3 mill. 1] + m2, 8]47, PP, m(t, 3] = mill, 2]+ m3, 3] +P, p, Ps in {9+ 400 + 380x110 = 709 = mn 1200 +6 + 30x 40x10 =13,200 We take min [700, 13, 200), i.e., 700 andk=1 in [M22+n 03,4147 Bp, ells wea P,P, = min 0 +10,000 +1x 40x95 =11,000 ~ 400 +0+1x10x95 = 650 We take min (11,000, 650], ie., 650 andk=3 mL +m[2, 4) 4 PPP, m1, 2] +m [3,4] 4 PyPP, mL, 3] +m [4,4] 4 PPP, 0+ 650 +30x1%25 =18, 509 = min 1200 + 10,000 + x 0x 40x25 = 41,200 700 +9 +30x10x 95 = 8,200 0], i.e., 1850 andk=1, m(l, 4] = min and Analysis of Alg 274 Desi Therefore the optimal cost is 1851 Now, for optimal parenthesization we apply the procedure. PRINT-OPTIMAL-PARENS (S, 1, 4) ® @ Print PRINT-OPTIMAL-PARENS (S, 2, 4) PRINT-OPTIMAL-PARENS (S, 1, 1) Print a, Prin (* PRINT-OPTIMAL-PARENS (S, 4, 4) PRINT-OPTIMAL-PARENS (S, 2, 3) Print A, Print" Print *)* 9 ® | Print" (* PRINT-OPTIMAL-PARENS (S, 3, 3) PRINT-OPTIMAL-PARENS (S, 2, 2) Print, Print A, Print™)" Thus the optimal solution ios (A, (A, (Ay Ag) A,)) EXAMPLE 12.10. Which is a more efficient way to determine the optimal number of multiplications in a matrix chain multiplication problem numerating all the waysof Parenthesizing the product and computing the number of multiplication for each, or running RECURSIVE MATRIX CHAIN ? Justify your answer. Find and optimal Parenthesization of a matrix chain product whose sequence of dimensions is (5, 10,3, 12, 5, 50, 6). (UPTU, MCA, 2005-06) Solution: Refer Section 12.2. (6, 10, 8, 12, 5, 50, 6) P= 5, P,=10, P, =3, P, = 12, P, =5, P, = 50, P, m(l, 2] = Py x P, x Py =5 x 10 x 3 = 150 m[2, 3] = P, x P, x Py =10 x8 x 12 = 360 m[3, 4] = P, x P; x Py=3 x 12 x 5 = 180 mld, 5] = P, x P, x 2x 5 x 50 = 3000 ml[5, 6] = P, x P; x Py =5 x 50 x 6 = 1500 m{1, 1] + m[2, 3]+ Py xP, xP m1, 3] = min m{1, 2] + m[3, 3] + Py x Py xP; . Dynamic Progaming “ | 275 Aan "M604 Oxy HOV 04 Bx z rnin {1 1600 = 900. 1504180 © ang 30 at hag Pieler [meas misao m2, 3]4 mil4a}ap F (; = min 1x Phx Py Px Py 1804 10xa%5 GO+0410x12%5, = 330 at k=2 mis, . m3, 3]+m[4, 5]+ Px P,P, min m[3, 4]+m[5, 5] + Pp x Py x Pr, mi 0 +3000 +3x12x50 " 118040438x5%50 , {8000 +1800 = 4800 min 180 +750 = 930 930 at k=4 0 ml4, 6] ._ {ml4, 4] + m5, 6]+ Py x Py x Py iP | m[4, 51+ m[6,6] + Py x Ps x Ps _ {01500 +1256 ™P'13000 +0 412x506 __ {1500 + 360 = 1860 MIN } 4.990 +3600 = 6600 = 1860 at k=4 m{1, 1] + m[2, 4)+ Pox Px Py m{1, 4] = min} m[1, 2]+ m[3, 4]+ Pox Pex Py , m{1, 3]+ m[4, 4] + Po x Ps x Py 0+330+5x10x5 = min}150+180+5x3x5 330+04+5x3x5 276 4 Dosign and Analysis of Algorithms # m[3, 6] = mil, 5] = 330 +250 = 580 min/330+75=405 =405 at b=2 380 +75 = 405 ‘m[2, 2]+m[3, 5]+ 4 xP, x Ps m([2, 5]+m[4, B]+ Ax Pax Ps min m[2, 4] +m[5, 5]+ 7, xPyx Ps (0 +930 +10x3x50 min {360 + 3000 +10x1250 330 +0+10x5x50 '930 +1500 = 2430 min /3360+6000 = 9360 = 2430 at k=2 830 +2500 = 2830 ‘m[3, 3]+ m[4, 6]+ Po x Ps x Ps min {m[3, 4]+m|[5, 6]+ Pp x Pax Ps m[3,5]+m[6, 6]+ Pax Ps Ps (0+1860+3x12x6 min }180+1500+3x5x6 930 +0+3x50x6 1860 +216 = 2076 min}1680+90=1770 =1770 at k=4 930 + 900 = 1830 mi, 1]+ m[2, 5]+ Py x Py x Ps m[1, 2]+ m[3, 5]+ Py x Py x Ps m1, 3]+ m[4, 5] + Py x Py x Ps m[1, 4]+m[5, 5]+ Py x Py x Ps min (0+ 2430 +5x10x50 150+ 930 +5x3x50 330 +3000 +5x12x50 405+0+5x5x50 2430 +2500 = 4930 ‘min {1080+ 750 = 1830 3830 +3000 = 16630 = 1655 at k=4 405 +1250 =1655 m2, 6] m(1,6) min Oynamic Programming = 277 m[2, 2]+m[3, 6]+A xP, xP, m[2, 3]+m[4, 6]+ 2, xP, xP, m[2, 4]+m[5, 6)+P. xP xP, m[2, 5]+m[6, 6)+ A xP xP, 0+1770+10x3x6 360 +1860+10x12%6 330 +1500+10x5x6 2430 +0+10x50x6 1770 +180 = 1950 22204720 940 = =2 1830+300=2139 =1950 at & 2430 +3000 = 5430 mf1,1]+m[2, 6]+Pyx Px Py m1, 2]+m[3, 6]+ Py xP, x Ps m[1, 3]+m[4, 6]+ Py x Ps x Ps m{l, 4]+m[5, 6]+ Ry xPyx Py m{1, 5]+m[6, 6)]+ PyxPyxP; 0+1950+5x0x6 150+17704+5x3x6 830 +18604+5x12x6 405 +1500 +5x5x6 1655+0+4+5x50x6 1950 +300 = 2250 1920 +90 = 2010 2190 + 360 = 2550 1905 +150 = 2055, =2010 at k=2 1655 +1500 = 3155 278 = Dosign and Analysis of Algorithms = _ Therefore optimal cost is 2010. ‘The optimal solution is (A, Ay) ((Ag Ay) (Ag Ag)))- Fig. 12.5. EXAMPLE 12.11. Write an algorithm for LCS problem. Determine LCS for following: (10010101) and (010110110) (GGS IPU, M.Tech, 2009.19) Solution: x = (1, 0, 0, 1, 0, 1, 0, 1) y= (0,1, 0,1, 1,0, 1, 1,0) m = Length [x] n = Length [y] m=8 n=9 ° yi o xi] o 14 0 2 0] o 3 0] 0 4 1{ 0 5 o| 0 614 0 70 0 |4— start m=81] 0 2 | here From the table we can say that LCS = 6 One longest common sub sequence is (1,0, 0,1, 1,0). = Dynamic Programming = 279 2.12. Find optimal solution to the knap anne pl LE blemt approach? ic ie W=3, (wy Wy Wy) = (3, 2, 1) and (by by by wack O/1 problem by using the onsi = (2, 3,4). otlon® step 1+ Step 2: For i = 1 copying the values of Row (i — 1), i.e., Ry wo till (W, - D) column, i. )= (3-1) = 2 columns w-2 Now calculate this value 2 3 Now the value of table {1, 3} is calculated as = Max (0+(b,=2), 0) ip, teP 3 For i = 2 copying the value of Row (i ~ 1), i¢., Row 1 till (w, — 1) column, i.e., D= (2-1) =1 columnas. 200 \ # Dosign and Analyale of Ne vey (ny = MQ) Maas = DD Stop 4: For copying the yaluo of Row (FD, hts Row 2 UIC, VY) column, ‘6, (wy = = 1) = O column, Max (34(b31), 3) = Max (De(bye1}, 0) = Max (O4(y" 1), ot = "4 ° a _2{ 7 | 0 ° 9 0 lo q o 0 3 ° 1 3 0 0 0 ) 0 1 0 0 0 2 2 0 0 3 3 3 0 1 3 4<— Start here So, the selected items are item 2 and item 3. * Dynamo Moguamming [21 ) Mind optimal wolution to the knapuack Ol inutancen n « m= 16. Oe? PD = (10, 5 18, 7% 6, 18, 1) and Wye MPSA AAD, golutlon: ayo S48 6 7 8 9 wo nH we ww mM 48 feo o oO o 0 @}eo ° (©) s]o 0 ®lo «6 ®] eo « 34 7]o 3 34 Start Thus the selected items are 1, 2, 3, 5 and 6, EXAMPLE 12.14. Let R(i, j) be the number of times that table entry m{[i, J] is referred while computing other table entries in a call of MATRIX CHAIN ORDER. Show that the total number of references for the entire table is SY RG 2 nian a 3 Solution: Each time the I-loop executes, the i-lonp executes n —1+ 1 times. Each times the iloop Srccutes, the k-loop executes j—i = 1— 1 times, each time referencing m twice. Thus, the totel number of times that an entry of m is referenced while computing other entries is y DRW Yo-eye-y? 282 = Design and Analysis of Algorithms Qn(n-1)n — 2n(n-1)n(2n-1) EXAMPLE 12.15. Show that a full parenthesization of an n element expression has exact, n - 1 pairs of parentheses, Solution: Proof by induction : n — number of characters p — number of paranthesis pairs Basis (AB) n =2 p=1 (AB) C) or (A(BC)) n= 3 p=2 Induction: Assume true for k characters, prove true for k + 1 based on assumption, Assume parenthesization for [ABC...k] that n = k and p = k — 1. Add another characte (IABC ... k] M) n= k+1,P=k-1+1=k ‘Thus assumption is true for all & > 1. Proof by translation: The basic structure of binary trees and parentheses pair, any parenthesis sequence can be represented using a full binary tree — intemal node, (xy) is represented by representing the pairs x Y — Leaves, represent the elements It is commonly known that the number of internal nodes in a full binary tree is one less than the number of leaves. All parenthesization result in a full binary tree representation. EXAMPLE 12.16. Show how to reconstruct an LCS from the completed c table and the original sequences. X = (xp 2 wy Xp) ANA Y = [poy ney Yq) int O (m +n) time, without using the b table. Solution: We can eliminate the b table altogether. Each cli, f1 entry depends on only three other c table entries efi — 1, j— 1], cli —1,,] and eli, j, - 1]. Given the value of cli, j], we can determine on 0(1) line which of these three values was used to compute cli, fl without inspecting table b. Thus we can reconstruct an LCS in O(m + n) time using a procedure similar to PRINT_LCS. Although we same @(mn) space by this method, the auxillary space requirement for computing an LCS does not asymptotically decrease, since we need @(mn) space for the ¢ table any way PRINT_LCS (c, x, y, i, 5) 1. ifi=Oorj=0 2. then return = Dynamic Programming » 283 then PRINT_LCS (c, print x . elseif x; * yj and c[i, j] = then PRINT_LCS (c, elseif x # yj-and cfi, j] -4ui then PRINT_LCS (c, x, y, i.~ 4, 5) 10, else 41. error eexawaw (’N EXERCISES L 2 Explain dynamic programming ? Discuss through the example. Analyze the longest common subsequence from th s (4, B, C, D, B, C, D, C, D, D) and (B,C, D, C,D). Explain O(n”) time algorithm to find. ofn number. 1¢ given two sequence of characters. ‘the longest monotonically increasing sub-sequence of a sequence Q23

You might also like