0% found this document useful (0 votes)
17 views38 pages

DP Class 03 Notes

Dynamic programming

Uploaded by

Kishore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
17 views38 pages

DP Class 03 Notes

Dynamic programming

Uploaded by

Kishore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 38
DYNAMIC PROGRAMMING CLASS - 3 1. Painting Fence Algorithm (GFG) Problen Statement: The painting fence algorithm determines the number of ways to paint a fence with multiple 'N’ posts and K" colours. The algorithm ensures that at most 2 adjacent posts (no more than two adjacent posts) have the sane colour. Since answer can be Large return it modulo 16°9 + 7 (1000006007) Examples Input: N=1K=3 Input: N=2K=3 Output = 3 output : 9 Posts \ WD ms OOO Ra B W=4 aao RGG Bag Bf DU) KEE yy ues] 2362 : 2 FD Ep) et DE) ory poo pee Py fet jisee Ge tio nak gas | 2(bx2) + 1) 2 nas . . KAR) A5RZ 388 Bak AD 12436 B oem with D 4B waS Nes samb es | Fay) et FCDgame = FLY #LEAD fad = flea) LE) feagngla 3#% 2 buys: pitt © | & la Fes RRSR ae wa oa (RY) JL) pige = samee Hl) + vide (Hel) = ei) C same vif) 2 wc Fa furdi d 203) i FP 1B ways i. = Total was fo = Fl same + Fl3) DIFF 2 [Fined ced] + Cheney » ce) Tora oop Fo= (Rey ¥C Fema) + FLn-) ea ecu nstul n pevarsoy yo = 2. NEF Pcuil (5) 74 be 23 call ig ate ty pate) ce Te) Ee out) 9 5 0(2") TOP ADO? Bore) eee 1 fls) oe oor ‘Approach 2: Top Down Approach 3: Bottom Up TOP DOMN: Traverse from N to 2 TOP DOKN: Traverse from 1 to N ‘Approach 4: Space Optimization Aw RECURSIUG RELATION pec ty «forci-sy + vec ialfe let) > pets) pp ci-a] DPL Ip vePnd on a6 fins apne 30 wt oy, 0? rng Sac na Lemme EE pater #0 pantp Honk osKH so a Run HA! pecsy= (orcart ore a) + (ie) (9 +3) ¥139 D UDA 224 ‘por 39 + vPLAL) # OEY ppl) + (aut a} #130 2 (33) #@* 68 = Paevd = pavd Paevs = CoAR aint Compuriog 2 tn) ‘Spaa conossig 2 lt) wet pis ponder of pare Problem Statenen Given W ttens where each item has some weight ond profit associated with it and also given a bag with eapactty W, (i.e., the bag can hold at most weight in tt), Return Kya Kerona Hat: The task is to put the itens into the bag such that the sum of profits associated with them is the maxtmum possible. tote: The constroint here is ve can etther pat on item = Completely into the boy or connot put tt ot all a Cie ts nok posstoce fo put @ port of an tten into the bag) eapsack Problem Examples: Input: N= 3, W= 50, Output: 220 Input: N=3,W=4, output: 3 Input: , W = 6, output: 65 Input: N= 3, W= 3, Output: 0 weight[] = (19, 20, 30}, profit[] = {60, 100, 120} weight(] = {4, 5, 1}, profit(] = {1, 2, 3} weight[] = {1, 2, 3}, profit[] = {1@, 15, 40} weight[] = {4, 5, 6}, profit(] = {1, 2, 3} ‘Approach 1; Recursion Inclusive and Exclusive Pattern Toms oweput: 220 wey [io | 20 | 30 park [go | 100 | 120 swt [o | s [ae fas ne 3 ot Oe pity we fund cn wits 7¥ go 720 aa a uy we [so] #t[p ao i : BAO KMAPSPER] Inctosion © pecvusion = put Zeno pashit + wt evan ihine We Petit Ree REC max Parfit = marl inewsion , Exetsion) if cinds ye WJE wneton 0 pabl J OR He inax >= net) € TE we cinerd go AY ; 058 Wet] = (4, 5, Thy profiel] = (te 2,3} omit) ( erett (a » re pe fo! 2 | ot |e | ot cone ote 0 ee | af alata 10 oll wks aw i | orf -t [1 ow _ wel wt 2 [ei] -1 | AE palo [oof aa Input: N= 3, W= 4, weight(] = 7 na OneL] = C4, $5 3, profiel] = (Ys 2 2b mn wet Fy eae ) uum 29 001? (vice!) Ze Row, Lina poeti® inc = O gow > O io inet 0") vent ae? 70 ott) sus Co row, |" Mg Po owt |? et Po oot? 7 | ["o oP, fo pol) Po Input: Wey W = 4, weightl] = (4, 5, 1}, profiel] = (2, 2, 3) ole Gate Output: 3 ve Laat, 2o4 © O wre) ‘Ge wu) Lowa paot it gam 27 Col 3 inci = 0 wi 2 O ae ste) marlin eu) we gutta 1? a oni) PO" ats SN bao gn tas Rov [am jor "0 Po por a wi ost Po Pa Po wos PP | i Po pons TY a Pet Po a 4, weight[] = {4, 5, 1}, profit[] = {1, 2, 3} ld v2 a wae 3 dameyy aon = 0 La wt (, = (treat) + wu) “Y ae . coh collet a Leva puoi rol? To "0 poo |? F -t aa gas > 0 tte mitted 7 ame meses ee ate pool) Po 30 Input: W=3,W=4, output: 3 “a a 2 ale weaned = (Ga, th, promt ola ete powe J (ice yao) Leggaa peti? ,3 eter wr as os ps o Input: N=3,W=4, output: 3 my DRowe | gum =) C01 2 incivdt = 0 ao et meumiled * 2 marl wel 013 a weight!) = {4, 5, 4 4, aD oroftel] = (3, 2) wg icéen “Ge a) Lia postir sbincor 1 61044) ont) ye" SY Las foo [ts] wll oe or Jon Row?) ‘Rol e s |'3 fo aor?) [1 a Po ww Po PP ~1 Po mPa Paka Pp 1 my — jum 27 £0170 inc = 0 spoons 2 cree a omar e3 = 4, weight] = {4 5, 4}, profit] = {1, 2, 3} ote ct wed Zak (vtceol Ze wou) Lows paot it i cot | gH) WO Stun 0 3) ye re ea mt) = a 4 3 Bh oroitl) = Os 2 2 cane anne ms sum 27 "* 2) bol 2 tonrday‘cs inctod = : ce wa paot it paoll = aren? linc 1 6x ) Input. N= 3, W= 4, weightl] = (4, 5, I}, profit] = {2, 2, 3} ore ooo oc et vi Lee iene ie “ae D a il gauks 2 010 pooruwnd 23 wereld rth : 2 etl! 238 Input: N= 3, W= 4, weight] = {4 5, 1}, profit] = (1, 2, 3} ore ve output: 2 wd cameyg gum 27 001% inctvd4 =0 gal 24 oe a eek (vtceol) Ze wu) Lowa paotit er 4 eter Gl wk as "Po 2 ore ed LBN sto) #8 marlincet | 6 wm) oe neg 013) zs S| 3 | 0 3is eS et [Pt o -iP-1 0 0 eB Input: N= 3, W= 4, weight{] = (4, 5, 1}, profit] = (1, 2, 3} output: 3 ote oo : : ze) we oped (ott tay’ze wo) uum 27 0012» Lowa pao i? Input: OS Me 4 mah) = a ; (4 5 2, wrestle 2D re Copan perief DRow ed eS ed one o conga gece) 7 Ligqua pootit ou my gaott = prpensrewnd 23 : oe sppwetted) owen! ous) 2 esl 83 a Input: wea wot weight[] = & 5 th profiel] = (2, 2, 3} i jis a oe ey DRow ed tf a) » : (erred Ze aio som 27 &0 Loggya postit wel inciuéd = O ms aul > organs eS wits alincen | 6H *) mils erm) * dae wes 0 13) pal a Input: Ne 3, Mn 4, weight] = {4 5 the puts 3 ole profit] = {1, 2, 3} Te we 1 a ae gum 2 Gl? * iced ‘Ge iow) Lowa paot it inclu = egret gant > orcreatwnd=D i wm) i) rl wie adh Ot 24 oni a ats lo Polo fo m3 | s [3 Po aol? sy [3 [Ss Po mPa Pa Pa Po ro Po hg ry Input N= 3, W= 4, weight] = (4, 5, output: 3 Oo Drowed jum 27 001?) incivd =O ga ppursted 7m a 2 ore tev elincen | 6% um) pod od nat 019 1), profit{] = (1, 2, 3} ore Wetee 7 tia <= oy) Lowa poi? owt} (" ver $ Row}? No “oO me wl se Fa Po Rowe | a Fadl ms Po mPa Ps Pa fo polo fg Po N= 3, W= 4, weight[] = (4, 5, 1}, profit[] = (1, 2, 3} Output: 3 ots ae oe z ny — (ice Z« i) aol Lite paotit Approach 4: Space Optimization Inclusive and Exclusive Pattern

You might also like