0% found this document useful (0 votes)
2 views21 pages

Dynamic Programming

The document discusses dynamic programming and recursion, highlighting techniques such as memoization and tabulation to optimize problem-solving. It covers various problems including knapsack, Fibonacci sequence, longest common subsequence, and coin change, providing insights into recursive solutions and their implementations. Additionally, it emphasizes the importance of storing calculated values to improve time complexity in recursive algorithms.

Uploaded by

anujdubey2308
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)
2 views21 pages

Dynamic Programming

The document discusses dynamic programming and recursion, highlighting techniques such as memoization and tabulation to optimize problem-solving. It covers various problems including knapsack, Fibonacci sequence, longest common subsequence, and coin change, providing insights into recursive solutions and their implementations. Additionally, it emphasizes the importance of storing calculated values to improve time complexity in recursive algorithms.

Uploaded by

anujdubey2308
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
DYNAMIC PROGRAMLN G7 2 Recursion . ; : Jh necursion we find all the possible solviong In OP we Store se Calwlated dada So that it } j impnows time complexity, Bt and =" n=3 . 6 © Top - down Gotten uP YF ) (mensiaation) (Tabvlation) 95 : (BoH up) om- @ 6d (Top - down) wri recursion solution fo store cdevlded valees in ip in dada strvcone @ Creake a Dota structure @ hile Hetwring out : 0 netomn opfny(n Tn OP, we Ye caluvlaFons aga” store Ieulede Soh 0” 4 Simple i ‘f ifs asked fo ® @ choice > choose oF leave @ Recursion call 22 (} optireal oviput (Max, Min ) se Types OF PROBLEMS yd o-4 Koapsacks d unbounded Knepsack 3) Fibonscls ad ics (os Longest common surseqper’) ag) eFse Chat inovconing sub seqpene) @) Kadene's Aly 3) mcm ( Mabtix chain rou! 1) pP on Trees —> Ensy FoR U a DR Grid jiplicabien ) ge = overlapping © Recusion — Memoization om EF + MemoizaHiow con used laler and gat seastsiO* (oP) (oP) ob need em So that we don) Problems ~» Tabulation vo) other s Even if DP exisis OOP) TLE another optimal so) wight 0 (n) / (097) right exists fo dhink first ey. °88 Greak oP (oor) a) Ocak, optima) a(nk) J HE Mow do Birite Recursion (ode Void recursion (int nt 0 input shoud decreue n-”-1 base Condition © Boe (ondition > heat main xh powible dnp yebum Fecuns ion (n= 1) 4g (n20) y 2 OP draw a choice dja gam ab +t To Sov ZN _— x H 0-4 KNAP-SACK profit sud Phat Given OW, w= UW, Wa Wy. We find max Ve WV Va Me swig W eHow 1 2 OPO choice drove vet © optimal ~ max profit © hone diagram A wisW wi>W oA : . n Size fm @ Recursion Sol (intr, Veit wW, Vint? Ve Wt | sn Profit if (nso Il wéo) —relwn 05 —,Qase conti | | else if (| Wm < w) | elunn max ( veo) Pratl (1-1, mhViN- ua) Pref a (ao eles vehuin Profib (r-) wiv. W) 3 Omani : mvization —> Just ald Storage (DP) U vectors int> > J DK in} DP (mW); —> Wn necwrsion only 2 Van (nw) wiv We change int Profit (™ Weo) —> netunn 0, it (ngoll else iF (oe tnafud==) € # (whl anitialis aise n (neoy) wo) >? fr (420 bemsitt) for (aro 3g 4) Ww pPfite) =° oP (01f] = ° for (imi 126 189 idaye ay (ink J? jew dtt)f fo (ih art i) VS ae 56d) ie (wri 44 aL, pesilta)® (rin ora ase psf) pe (i-ifa] 5 a Rew» ofa fu) © Subset Sum Problem f subset whose Sum is H Acpl: 2s 8 Sum = 11 present oF mot T/F > agi) vias afi)ssem Afi] > Sun JN \ we x 5 B boo! find (iat Array A, gum) C Recwr if (sun==° oT ie (0f 0) F : if aia csun) > * Ey relwin rea ter Basie) ) fin po son): y alse teburn (find (an se) j = b V Bal ig (sz) 77 it Cpe! a x) ge ere vey oP (aksun] =~ pete i if ( oi yar } | died C } 3 ; dhe eh pp farssun sind (nen eser) retwer pp [0 Cou") 2 V (vcboo! 7? DP (nav, Sunt V5 wg for (e090) OPEL) = 7 for (170 > Sum) + ppfo) (i= F for (1 O44 for (dO ) it (6==9 oprii)>7 (eri-ati- ann | opri-iNGe?) if (apt) $8)~ OPN (AT tue OFFA) > bP LI-G) return OP(T Gurr) e Mini Minimum Subset Sum Ol thurencé hen 5 anf) A! pivide A into oases 6 Ar find min \si-Sal a, grees pnsVAl® poeta hae Sece (62h) us 9s, = St x Good > Simple find all posible valves 4s> rin Wi-S chews e pe (ath st) i bool foun a, oP) (se) 97 hn ye (nso)? aF if [ort y =) " fort ee Sas cq) onteny= d(T Ajn-n A.0°) | fd 5009 6 on pemrfs) = fil ret 5 Ar0P) ele Felon pein) fs? int res = INT-MmA» for me 0 IE x-sl); (ind (381 yet) tee erin (te 2 s\); 3 "Ee Qua) sum partition Aagisn sy O/p = T/F > Simple find lange Sol: Se) in} Taaget = Sun (AP) > Gubse sum problem it (Tange 2. $0) oe ase T= 7/2 oo) Gnd (fat 91 ¥ int) AD 7pP) ine : (neo) a oo) ,7- flies), OF) ik (OP fear) ==-!) in = (find (7) Cit Laren ety OFF tT) ( ol (mT) aye pe fa)(t) = fad (7! (A710?) d retusa pein) © Count of Subset of 4 giver sunt eine & oe fas, O09, oashG@) - ponte vein) pp (ntl, Sunt!) 5 - : t os es . whalise >! G 0 sft « for (rian) ¢ ; t j: 1 Sum) . ane) retin OP [o}fsum) que diff erence eCount the no’ of Subsed oF (G29, 090) @ ~@ Aur A? wien $ pe + ue > g= Sum(A) Count no’ ¢ — = 2 anes asd oo © TarGeT Sum pen A:.! fos. oe rumen ke 89% as Sum i. = ae afi) 7» ee _» wes Ne Se sh g, + Se g Stoo = ont Si HF Ungounnen lenAPsacie 9 In this hore is unlimited 5 hy of: Hers oF each type (OX) Multiple occwranter of same ‘lem H 9, on st (ain < W) else rehonr find (n-), a Wd = unbowder ze Ain) sw Felund fod (1 alse nchun Find (n-tm¥) q i (eee (ercys Pei | bywebs fi Ce path fuel, ital Ws We. wy Va ind (n-} A WwW) A,W- and) * find 1 8) . Wr Va P)) . - CoIN CHANGE - L tein ft ve supply oF Coins ne es find Max no weys (414.3). > wu? paar ‘at cout vn, Sun (om) E a . (rt gmeeo) 2% ie < Sum oe ieee (206 EID inf), 0) - a . : Ly eon can (Se + count (nh Summ fol” se (ount (n=1, sam br" (min no’ of coins) 219759@ @ (oIN CHAN GE - L on: ! 23 Sum > S > prin (14 Gourd (el 1 Sun ~(0in6s tia), unt (0! nie) ound (n-1 1 $07 L089 te NoTE yewaP- SACK ve Unboundan fradiond | . ir (de) {orreeds) Lowest Cov Longest Comes Soesedvence (10 s) IMP ~y 15 Subproblems x: @V« D8 y: Oe Or result + ab dh > (¥) length, xX leghh 4 tog @ Revrsion — base conditien if (nezoll m==0) 0 > Smaller inp — rec {met en) er rec (mm! > choite dlagrom -F a ee_Qoao ) om Fee (n-1,m-1) e ™ | * | agn)= ¥fre) xin} br) | ¢ yx (0-0-1) imax. ( tom), (a.m-1)) \nt tes (mime VL yetneo leno) > 0 iF (xte-f)= yee) ves ( meh Y) | aye > max (4S tn-hien jXo¥) ECS (njm-HiX0¥) 4 | @ Meetiaation ene? int wes (nm mo7 OP) E [ le neolm=o) > ° ee o> if (DPI = “0 | if (xfn-idevin-i)6 = 14s (nen) op opin) “7, . [Link])) {les tn-tomixs YP) LCS (0-1 Xs Lb else pplnim) = > retunn OP obulstion ° aes initials ¥nmeO pp{n'fm) = 9 °F oy (edi isn 44) ntl fe (Qe dsm ig NE if (X-D == ypi-) pepiyfa)= 1+ DP-NS-19, else op fifi) = 9% (DPFi-IDEAD , DP ja-2) @ Nove > Vevcintr? OP (21 veinb (m41) ) Works ° longest common substring ne 1 wbede 9 Sy 0 ye. ab Fabe Sel! coms Wow = fo a xe Vad xsi) # Mla) v opal) | + opfi-")fd-!? 0 ae maw wmonces me substring (Nor or) @ longest Palimdno using Two poiler\ expand 0(n*) > ge 49 each cham, . Printing longest common subseqyence (ucs) jeg bedi’ @ stant trem max = Ea < @ Gro in neverse « b c c#a f fF abct — fe Shortest comron suporrs eqpance edits Sat tgeek”—meegng 0b ben agb vesu = geeke —> bs “eke* | “> | Gol: lalers Joln™ Pound > fi) ECS string travose neh a, if dale = U6sii? pop trom LCS resus = a+b— Lcs (a,b) ab end add Hemaing cas Les into w © Minin tm nia Gh of insertion & ddelion 40 make a—>b a: heap © b: Pea —» 1 delesion asheap > €@ G peas @ Sel: find bcs (heap, pea) —> insert & nemave othe tet > ben Motel +> m-Les Ins e longest Palindrome Subseqperre g; abbagaba © baaab Siz abbaaab? me > ues (62046) Siz S (rev)= abaaabba > us (s, Str) © SyorresT Come = ol) ; da, than in oFd O7 Traverse tHnough b and fin Two pointer — oP af ucs (ab) = lay > F la) > T of insention to make a string Jo make i palindrome 5 adebcbtda @ fe Minimum number G: aebcbhda Sol: find Length of maximum Palindrome substring Res = LCs (5, Sieveme) se M fu ATRI | X CHAIN MULTIPLICATLON (MiCM) 7 . Ldentifcation + Olvide g Conqyev + Doolean Posen thonin + Mn| Max Valve of an Exp © Palindyom & fanhHon eres ane — for (Kata) ; > at ik

You might also like