0% found this document useful (0 votes)
21 views18 pages

Dynamic Programming Notes

The document discusses various dynamic programming problems, including the Knapsack problem, Subset Sum problem, and their respective algorithms. It outlines approaches such as memoization and tabulation, providing code snippets and explanations for each method. Additionally, it addresses variations of these problems and their solutions in a structured manner.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views18 pages

Dynamic Programming Notes

The document discusses various dynamic programming problems, including the Knapsack problem, Subset Sum problem, and their respective algorithms. It outlines approaches such as memoization and tabulation, providing code snippets and explanations for each method. Additionally, it addresses variations of these problems and their solutions in a structured manner.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Deepak. Marglon- Date......

***

Dynamic0unmina Page.. ******************

DP Enh once.d Ketunsíon


hen apbly DP-
Choices i) phma
(2 on mone)

Note Note Newstant qfoluing Dp_p10blarna dimctl


y makng table U(Gahn- Uh Manner
Ho tg tant Wilin QP Code
hesi K ppnaachz
Memoiz aHon Tle pak_Mangani
Bottom-Dp_(9abulahon

O: Kaapsack
i)nbaunded Knalosack-
di 9ibonacci
LCS
(y 1L3 Tanant Panblonna.
LiKadone's Algnithn
ijMatnix hai Mulkiplicahon
i DP eh anid
Othena
ate..**********
Page.********.
Knap Sock_ Paoblan

h a t Knapsack
hich 0e ha
Knalp.sa.ck a kindobag
items maxinum
to qstong ha
no
ohnapsath

hnapsack

nachona Unbouncded.
O1

achon a KNajhsack
,

I a a gneody pnoblen in udhich, p nee.d


QSono aMaMumvallsaeighf into
Can g$torn
a nHemb Cuhn
O11 Knapsack
Tn tho bnoblom e ung d in thsa
e.ithe nc lud! an ikom_into hL
bag Jst_exclude it

Uabounded E4 ane 0a 011 butL


hau n
Date.. ***************
Page.. ****************
:1 kapsack Puoblan
i aua qu PanHHun.
iiCount Subset
Liv) Minimu oswrn
Pubset qum Dif
aniatonM
ango un
No Pubset bunven dif +
oblam D1 Knaa.sa.ck
TdanikcaHon_ T
aDP enoblem 3
chaices
ti Becauo Dpimalans
Max Min, Langesi,el
1P wtttTSH5S
Val tI h5|2

TW 4 Ka
Max. Pnai
DP Code Kecwnsion Memoiza bion Jablation
Hoo' te Wnite Kecundive (ade
-
ecwnsineCoclei

G Baoe Cond
CiChoice Diqanan
i) ban Condihon -hink e the smalat valid a
înbut
h a p a b l e m i twill he when "oe.
n i22 he
hanean emptaanna
ba4

Choice Diagnom
pn on element tem

Val
wok
Wtiiw ttis

he Maxim um of hdo ChoiceS abo


in he left Subtn 11 bpO
GnSLen

Tt ConL be snimh nht ubte


********

Page.. ****************

Hecuive Coda
atKnap
tefit ) sack Cint ot1,at
valta, nt w, intn)

ifn=o 1| W=0ne kwn 0,


if wt[h-11< W)

hetun _maxalin-11+ krapsack wt vayw-wtni

else ifat [n-1 )W

etun Kaaksackwt,val, W,n:1)


Date..
Page.

Memoi2e Cado
We pill add linld er tha- ecu7I Codo
le memoize_it.

h booicidea AD makean anna


10cunsing Coee2
Chaungingvoniables n the
Intd ln+11 Lwt13, menset[dp,-1 Size ofldP)
1nt Knabsack (int wtt int yalr int W,intin

h=0 | W==0 1etuun -


dhIn1[w1|E-1 achundp[nLw4

ifwtLn-1)<w)

he tun dp InJlWE max va[Ln-43+ hrapsichutni


napsack{

else iwtin-12 W
hondp[nLw] kupsack(wl,val,n-1
Date...************
Pa
ragea*nseee**************

abulaion Code. (Bottom Dp Codle :


hebaaic iclea Behind abuleHon e
1Cuaive Calls _fhat Ae_hanl2
oSeenin he Memoitation techniauo inle
ternah onm

abulaion_can be pn sonmed to Hhe plowina


hoo steps
Tnitializahion Chanyging heunaine suntions
boical hse in itenahue one.
an
y0Cunaiàn

ntdh [n1i1[wt1,
iiq lisation
Hon int i=0.ien+1 itt)
fonintjz0,<w41,jt)
dp CiC{) 0

7Ttenative Veròn

ton (int izL kn+1 itt


oninfJEljsw+ljtt a
Ewt i114 Wj)
dpilLEmadpLAIj-wtL-1tvel l i ,
ap[i11LIN3-
el5dp £13C] dpri-1Ejat
Netunn dCnItW3
Date.*******
Page...*** g

evision e knagbsackad Intnoducion


voniaions unhn_pnoblor
A

problemin dh cap
aa
Tdoni Knapsack_2
i ODePnluedwin

hene ee haug
Whn e gee a_lonn
Capacty and a n i t e m a 1 0 hat
opmal banti
ha aßaciyaith
Vlls unden Knapsack.
noblem
hapben that
oimeime his
1athonhen

towtL1 ovalc1 hon 22


anc conaicton
nglecE h yaluo
oeiht

Lzample.
athnn)-

Choices ono aCon.stnain aA W


that we_cnE taKe alu MOne hat
Ow anS

ndthine _ala e the change in


the nfhalizatini in is vmiaio
Date.. ***********
Page.. .********

Subset- dum Paoblem


Problem
Gine n andA6un find
whethnb ing Sone elements _
SuM notL

LExomble a 1p n 2,3 K,10


um 11

O/P Taue
an 2,3,1,1,10
un 14

O/P False

rnilal ih oi kaspsatk i
s We Con ose.e hon2. lthanegiMena-
an 1ahen han pad hat we hue
qseen n-Knabsack le mnsidpn it_as
1eight a1 AdisCusBe n he yision
se e hane choice.s te ncu de not
nclude an Plemenfonlf un Set

0ehau gon Mazimunmi capoc


gSum analoaoua w in Knapsack)-
Date..**********
Page..* .

Cad Vasiationd
ThihalisaHon
O
d_Surn i
a1ags11e dOmmorpn
possible le ei bun_
always essible empp ubse
Hes it

dum gntaken hai 0


bossibleto AetSn?
emmpty is it
NO.

DO e 9ot Oun initialisaton0


dpCat11TSumt1),

on int i =0; i<htl;_itt)


foninlj0,j4 Sumt|JH)
f (i=z0) dprillil
g0) dpGtjiz tnu

Liy Main Colo -


wEr
W Sun
0/P
int ax nof
oubiC Vaniq hon
Date.. ***** ******
Page.

O
nopunla
Le usl )
os e ernofunchonin bolesn
onuiton

dpti3t =(dÞtiJ Li- anm[i-13] dpti-11Cj1


else
dpriir dpL1:19 i1
han
Desak anso
Mekutn elp[n1LSuml
Egua unanhihian
Problern
G en an atauy Can0e_onnHhon i in e
to qubseb Such hat
lemont and u itin hifhaae_
qubs.ek e i ak ecual sun in heul
9u h6e6.

Op Tiue nce S,5

Op alse
Date.....
Page.*****

7 orn e nhuihon and bsenuaHon a.

Pince, 10e haua_bo taka


al h elemank
ethe as11a ndpanhhon
Qnd
into-
eaual um J h e ,
tw oubsek
he a1a Phoulo_
tota um
he nenL be odA

noof
X
u n t h eubseta

oinceoubsetsuS,) NubsebueS)

2 C un he amoa4
2thoulalas be divisibla by 2
then ois6e 10nitten

Als
enough osho tnat get q
ubset nomnam h wholl dn
half unS

We ill et
Date....
Page....

Codo
hool Eual Sun Panlnl anL1, înt nintsu

Sum , 2)nehan false z-


i
MetounSubset Sun int annLI,intnint 5un5

oun phubset pun 1lfha iiven u


roblem
Giuen ind
lun ftad ho:
man oub.Ses inon an_passf hle.

X bi

TP 23,S, 8, 10

Eplaakon S5 2,
Pagt*********

wih pubset oSun i


2ilarity
bem 1 n y ne
noubsetoumno
Aubsef o un
outu
possible 1 not
de alse niecl
But home_,in thio roblom
Couni0 the ubset

Code Votiahon
Tashaisaion
for int i0 iknrlÍtt
son[int j z n j k S m t j 9

i=O.) dpti1Lj1= 0.
dpi1j121

MainCede

fo11Li:11 )Su dpriCj1-dpti41


else dp1cj1: deri-11rj-amti-11] tdli
DOTG.*****************

Page......*nn*

Mnim um o.bset on. iffenentel


oblem
ien aay o inlegens we hane
panitioni inle qSubsek_ehe - with_miniMum

SS minim um
TeK Monglon
2

htuhon and hsenvaHons

inen ho0 6ub SestsS and S he


Such hat_
S-S mindiff
Lel's 9:

f SE01a
thenanthune ditononc ill b heSun

Sum a

Let's Claim hon that atleast ehe


dhe QSubse willal 0S prmsen in he.
ny hal tomplel
eaailSince,
p1oued
Cun bo
StS 2 Sum ann
out the diftenc
Coun finc

giuenlann-S)-8, dift
"

di ounlana-2x51H

Sum t41,
int lh[nt11L
4onlint izD10t11, it
pindj-0,jK[untiJt

iffiszo d6rirj1=nlsa

dpri gI=tnul
ifljz-o

fonint i2 Kht tt
onintj j{Suntljt)

ifannfi-11< Sum
dpi11j
1dpt:21j-dnoLi-13]dpli-1

else
Dat*******************|
Page.. .

fon inki2 Sum2 iz0,i

diE Sun- 2 xi
bneak

Aetunn dif
lv) Cbunthe numben o pubset_wih

nlon
aien anainaoncidifleninca betoe.en fhe
0Subset Sum CAuNt
Subsek ilh Tha dtewnce
Ex
1/P o1,4,2,3

Explan ahioniL2nd3 L22nd


71ands
kepaK Mong
Tahiions an d 9hsenvaions o
2
Lethe hoo o$ubses be and 2
hen

Eit
AISe e KnoLh hat

unarnn
no
adding .di) ond i) e 1

1 (Stdiff hot diuisilska


O il qimply b2
Qthon uoiSe unO
u bset find he
hia Sub set - Colun p
ansuti 0n&

You might also like