You are on page 1of 24

-0

DyNAMIC PROGRAMMING

he enena Hethod
method thel Can e ed
DP is an algoithm dosgn
When the 80lubson to a prohem may e viewed as the Yenull g a

Dequence olcisiors
General method fov mulistae oeciston
opinuzings
Pocense
An opimal Bequenre Cecson may be hurod

macng the olecisfonn One at a Bme and neve makins an eaoneeus

decisfon
In Dp an otnmal sequenr oecisio ns is a y ved at

by malin explicit appea tv the principle q opBmalh

Th principle States that an opimab0 Se_uence

that whatesen the niha) State and


clectstopnha» the Prepevhy
mst Consitule an op bmad
decisions
olecistom ae Lhe yemairng the
oleci
Ccisonsion Sequence cUth deqend b the Alale Yetdlim hom

tosst dectsen
naicpogsamming posemring stano hv plannin

and does not ele to Compiule.propamming.

Knapsack problem- An opima Sequene odecis'ons wi maA


imize
he 0bjeclive funcion pix; It w) alo Sabshy he Constva'nts

2 wX M and 0<X; <|


Shotet path An optimd Sequerre ) lecstor Ps One tuhir)

eul in pdh of leaN rgdh.


velalton lhal ha
In mlebing dh DP secuvene
use One wo deen app wache,
be 8o ved, ône may

o cwand o baclcwand.
Vanable fos tUhch a stonco
et , .n be thbe

oects'ons has v be nmade


bvmatahon i r
the bvuaol approach,dhe
decsion Bequeve
?s made in tems e Opmal
cleciston X
clecston :

e h
--.. n A0ole ahead on the decisioncuene
hbvmulabon
övmulakon
the bbr
baclewad approach
In the
SGUenc es tiv
olecision
tenm e opma!
oleciston is in
o o c bcrwanols on dhe deci'sioo Bequeme X,2...n

Feature dhe DP appvoch is Ehet opBmal solulon


Bubpro blems ae retaine So as bv Cvorol vecompung dheiy valsen.

oEnenal stakement)
DP is a technigue by 8plvng proble wh oves
lapping
Subppblem hee Bubprphlems nise vm a ecmence Ydoing a

8 Problem's S0 luion o Aclulon q ihs Smaler subprblems


Rathey han &plving OVeyapping &ubpmble again a n Acain,

DSugests soluing each 0f the Amallen Subproblema Only onre


Con o
ecovcling he renlli in a table om uohich a solubon »
the oviGna Pro blem an theh be btaned
Dp ts hyp*ally aple hY opHmzalon prblem

Divide anc Congue


pbm is divicled intv Small Bubpbm These Bubpbma
e Solueol indepenolently Finalla the 8olubor ubpbm
OUne
ane collected bgehe tv get the slubon to Ohe gn pn blem

a pcake Subso lubior may be Obtained

3. leas epctent becaue ewovk en Bblubono

Ube up down appyach of pbm ing (vecursie)


debeminiskc Points uuale
Sspitts t I}p ak Apecikc
inthe middle

DnaniC prgomming
all dhe
Many oleston Beguere ano erenrod and

Ovealapp'Bubinotancea ae Consilee d
Compuing dupcaHon in&0lubiorn 's avoroled vtally
d

3 Etient
wes botprn p ppmch of pbm 0Ning (iteabve

S Spits t Ip at eveny poss:ble Sp potnts vatbe than

ak a
pasbcidan point A¥te
btsu all 4pt point t deteminpe
which Apl?t potnt ts
Opkmal
eedy Hedhod
. ved h ohlainng Opbirmun olultn
Sel
dSel eastble 80lulion and prv up the pinur 20uEen

Opbrmum Selcckon s w'thul Yevfsin pevib uwsly r e r o


Bo1uBon
Theu is no a Buch
qvanant geli epkmum 0lon
Onle one deison sequene is eNen
gene ated.
namPommi
wes oka"ning opirum 01uton
i

a ne specal e asable solukon


3 Considens a posble eque n oTole to oain thf

pbmum 8olubon.
4-
Guaante that t co)qeneale cpHmas solukon sig
Ptreiple ) 9pmalbhy
S manuy olesion euecG may be Generalrd

Both hos enenatsategy bu t e puspose


is Same.

DP iS Bme Conduming COmpaneo v qreecdy mehod

Mrstee aph
Apasrs shostel path algorithm
napsac pnblem
Tsaxeln saleman pmblem
plima Binamy Seorch bree

Reltabirhy dogn
DP tc
D luwo medbecs mernoraHon and tahulo -
mehod
Memoizakom b\Vau tty dour appmorh (TeruS?)
tablas meod Hlloee olbm tep
appruach ( ilevakve)
EXomyple honac
int fh(nt n)

(n<=
etunn n, h(3)
Yebwn Sh(n- a) /
+/b(n-i)
l ) b(1) bt) ba)

To ind Cors iole 9 Calls


T(n) 0(a")
man bmes the same unckon is callel ain and agin
takern.
Kecluce dhe knchon Calls and edua the Bme

intb(intn)
3
Store bhe Tesul, in dhe
Yebnn anay
So totas s imesit ts cadeed
FEo) 0
F-1 bn)=nti CalWs
Tc 0lo)

is done m Smallet valbe


ng
.

F:3-F1a]tfli-); Soitis bottpn p appvach*


Yeuin FCn)
3
Kedu dng dhe öme Complerhy bom
MWBstage 9haph:
AAaa
H malstage gnph (v,E ) whrh is a di eted

aph: ASo the p h *s he Coqhled Paph

ANhe Vesbces an pevikoreel indo the Atag


whe a Verbces ane viotool Theu s
intv Stoge). a

Sel eg veka in each Atago.


IP (Uv) 's an
edg in E hen u e stage and

e s
obtan he min: path ak each n stago by Considerijng
the poth length of each veslex obbained in eanler stap Th the

Bequerce decisiona Ge taken b Conbiolering ovenlappeol dolu lon


n
DP ma get any ho 80hubono hr a gen
PrcblemFrom alt thexe 80lusonssk b opkmal 8blubion

inally ophrma Solubon becomes the 8olubon to ven Pmbem.


Mul istasp &oel ing hrwad and
haph an be

backwad appnach
MdBstaco Gaph is ued tóv
epvesent the Teso wce
allocason
ind dhe shtepl path um Sounce Strne
so 3 -
Stoee

Sin
Suuxe

oetayd mdhod.

vey stage take the dkcisfon So pintpe opömali


holds
V 3 5 6

Cost 9 8 5 8

d 6 6
Stage Veybea
htage, Cost ( 4, ) = 0

Staga, Cast (3 5) = 8

Cost (3,6) = a

stag
stage, Cost (2.a) min (c(a.5) +Cast(3,5), c(3,4)+ ost (3,8))

min (3+8,6+a) min (\),8) =8


Cosk (3.3)= min
(C(3.5) +cest (3.5), c(3,6) + Cost (3,1)
min
(4+8, l0+a) =mi'o (a, a) =
)a
Cost (3.4 )= min ( c(k»6) +Cost (3,6)
c(49) t
Cost(4. *)
min (3+3, lo4o) min(5i 10) =S
=

Stage Co st (1,)= u'nC( a)t ost(4), c(h3)+


cost(3,3)
c(,4)+ cost (2, 4))
-
mi (+8, 4 13, 4+5)
min (9, 1A, 12) 9
Cost (ij) i=
skage, j -
Verkex, i s Some Vevtex

Cost(ij) u n c(j.J) cask(i41,2)


+

EE, and ! belmgs y to set


eolg
e Vi belongs to next Stage
Now, have to taxe the sequence decision» based on
the data anailabie to solue the problems
wing bhe od Values, kake the docsions
Deciston
Deciston is taking on the orvancl dvecbon s o i s a

Horwad medhod
stoce Vevtex
d (,) = a stanteol at vevlex ,
dla,a) = 6

dl3.6) =

Algoithm fovcwand (E,


Sthges.n.p)
Cost[h)o
r in-j to by-) do
Y
Getmin j n)
der be a
Costlj3 cljJ3+cast yJ e E andVevlex 8uch that
c(j.r) cost (7) is
dler mi'nimum

PCiJ
PLstage 3n
fos
(ja to
stnga-)do
PLjd[Pj-ij
ovward appmac,

d (.3) =
11
d (1,3)=2
d(1,4)- 7
d(l.5)-mn nd(a.5),e2id(3,5)1 - ninfH 3,»43

d(1,8)= min 1 d(3.6,024d(3,0) 94d(h.6)


ru'n16. 3410, 9+3j= min 4,13, u3=7
d() min
d(1,S)td ( 5.1)) d(1,6)+ d(b. 7),
4(1,4) d{h,¥)
min 4+8. 7+9, +1o= 9
minimum Cost =9, coth path 3 - 6-7

Backwand appmch:

dl 14)= min (I+ d(a.4), 24d( 3.#),4 4d(h.7))

d(3.7) =m'nl 3 4d( 5, ),64 dl4.t))

d(3.4) =a'n (h +d( S 4) IDtd(8.#)


lCA4)=min ( 3+d( 6.7), d(4*))
d 's, 4) =8
dl64)=a
d Ch4) =10 Verter 6
d(.4) = min (348.6+ a) =min 1,8) = 8 a - 6-17

d( 3.4) =
min (+8, 1042) mn(12, 12) )2 3-7
dht) =
min ( 342, 1o) emin( Stlo) : S - -7
d(a)» m'n( 8, 4 134 5)

min( 9, )4 1a)9 &-7

hgovdtn Bacltwand (E, stage, niP)


backcoct [13eo
6r (je 2 ton) do
o Y be a Vevtex Buch hat
j GE and bace cost ( ) +
Getmin (jn) clYj)is minim um.
baclcosti)backcost [3+ of»3(
djer Compuna backwad cost
Coch vertex.
n d min cest padh
P stagelEh
j e staga-) b a by -)do

PLjdlptjo)
Rod
Hod aa mnimum
minimum Qost path m S btin mlesta raph

tng durnamic pqamming


3

3
5

3
S

5
S 3

6
b
3

V 3 S 64 8
Cost 16 918 IS S

d 3 768 8

Solubon 7 10
94 3+3+2 ) =)%
36 o 2 (7 25 +a) =I6
-
wayshal's ard Floyd's algmlhm

(Wanshall's algowl hon ox Comprmlvr e barnnive c )osuwne e2 a

eled gaph. pat malr'i,)romiive


Closue, Boolean mabia.

loy algovi{hm
Floyds -

a paivs shovles palth prohlem

The bamikve coseu of e ctvecteed graph cwiD, n vekkes

Con br desnes as the nyn booleon mabix T in which the

element in the h ow Gnd he dh collemn s thene exsts a

htn bivial pdh tvecteed pcdh fa Posikve Jengdh) om be vevtex


tu the i vevl othevwse
is 0
Warshall's algoibhm Conatructs the trarsvye cJos u re

LhTouab a aeries ) nxn &0olean nmatices


RCo R(k-)
Rk

The ekement in the jh yow and Column

nats (=1,a...n, Hs0, l,-..n ) is equa b nd Ondey if hera


erss a dected Path of a
postbve length hon the h veye to the
velex wilh each intevmed'ate Vestex }an numbeued not highe
Bnon K

R not allow inemeotake verbre i dhe paths


aeny matii of bhe diaph
we bhe Vetey as intemeoliake
olln veiren of he digsaph an
ine rreal?ou
diqaph's Yans?hve cosune

Omple all he elkmends o each malr p ' Pnmik


nelate PredoaQsoY R ' )

(k)
TYi = dken eihe T%-) both =I ard
0Y
p;
I P an ekment Y; in Rk) reoinsin R
IP e elbmen ; is o in Rk)
ha to be changod b Iin
Rf ad onl the element in YOwi and colurmn k and the
olemenl its column and ow k are boh 'S n R-)

(a a
6cd

A =b
O

dL
R 0O
Conbiden a the in terreolale Ve.le*
L a-b=
d-a
(d-a)(a-b)- l4-b) 1

Con.sder b as the intemeeliake Vertex


a-b (a-b)(b-d) =(a-d) =/
d-b- (d-b)(b-d) (d -d) =)
=

(b-d) = (b-d(d-b)-(b b) IX becae, =

Constde bas the intemeeliate


Veslex not ol

ConSde C as the intemeolate Ves lx.


(d-c)=
dhere is ho chans in dh atk, Bo matx
eman Same
, (3)
R
Con Side d he
as
nemedate Vestex
(d( a) (a )
(b.d)-
(AYA) (d-a I
(AAAh)-(d b)-f
(d-a (aA ( (AAd (dr)- )
( d d A (aA
(d (hANda) (
(A (hdd (b)
b)(A) ( h )
(h dA) (hd)

R)

lgerithm washll (P[i.n, 1. )


fo ke lo n do
feyi o n do
For i I b ndo
R (x-)
j) R n r R(k-)
)
eh
dnd
R[kj)

Compesihy 2 way shals algovtkm ís Oln3)

Pprpleabon
hen a Valie in a
Apreadhet cell ichanged, e
spread cheel S)d mmul 3noeu all lhe olhey Cells alerted by he

Chens If he preodsheel mocl leol b dqaph (ihas e


Vesben epreenl Ue ead shel Coll ano Edges inelcate cel

deende rre lhe bekve cles-u


In SE aniBve closeve Cn tued h invesligaing dola
loce and Cond) deneeviten a tal a hY inheit
ar e
Lelng ohjecl oven!lnd

Hcyd's avlhm kr he al pairc shovle palh pnhm


iven weghtel Connerted roph( odrectad or andi rec ted)
a
he a
pars sho«tert ath problem a s u nd bre lergh of he
Shertest paths Km each Vestex to o l ohes verttes
paivs- From every vevtes to oll ober verte
D dstanre hmat"x
The ekement d? in the Phow
Ehs mabwa the" Cola mn
and
inicales the length of he shovtest path Fon bhe
h vestex to the dh
verlex with each intenmeetake Vevtex
ha
alithm is not applica ble the hegave lengh

Houd's alprithm Computes bhe d:starre mabra ) a


wehted qhaph wtb n Vevtice throgh a series e nxn maice

doe net alloto any intemekate veslices inits padh

weighe mabs be ghaph


eU
D Contas e engh ) the shovte patha Rmon

Paths that Can ue all n vestiCea Q» inte merliats


ate

d: (1 = min (k) ()
d +d
ov kz, dij(ow
uj
( o 3
D

L
3 o

Vestex a a»
intemetale
(a-C)- 3, (b-a) = , - a ) ==6

b-a) (a - c) =(b-c) min =


, 3a =5
Cd-a) (a -c) (d-c) mn»
, 6+3j =9

9
Vestex bas întemeolale
C-b)7, b-a) -a, (b-c)=S
(C-b (b-a) - (c-a) =min ,9 9
(C-b)(b-) ( C-c) mins o, la o

D
o

Vestey C as inlemestate

(a-)3, (b-o»5, (d-c)=9, (C-aj=9, (c-b)-7,(c-d)=


(a-cXc-a) (a-a) 0 Cd-cc-ay- (d-a) =6
(a-c)( C-b) (a-b) =10 l6
Ca-c) (c- cd )= (a-d)- 4 (d-c(cb) (d-b) »
-

Cd-c)(c-d)- (drd) =0
Cb-c) (c-a) =(b-a)
(b-c)Cc-b) =(b-b) =O
(b-c) (C-d) -(bd) 6 =
3
O Vertex d a
intemeediote
(G-d) . (h d> b. (Bc-d)=)
(d- a) . (db)=6. (d-):9
(-d (d-a) - (G.a)- 0
(b-dXd-a) (ho) c-d)(d-a) -(c.a)a7
(a-d(d-b)-(a-b- i0 (b-d)(d-b) -(bb) =0 (C-d)(d.b) (e-b)- 7
(a-d)d-)-(a-c)-3 Cb-d)(d-)>(b-c)-5 (c-d(d-)-(c-c)=0
D 3

16 9

Hlgoithm floyd ( LI..n, ).-n))


DEw
fov k to n do
for i bo n do
for bndo

Yetun DD

Pplieaso n
Pre Compubng olistance br mobon plannio. in
Computen game Communicabon, rapovlabon nehek,
COmpute
Opeahon
Yenesch
-
pp PPly warshall's algoithm o ind the rapikve chsuo dhe

tgaph deined hy dhe Mocoi adjooney mal

oiogaph widh,
a
Solue he all paivs Shorlet palh pwbem or the
diogaph
Dhe blewng weahe malv

8
3 O

3
3

3- closu or the
o btain he brari Bve b\lacing haph
aph twing
ing
warshab's apidhm
QBSTOpma Bir Seavch e
heguencies ane
rd be
T the Kays ae ven
oul Whkh taeo Ovganza5on w be opbmo
qven. nd
based on be hequ encea
dheY
cost the Seavchirg s minimum baned on

of the Beavch
nequenc
E Consider A,B, and D to be Bearche or
h kous c

wit probabhbes o),0 3, O-k and O 3 3 repecövely .

Thene ar l possbe bina 8eavch rees Containin.

thee ouw Tootrees ne

---o
Ooi ) + 0 242 +0°4 3
+0 34 =3.9 Oo) a + D241 + O*k240 34 3
)
d:9 and a ) , the aveaap hunbe» Comparisorna in a

Buccesf 8ea vch


The above 2 ree s nt opimal. Ehen nd the opimad
ree bygenenaina al BST widh dhese ke

The abave ethaussve sech apppach s unrealesic.


OBST,
ket a .a be disnct kay 0rdlonac rorn the

Smallst b bhe largeat and t P... Pa be he prbab?lFke

Beoichng o then
HVe1o COmpassonm in a beach s the Smalltst pacsble.
Avevo
-
Aet c l i ) be the snalut Gvenoge number g oparisere

adeina succesf Bearh in a BST T. mad up g kyp

a ai.. a whe andj ane m e inlap indice 1<jsn

o des ve a
vecuvenc e
Consfde al possib}e s to chese a rtot

Gmon he
kes j
Fov Bch a BST. he Soot Contairs ke e , bbe

let subbree k
conain Ks : pimally anrangd
and ngk Subtre T. Contasn keys ay a suo cpimaly rerge
Count tree levels Staning wib to malke he
passon ambens equasthe Ke's lovel, dhe #lewng
TecusEnce elakon is cbtaines,

CC) min Pe 2
S=i
P(levd ag in
T+)
4
Ps (ken os iT
min
Si
S=k+

mn
CCi,(x)+c(teo,joÈPs
Kecusven ,

CC mn
cCi)+ C(k 2 fos 1i4jsn
S
tve
No3 Comfar'so in the emply
C,-) o
)0 a one node BST Containinga
c(i,) =Pr in
Aurcostad
dhe avenage no 0 Comparssons by
Compute c(ln)
-

Bearches in dhe 0BT.

A C D
Probabl: ty 0 3

hoal ts bo
Compute clnA
Kilng the table along its oagonal, tasing otl) alzras
On the man dtagonad And a prvbabilFbe Pi, 15isn,
the Cosne
ght above ib and moung towad ppes aht
Markao anothe table k e t the opkmal tve Stan
Sta»hg

when bhe tcble s klkd, s enbres indttate ind'ca n


Bhe Yooks of the ophmal 8ubtrees, Whwch makes iE passible
Yec nblsuct an
opb'mad b e i bhe enBre Bet given.
Rj) by k Valu whtch is dblained a minimum
Table Ahouls be A}| tup diagonally.
c(i,)o, c(,') =P
In a alse, CCij)mí'n
main ubie
ci,y-)+ e(ks!,jo 2 Ps
Rood table

3
3

COmpubr C(a)
= , cC. o) +e(a.a) + á Ps-040240.3 =0-S
C(ha) min Se

S>1

3
k-2 c(3,1) +c(33) 42S=2Ps= 0t0A +0-f =I o
C(2.3)r in

h3 ca3)clh.3) +~1s=4a 6:6- 0-8


(3,4)mn k3(3,)+ c(4,4)» ~ P=0+0 3+0-7 =i*o
S3

=0:A 0 +0 9 =1:
Sa3
3

S=
c(13)= rio 0:1 t0-k+0 .9 =l '

3
ea) +c(h. S
0h+0+0 .4 /.

c(a.) = min K-2, e (3,)+c(3,4)+2s= 004 o.9


c(3.M) - .9
S
k e(2.2)*c(l.,a)+ 2 Ps0 o-3+d
S2
k l c(3., 3)+ c(5,A) +2 Ps= 080 +0*1="
S 3
- Co)ic(3.4)3 2 Ps: 0+lhas ho = dh
c(1) min
2c(,)» c(3,4) P014|'04I'0
=

0 A4 0 34/0 :

1404lio 3
-

C(, 3)4c(3,a) 2
Ihe veag r COmpasom in he Opkma reo

hre Cortains
Cortain
the oot dhe 0ptima
Snce R (4) -3,
a r d B ard its
Bubhe is mask u 7 keus
dhe
3 kay The le
ahtsubhe Contanjust key D
(Ae
be rot df bhe opEmal
tr Containin9
E(4) =2,
9 and Bs B. w* A beng ts let ckld.

opbimal tree

IP n 's bhe no. g eernents.


8 c h tr@s
Postble binag
=

No nt

Edlape,
If 3 ,
a C Ca ne _h!
3 (n)

If n:3, x3Cg &Cado


=5 S
341

You might also like