Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
17 views
38 pages
DP Class 03 Notes
Dynamic programming
Uploaded by
Kishore
AI-enhanced title
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
Download
Save
Save DP CLASS 03 NOTES For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
0 ratings
0% found this document useful (0 votes)
17 views
38 pages
DP Class 03 Notes
Dynamic programming
Uploaded by
Kishore
AI-enhanced title
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
Carousel Previous
Carousel Next
Download
Save
Save DP CLASS 03 NOTES For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save DP CLASS 03 NOTES For Later
You are on page 1
/ 38
Search
Fullscreen
DYNAMIC PROGRAMMING CLASS - 31. 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 BW=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 waSNes 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 = CoARaint Compuriog 2 tn) ‘Spaa conossig 2 lt) wet pis ponder of pareProblem 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 ProblemExamples: 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 auy 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 aaInput: 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) PoInput: 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 Poa 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 30Input: 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 oInput: 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 Pp1 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! 238Input: 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 eBInput: 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 aInput: 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 aInput: 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 ryInput 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 PoN= 3, W= 4, weight[] = (4, 5, 1}, profit[] = (1, 2, 3} Output: 3 ots ae oe z ny — (ice Z« i) aol Lite paotitApproach 4: Space Optimization Inclusive and Exclusive Pattern
You might also like
DAA Unit 4
PDF
No ratings yet
DAA Unit 4
50 pages
Design and Analysis of Algorithms
PDF
No ratings yet
Design and Analysis of Algorithms
46 pages
DAA Unit 4 (Greedy Method
PDF
No ratings yet
DAA Unit 4 (Greedy Method
17 pages
DP ClassV Notes 11th June 2023
PDF
No ratings yet
DP ClassV Notes 11th June 2023
9 pages
DAA Module 5
PDF
No ratings yet
DAA Module 5
26 pages
+2 CS Book Back Q & A
PDF
No ratings yet
+2 CS Book Back Q & A
107 pages
ADA Notes
PDF
No ratings yet
ADA Notes
50 pages
18csl76 Ai - ML Lab Manual - Akhn
PDF
No ratings yet
18csl76 Ai - ML Lab Manual - Akhn
24 pages
Week 9 Live Coding Solution
PDF
No ratings yet
Week 9 Live Coding Solution
12 pages
ADA File 11 TH - 16 TH Exp
PDF
No ratings yet
ADA File 11 TH - 16 TH Exp
25 pages
DAA Unit 3
PDF
No ratings yet
DAA Unit 3
14 pages
Untitled
PDF
No ratings yet
Untitled
12 pages
Daa 2+4
PDF
No ratings yet
Daa 2+4
14 pages
ADSAA Unit 4 Notes
PDF
No ratings yet
ADSAA Unit 4 Notes
28 pages
Daa Notes Unit 3
PDF
No ratings yet
Daa Notes Unit 3
20 pages
DAA Unit-5 - Merged
PDF
No ratings yet
DAA Unit-5 - Merged
17 pages
DAA Notes
PDF
No ratings yet
DAA Notes
17 pages
DAA Assignment 2pdf
PDF
No ratings yet
DAA Assignment 2pdf
23 pages
DAA Unit-3
PDF
No ratings yet
DAA Unit-3
32 pages
ADA
PDF
No ratings yet
ADA
18 pages
QT Assignment 2
PDF
No ratings yet
QT Assignment 2
11 pages
Dashrath Notes
PDF
No ratings yet
Dashrath Notes
20 pages
PB Daa
PDF
No ratings yet
PB Daa
5 pages
18CS42 - Module-3
PDF
No ratings yet
18CS42 - Module-3
43 pages
DP - ClassII - Notes - 7th June 2023
PDF
No ratings yet
DP - ClassII - Notes - 7th June 2023
11 pages
Unit 4
PDF
No ratings yet
Unit 4
19 pages
FALLSEM2024-25 MVLD601L TH VL2024250106607 2024-07-24 Reference-Material-II
PDF
No ratings yet
FALLSEM2024-25 MVLD601L TH VL2024250106607 2024-07-24 Reference-Material-II
15 pages
ADS - Final.Solution
PDF
No ratings yet
ADS - Final.Solution
5 pages
Daa Notes Unit 4
PDF
No ratings yet
Daa Notes Unit 4
14 pages
DAA Assignment 2
PDF
No ratings yet
DAA Assignment 2
25 pages
Unit 4
PDF
No ratings yet
Unit 4
21 pages
Daa Unit - 3,4,5
PDF
No ratings yet
Daa Unit - 3,4,5
56 pages
1
PDF
No ratings yet
1
8 pages
ADA - UNIT-3 - Chapter-1 - Greedy Method
PDF
No ratings yet
ADA - UNIT-3 - Chapter-1 - Greedy Method
47 pages
Dynamic Programming
PDF
No ratings yet
Dynamic Programming
47 pages
Algorithm Design Paradigms (Dynamic Programming)
PDF
No ratings yet
Algorithm Design Paradigms (Dynamic Programming)
68 pages
Daa Micro
PDF
No ratings yet
Daa Micro
25 pages
Daa U-1
PDF
No ratings yet
Daa U-1
39 pages
Daa Assi 3
PDF
No ratings yet
Daa Assi 3
20 pages
Ada Assignment by KARTIK PATEL
PDF
No ratings yet
Ada Assignment by KARTIK PATEL
20 pages
Dynamic Programming
PDF
No ratings yet
Dynamic Programming
47 pages
Eraty: Here, DPL) Represents
PDF
No ratings yet
Eraty: Here, DPL) Represents
21 pages
Daa Unit 3 PDF
PDF
No ratings yet
Daa Unit 3 PDF
17 pages
Embedded System Assignment 1
PDF
No ratings yet
Embedded System Assignment 1
3 pages
Greedy Technique (6applications Explained)
PDF
No ratings yet
Greedy Technique (6applications Explained)
17 pages
DAA Unit3 Notes - 241015 - 101831
PDF
No ratings yet
DAA Unit3 Notes - 241015 - 101831
36 pages
Dec 13, Doc 1
PDF
No ratings yet
Dec 13, Doc 1
28 pages
Arrays
PDF
No ratings yet
Arrays
44 pages
1 Dynamic Programming
PDF
No ratings yet
1 Dynamic Programming
4 pages
DAA Unit - 2
PDF
No ratings yet
DAA Unit - 2
14 pages
DocScanner 2 Sept 2023 8-47 PM
PDF
No ratings yet
DocScanner 2 Sept 2023 8-47 PM
15 pages
Aiml Unit 2 Notes
PDF
No ratings yet
Aiml Unit 2 Notes
33 pages
UNIT 4 Greedy Method
PDF
No ratings yet
UNIT 4 Greedy Method
29 pages
Daa PDF
PDF
No ratings yet
Daa PDF
71 pages
+2 CS Book Back Q & A
PDF
No ratings yet
+2 CS Book Back Q & A
107 pages
Algorithm Unit-3 Notes
PDF
No ratings yet
Algorithm Unit-3 Notes
56 pages
Lec 2
PDF
No ratings yet
Lec 2
33 pages