Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Dynamic Programming for Optimization Problems, Study notes of Design and Analysis of Algorithms

The concept of dynamic programming and its application in solving optimization problems. It describes the four steps involved in dynamic programming and provides examples of problems such as matrix chain multiplication, longest common subsequence, 0-1 knapsack, and shortest path. The document also includes a code snippet for the optimal parentheses problem. The typology of the document is 'lecture notes'.

Typology: Study notes

2022/2023

Available from 02/03/2024

saurabh-jaiswal-5
saurabh-jaiswal-5 🇮🇳

5 documents

1 / 41

Toggle sidebar

Related documents


Partial preview of the text

Download Dynamic Programming for Optimization Problems and more Study notes Design and Analysis of Algorithms in PDF only on Docsity! = em ¥ gues Unit - Dynan Ic Program mm ing - dn chyna c prog~ amnming clivide phe prob lern ete aie probleme J i4 Is applied for optimization problema - tuhen the &ub- brobleong > is appheable Shore dhe are — dependent =F Svb~ peobleena inferrmation + @ A dyno it Programe algerithrn solves each ard = then ~— Sawe® il gub — problem gue once answer IP a table @ Ft see bodtorn — Up approabh » Dynamic Programmi usea four ep fow calwing a problem — © Choracterize te Ghucture | of am optima solution 6) Rocurively define fhe value of an optimal solustin BD Corsptte the value = OF oplimal — solution, piel ing Godttorn - up fabhion « ase non Bam on infor mation - iia aii | ramble with pp — 0) Matyi Chain multiplice pion problem ® long G Common subsequence problero @ 0-1 Knap satk problem © A) paive — Shorte- pad problem S: Malia chain multiplication Problem —— Ta dhs problem to. find = he Bly: banenthesis v of A) Aa As — An madiced In Q she product is wy thal minimize dhe pumber of — revlfiblicadyon - This — peoblem is @poted as followsieg "Given a sequence (chen) ih ai wldtal Ay oP on pn matriceo — whevt for detardy ~~" i fi hag dimencion bi X Pi folly parea dhe size the product Wie —-— dn in wrest pha the rumber of Stale mH py cat mipira 1 Ze6 Qeb-O The Ghuelure Seb Ay aie jnto Ai, Aig, Alta - ~~ pe digg ipa mec liy ord Arti, Abo whee Ick SI A das, Jz& Kee m[s,6]= mss J + m [6 b]+pa% ps pe c+ OF 10%D20H AS = S000 &) m[13] del, kel2) J=3 mtd Fm faa] + bor py Pa m[h3J = of dbas + BONIS = 7035 im[o) + m [33] + pom ba %P3 = 19000 = e360 FO + BORIS WS m2, gr2, k= 43 Je4 | mp2) + m[3, 4] 4 by Pa™! b4 mae) ~ = OF 980 F ag nionlo — 980 + asAlso = refs] + mL 9] + Prt Po % Ps = abs tO # 288% 10 — arbaS+ 38h SO - 4375 6000 > 5] des = 3, fr - 3/4,Jes m m(agfe Pe nnd thas : S = 04 lovo+ isnsn20 m[a,ajern[s.€]+ hPa Ye L = S500 mae] = =o} S000 + S00 X25 13,4] + [SS] + hep bs = 6280 = Foto Is - em Sm10%20 ma, s] + m[5s]+ bats be lo = {000+ OF SM2ON2S =- 3500 ad m[aS] Jol, K=h23, d= . y at rm) + Jer, jr=2, 3,4 JES ple [35] + prt babe “= 0426004 3SAIS N20 = ooo ro [i,t }+ m9) +Po™ hy® bo = 4+ F3qS F.3ON3SNIO m[ta]= = 19635 rfi,a]+ mfa.t] + besa tal 15950 + 250+ 30MISAIP mpayaltm[es] Pm bps =— 2}oee — 2698 1000 4+ 38SMSN20 roy] + mf 4] + Pom ha Po = FAS = 9875 + OF BORSRIO m[2,%] y10[5S]+ py barbs = 908 = ae O+ B8A10 020 m[3.6J a j= 3,k-3,4S, J=é m[3,3]+(% 6]+ pa®bs™ Pe =0+4 3500+ ISAS WS — S348 3,6| = — rol | ]+mfsi6] + faxbar Pe mat = 2504 5000+ JSAJOWAS 9s 00 5 s]+m[os]+ ow ps "he = asoot OF JEH20 NAS — 0000 rm (1,5) Jv=), K=1,23,4d2S mf + m[2,S]+ bo% Pit bs m[tsJ= = ot 7125+ B0N3EAIO = 28 jas mfia)+ [3S] + bom Pa® Ps = 1$360+ 2800+ BoONISALO — 27250 m3] + in(a,S] + Po barbs = 9025 + 000 t g0ns$ N20 = & NOS fun] MES] bor Pa Ps = 9375 +0 + B30 A110 X20 = IS3eS m [26] m[ve] . jea, kash 5Ie6 jel, beLV2S I= rnp s]4 melt hibatPs inf, Ore(26]+ bor bi® Pe =o+ $37st QSN)ISALS =o !pseo + Zonas AS nbd) eqendenth 2a mfa3j+" He. 10 P38 a Os soot BSxSN2S m[yel- (mb) 4m[36]+ fon = 10500 =)9go +5375 + a0n)SM AS mM m[s,6/+P™ Ape = 32.05 fava} +m[Se6]+ Pom EMP sana] + m[asa] + Bohs Pe = 4375 +5000 + 2Sn10 426 — J@125 5 = 38754 3500+ Bons m2S < 1812S r[2sJt m[o,e] 4b bs PS m1 9J+ (56) + behg™ Ps = qjosrop 3SMI0NAS = 9335 4-So00 + BONIOM = 2462S = 2/695 m(ys] +m [66] + boxips MPs — 078 FOHG0% 2070S — .2607S "EE © PRInT-optsmaL-parens(s, SCI) punt ")” OTe —jnidiad call PRINT- OPTIMAL~PAREME(S).n) panto am opdirnct) poren thes) 204i Ou of <Ay, Ay.- 2? A> ° © Time com bl ewity of dhs it Or) - §: LongeGt Corermon Sobseguenc’ - Problem —— ee lati » giver two g problem, we oe fF ar dhe OL ‘e je ok Xm and Va cy, — Dp) _ cvigh -to ind a moxymury of x end y. Geb @ A _cvecursive_ Solution —— let Clig] be vhe Jenath of- an LCS of the sequenceg Xj and = -Y3 fag] = ° jf d=o OY Jao [tiat- "| = C [i1,d-] +4 if JjJ> 0 oud %i5% = moat [iS ebbari], 1 42> ommny tT Cae Gtep- @ Comfatting the dength of ian LCS = En- find LS of de Followive Sequence @ : ~ XK = <A) B, G B,D, AB > od Y= <8,D, CA, 8,A> Bolus, Make dh tbl Pe alYH) * o}| © 0 0 Oo 0 0 O op op | or JIN | lH] Is Jin fle[ tle] tt jan | aa Ip LIP} aAm fae] avr | AP ISLIP [ar par | sn] b= it anf aril 2r |] ar} 3tl Less BCBA tart atlanta an bs eh el > 0 0 0 0 lo © LCS- LEGTH (x,y) @ ms X length @® N= ¥- length ® let b [it )).-n] and c[o--™,0--” new tabled ® fr dc | tom © fio] =? @ fr Jao wr @® c{o,j] =? © for b= Lt © for pest ton if ~e= V9 cfagd= cli, gi] 4) bfd) = te” elseif cfa-l J] >velga- emg) =f ea] u) bid = ee a fg = Aa) + Ass) By Pa Ass bss - Bu) Pe: (ant A) (By + Bs >) Pes (Are >>) ( Pay | B,») = (An ~~ Ary ) (By) + Bp) Cy tl Pst Pr +R P,+ Ps rel p) Jedion to A: sy Quesiou- “Use Gr assen's matrin of fellow! feo rote nC - Compute bool ct 8 [i 2) 8 [a | » pe terse 4 pro GTS pr» (1t3)2 = Qo p= s(a-ge 7! Ps = (45) (6+) = 40 py 1-2) (648) =-08) Vg C= \+ Gas 14] Cay ba Gas 6t4e-7atot ; — & be jes MVPVFVVUYUVUULIVUELULLLULLL ULE CeCe see eee. : “ie 7 Ty = gen Pn} = 47[%) + Or) ifn >! Mfler solv thig ecu CNC E yelation, we get G.Greedy dlgerithm §: to Solve op dmizala (ireedy gorithen ave iced prob lem ‘ We will solve the B Ulowtied ¥ Uda freee problem > tsnapSack problem —> Minimum — anni fee froblem 5 Ghortest fork” problem (Dijak Shea algorithm) . =F Minimum os Jere non- cyclic sub: prabh . Shanrinf yee fsa Be of a connected and unarvected graph b that connecde al the veticco together * yu 7 ® for grnbh Cy can hove more d¢han one Sha nnik tyee - @ No cycle t hag ni Capes, whert @) Sparnia 4tyee uber OF nodee (vetices) * MST- A Spanrint eee jc said to & MST if gum of weigide of all dh eclpes In j gmoll eG , yw wo fo nis the nv D Prins _Algocith — © Br frimte algorithm cue get Connected Greiph at each Gep - Aperithes — MST- PRIM (0,w,7) © for each Ue OV @ they =e (8) uA = NIL @ They =0 : © 0= Ov @ wile © # . @ ne Berract-mzn (OJ for each VE Gad[u] sp veg ond w(tyy) <V-key 8 fo) yor =U 6) Vv. bey = w(iv) “Tate Complentte of his algorithm ofedgv + vig) = O[ Flv) » vie @ => Glution - a he c aJe] tals } é) [o]=[=[=/ [= [~l es] whe 6+ ¢4 {a} — Botrae} ee vag {a> Tbh) ~ S ~ ~ _ = = i while OF {o] <— Exbract MIN (oJ Ad 1 1bJ = 154) while OFO ccJ Le Endwact- -NEN( J ay iS= fh 4A) EEE while O + ; fa Ey drat-mrn(o) waren FEF LEE wlale oF $ {£] <— Bobrack -MTN(0) vagit) = tg fale | [| 12] h io > Qe wale oto a @ — _Pabeet MEN(8 Sghagh Adyt4 {h3 while OO | Leen Gadrat- MEN (6) fay {HJ = re os oe while OF ®. a je govat “PAM aa $457 {2 SNATIALT 2E ~SINGLE- SOURCE (6,8) @ for ewh verien VE V[O] @ diy-* = NZL a[v | re ® afs]= 0 > RELAX( YW) @ if di)> ele + wy gp = amg 4 eld @ n[v] = o Vv a- O74 & | RELAN( UY) WY Vv ~Gu “ed digk@na's Algorrh while OF 4d {she Entra} -MEN(©) Adj {s}= Ea) ery Rela (St) Relaw (S/Y) wile o+4 y3 —— bobact -NZN (@) agi < ee CERT whilt OF 9 ‘ ZEe- Fodacot _MIN(@) mgt (9 Fe a Ew ‘ ta a J) N (9) Ad: 0 a4 J Hi . oa Set Ss _u— E yw c 4 M a nc ) A ¢ std = Ls a {a 73 45 eo ac sof 1] Relays (uv) if af] ofa] Helo) div] = ofu] + fey) Relow (St) if dfvy > dl] + wey co> ode \ hue Relouro [+ x) ie fv) > al] +w [wy o > 6TS elon (% +) if ah] > du) +e Cry) 6 >II2 Relant LZ (HF gy eae] Fey) o> 6-4 Relays (2,%) if dw] > ala] + eer) See Relaro ( 2,8) fay rale] ¢ OY) op ata Fa lee Rela (S,Y) if ape) > of + OY True pela (2) faloe pelo (Hr) safe) > ofa] + le) due 979-3 Rela (4) fp ad{y] > ata] Feld) a> Falee > gfv]> ofa] + ew) coo > On| True Relos fA.c) +p gfv] >afe] + w (uv) o> ott Tue Aelox (BE) rod o> a-| wy 7 aed Fe (OY) True salah ns wa ra) 4g > 8-) True - Rela pat avy> a+ aint | oo > 734) Taue felon (0.8) if atv] > atu] ¢ w (uv) Ap ltl” Falae a ue » Ou e _ We {S, ]0, 20, 30, 40) Ve [80,20, 10°, 90, 160) a a W=60 meh WwW) Ss 10 2° 30 4 160 Pe pick she idea! Hen t , Hema, Her [ - — go4+ io + 160% ge = 2270 r Spyytion vectiou < (*, 0, 1,0) t) | Assume all dhe itemo an ru | Okcencive order pf pred valu® por weigle Frachiona).knapsark (w, Vv, W /) n= length [w] fr L=!40n do xf il =O ap 0 Ls] weight = O tuhile [4< =n ad weigh a w) do if (weight + wf] <= w) then else Huffman invented as preey thot con@euda and = Optima) profin Code called uf fran Code zt is) = we for en- bal and decodivt on the network security : code is set to be peofin code algorithm iL ‘ho bee A Be code word «ise fy» of — amother code word. = b— Ilo C Cc 10 BD Cu— Concider the Following 44, b,C) dy e, f3 auel Pe frequen of Aa iter faj= 4S , . f(s (2, f bs 13, f (até } £(I= 9, f(f)= FP fg He fubR cron code fer Has date i Shit he Frequenty of letter In Inowrenelo order eed, Fla lay PCa, fldel6 4(Ya 45 (J fe) (ee) [ore] fare) [ong F(fjs S, Ff Pids minimura Fequeay | ; Algorithm — Huffman (c) OD nr — lenpth[c] @ a@=cC © fr Jeti tr) @ do allocate a node 2 © x < Eatrat -MIN(9) @ Pe Exhact - MIN (OJ @ teft[z] =-* aigitld <— Fy — Fed + FU) D) insert (Z @) Cu— use greedy brapsath — Wy} [00 So qo 20 [0 JO vei go 38 Fe + Vi ’ 6.4 O.> oS O2d | 0:6 werget - 100 iow a g ems , Hem, Her G , item 7 of idem 3 J oF ast be rng | 3} Sp Tete 66 i oe Po fei hdd, LD