0% found this document useful (0 votes)
50 views22 pages

Unit - 3 Dynamic Programming

The document discusses various algorithmic problems including the shortest path problem, knapsack problem, and traveling salesman problem, focusing on their definitions and methods for finding optimal solutions. It emphasizes the principle of optimality and dynamic programming as key techniques for solving these problems. Additionally, it provides examples and mathematical formulations to illustrate the concepts presented.
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)
50 views22 pages

Unit - 3 Dynamic Programming

The document discusses various algorithmic problems including the shortest path problem, knapsack problem, and traveling salesman problem, focusing on their definitions and methods for finding optimal solutions. It emphasizes the principle of optimality and dynamic programming as key techniques for solving these problems. Additionally, it provides examples and mathematical formulations to illustrate the concepts presented.
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/ 22

amiC Poqamminq

Onamic Po91ammi
DyT Pyoanic poq amoiq
S an alqoritbm desiq mehod that Can be
used wbeo the Solutioo o a Poblem Cao be

Vew e d as tbe eSull of Sequeoce 04

deciSio0S
Priocip)e of optimality
The Piociple of optimaliAy States hat

an Optimal Sequence of decisio oS as th e

Properdy hat ohaBevey tbe initiat slate and


decisionS ae the emaininq deciSiooS muSt

deciSio0 Seguence uoith


ConS-litute ao
optimal
eqadinq to the Sate veSulting 1 m t h e

PinS decision
A PairS Shortest patbS5

Let Gi=(v, ¬) be a diected qrapb d i h


verdiceS let Cost be a cost adjacenC
mali fo1 G Such hal Cost of Ci,)-o
1 i s 0 Then coSt ti,j) is tbe leoqih (ov)
Cos of edqe Ci,i) ;f Ci,J) e eC) aod

CoSt ati,j). if+ and i , )qe l).


The al paiS Shovtest Palb Pvoblem iS
o detemine a
mali A Such that
ALi,TDS the leogth o Shovtest palh
omito
Let uS eiamine a Shotest ido Pa-lb in
G j The Path Oqina-teS al vertci
and qoes tbrouqh Some ioteroidiate v e ices
and lesoinate s at Nevtt1 we Ca aSSumbe
hat bis palh conains no Cyctes foif
bee iS
Cucie beo thiS
a
Can be deleted
ithout ioCeaSinq the path leoqth
f K S ao ioleioidiate Vertei oo

his shovtest path, theo the Sub paths


om iAo k and from k lo i must be

shoitest pathS fomito k and K 4o


espectively 0theroiSe the iAoPath
S no4 oioimum lenqth
of
VSinq G,T) o vepresent the lenqth
a shotest paBb rom i lo oin
eat hha
ao
Abrougb o e r te of inde qea n

K, we oblain Ai,T)=oinnin ki,k)+


keo
A(K,T)S, cost GD}
Clealy AG,S: Cost i,T) we can
obl ai eccuren e of
K-1
Find Abe a paS ofShortest path
path
Problem fo lhe qiveo 9a ph

3
A 2 3
2 3
O G 15 O 6
15
2 SO 2
2

3 3
3 37 O 3 3 1o

2 3
2
6
5 O 2

337o
(,3.oio C,), i,k)-+ (h,)
23 2 3
C,3) in2,8+151
(,3) i o 2,23

2,3)-
C3,) mio j),
3 2
i, K) + (k,3)
C3,2) min 3+9
3,2) min 1}
C3,2) 1
(1,3) min i,j), ti,k)+ (K,j)9
3 3
C1,3= mo15,y42
Ci,3)miof15,6)
,3) 6

C3,)mio Ci,j), Ci,k)} (K,j)


3
32
3, m i n 3,0+8
C3,) mio t388}

C3,)3
C1,2) mio { ti,j), G,x)+ (K,3)
23 3 2
C1,2) mio
u, 15 t
C,2) mio u,15
C,) 4

2,mio Ci,j),Ci, K)+(K,i)


23 3
C2,) oin,2+3
C2,1) oio 8,5}

C2,1) 5
Algovit hon A palhs (ost, A,n)
cos [:o,1:o) is -lbe CoSl adjaCenu
ofa aph oit
Iovevdices : A , ) i S the CoSl of a
shotcs{ path fom vertex

i o Netea CoSA Li, jj=o.o,Soi <1n

fo i: =| Ho o do

fo a l Ho o do
Ci,j7 Cos4 [i,i3; // Copy Cos into A

o K =| lo n do

fo i=I Ao o do

o l Ho o do

ACi,3: min (A Ti,i) :a ti,k3+a[k,jJ);


O Knapsack Problem

>1f we a iveonobjecA and oe


ae KoapSack o baq io which the

Objec ias a wetqhl 'w S Ao be


Placed 0n he Koapsack ha S a Capacil
'M' heo the pofi Ahal cao be earne d
iS Pixi The o bjeclive is o oblaio
fiNioq of KoapSack witb ma Pro-fi
J
aroe d
Maximi3ed Piai Subjecl to Constvain

and is to-lal
idi M, whe«e 3n o

Dumbe of objecls i =o (o)1

Hov
The eedy methopd does nol wo1k
tbis P1oblem To Sotve tbis poblem
method
uSinq d
wil
dyoamiC PrOqO
pefovm the
Dinq
fo Il 0oin Ste ps

S+ep: The oolalionS uSed ae let :)


be be value of oplimal Solution
hen s' 1S a Pair of (P,w) wheve
P : 4 L ) and w:4j.loilially SCo,o)
we
Compute S -fom S' These
Cao

Com
Pu-lalionS of S' ase basiCall
Abe Segueace of dectsions made for
oblaininq Ahe oplima Soluliun
Step:2 We Can qeocate the Sequeoce of
decisionS o Ode Ao oblaio the OPtimal
Solulion of Solving the knapsack proble
el xo be he op-li mum Seque oce heo
two ioSlances x'1 aod
bere ae

x
Xo- o-2
sep:3 The ovmul a bal ase uSed uohile
1S let
Solvinq the koapsack Problem
Filyi be tbe value 64Op-imal

Soludion FilymazF;- L9, F -


oitiall opute s -
o,oy S, P,w)
CP-P w-) S
S S'us
Puqinqa ule
S't

ConlaiS Pi,w) aod (PK,k)

hese too pairS Such thal f P J Pk) ancd


theo PT,wT wi be elimioated
w)
Calied a s Dominase
Th:S Pu ule is aISo
Nule pu qinq ule .Basicalythe
1o shovl
Shovl
domina+ed JupleS qels Purqed
and
emoNe the pai with 1ess profi

more weiqhi.
Solvethe KoapSack inslances -6,023
Shown below.
wi are
Pi,
Pi
2
2

s o,0)
S o+1,042)
-,

s sus
lo,o), 0,2)9
S t3),(3,5)%
sS'us,
:(o,o, (1,3),t2,3), t2,5)
S' t5,4), (6,6), (7,1), l8,1)

ssusS

to,o),1,2), (2,3), (3,5), (5,4),(6,),


C1,1),8,9
Note hat Pai (3,5) S Pusqed om Ss
becaus eASSume ba P;w) 3,5)
and Pk wk) -
(5,)
Hee (PPR
lw >w):s lue
Heoce oill eliminate the Pai Ps,T
3,5) from S
AS m:0 tue will find the lupie deoofinq
weiqhi6 (6,6) s3
,1 lo
C6,6) is oblaloed by addioq i30
S
S
So, 131|

Remainio q Capacidy iS (6- P3), (6-w3)


(6-5,6-4)
1,2)

Check CI,>) io previouS Staes


Check
s".
C1,2) is oblained by addioq i, to
So , 1

Renaininq Capacit is C1-P, 2 -w,)


C1-2-2)
Co,o)
So ( , ,X2,73) : C1,0,1)

SP13t-Px,+ Pa ,tPaM3
1xI+2 Xo+5
6

wiai<m
wi twX,t w a 2X+3 Xo -9x|

66
The avenioq SalesperSoo problem TSP)

le G = (v,e) be a diected q a ph wilb

ed qe
ed qe CoSt C The Naviable CC i i s
defioed Such hal Ci0 o alliand j

Cio
e lv\= o aod asSum n> a lour o
GiS a diectrd Simple Cycie that
includes evevuetCx in v The coSof

a lou is be Sum ofCoSt of edqes


o0 he tour

The TSP P oblem iS o Sind a Hour


of mioimnum CoSt

A ou be a Simple palb hat 9HariS


aod eodS al vevtel ¬ver lou ConSisis

edqe C1k) for Some kE v- {iy and


o ao

a path from ete K lo vevtet+he

each
Pa-lh fom Nestex 1 qoes hrouqh
Netei iov- { , k eiactly once

leoq-lb o Shostest
Le qi,s)be be
J
Path Sardinq al vesdex i qoinq-lhrouq
all vevtices in S and leminateS al vetex

The funcdion q (i, v-S 13) s lbe leoqth


a optima Sales PeSo loo
rom be Princtple o optimal-
C1,vi4)= min / Ck9Ck,v-i,k})}
2 kco
enesa we oblaio a (:,S) : m i o C i 9 - U
SeS
15

ConSidev the directed q1aph given below the


edqe enqths aeqiven by malrid Find the
mio CoS ofhe Aour

23
1O o 5 20

25 o l0b
36 13 o 12
48 9 0

qCi, i2,3,4)- mio


ftqt2,{3,43), t 9 ,
2,9), Ciu+9 («,i2,33)3
g C2,3,4)=mio i+gl2,i93),t9 C4,3y)
gC3,2,4)=rmin fG+9 ,4),c,+3 (4,i230}
q(u,2,3)) mioiS44g (2,{33), °u3+9 (3,{23)
-

C3,43)= miofau+ fu,0


m i o (12 9 )

mio l20)

(u,33)- min y3+ {3,43


mio9+6
min153
q,f3,y})=mio + 9 (3,u ),¢+3 (4, f29)
mioq+20, 1o+ 15 j
mio 29,25

2,14)= mio (C+i9,0)


m i o io+S)

mio i8)
4 , i 3 ) = mio C eu+i2,6y)
io 3+5)

miC13)

C3,2,9}) = roio + g (,i43),4 9 C4,23)j


min
13 +18, 12 +13j
min 31,25

gC,i3 min +i3,b


mio 4 6
mio 15

g (3,i23)- min
+12,01
i n 13 +5

min 1sy
16

g t, i2,33): oin f ,,tg (>,13}), +9 (,113)}


mio8 45,9+18
mio23,21}
= min23
9 C2,3,19) = mio t 9 ( , {3,), c, + 3,11

Cyt 9 (,í2,33)
m i o 1o+25,15 425,20+2*J

mio35,45,13

Bmio35

a)Find he optimal CoS o the touY

13

g , 3 ) mio isq (2,i3,1), S+93.)

qo,i)- rmin, ,+g(3,iv),,yt9,f39j


oio9420,9 +15 j
mio 29, 17

oin1
g C3,iu) min f 9,43
129
0

g (u,i33) mio Cu3+ i3,43j


9+6
15

93,
12,) oio i cSt9 (2,{4) S+94,i23)
( 2 , i ) - mio e,utiy,43}

mlo2+8

(4,123) =mio yt{2,9


8+5
13
9 C3,12,4) = mio13+1o, 12 +13 y

mio 23,)5
23

(4, 2,3})=« 1(2,31),stg6,175)


t
C,29) =mio iCat 3,43

15
C3,3): mio 3 +,43
13 +5b

g(4,,3)) mio fs415,9+ 1


mio3,)1
23

(,,3,4) min 1o 417, 154 23, 20+23)


mio21,28,13

Bina Seach Tree


Optimal

Suppose we ave Searcb a wovd om


di cliona and o eve equie d wod

we ae looKinq up i he dicliooary

heo becomeS time CooSumoing


POCeSS.

To Perfosm lbis look up move e44icieocy


we Can bu:14be Binavy Seaach of
he Comm0N d$ as keqword
wO«d

elemenS aqain we make his bina


Seach Lvee effictent.By anaoqinq9
eque9iluSed wovds nea o the

oo node l e s S sequeocuwosdS
aua from he ool Such a
ee makes Our lask mose
binau Seavcb
Binay
Simpify
Sipi and e-fficienlTh:s pe o binar
iS Called OBST
Seach ee

be Sel o ideolifie
l e lea ,a,
, a,a
, -.. ao a

<a,<* an
Such thal a
toith which

P C)be he
pobability
1e
Search fov a be the
we Cao
element 'x'
of Seacbioq
Probabilit
Such hat a i<{a1+1

lo otber wovds PC iS the probability


SuCcesSful Search aod 9C) S be
o
unSucessful Search.
Po bability of
e will apply t h e f o l o w i o q foimul a

fov Com putinq each Sequence.

C +mi Ck+CKJ

, J-I +PS) +9 (T)

loitialy w G), vi=0, Cii= o


iveo a,,a,a3,44 (do, if, iot, while) P'SG
PoP PP 909,9,93
&s ae3,3,1,1)G (2,3,,1) ave Succe SSful
unSucte SSfu e S pectivelu ConSlruC
an

OBST

Sol we know -hal

Ci +mio / ci, k-1t CK,J

w ,J-1 +P CJ)+9LT)

Joilially w i i : 9i) i i zo, Ci;

we have to ConSlruC Ahe lables -fo


ValueS ,c, 3

wool |w 3 qu

2 2u3 y

3 34

loidially -lbere is oo oodes bJuo i i

o wbich iS alsoi J ov) J-i= 0


Wo09 o) =
2cD 3
9(o) = 1

3 3 9(3= I

Now 1 node beloeeo


eoiaod i
So
So we bave -lo ConSlucl wo, , 3 3 4
o o0t PCI) +9) 2+3+3 S
2 o+PC2) +91) =3+3+)
3 tPC3)+9C3) = 1+14 =
3

34 3tPLu) +9u) - 1+1+1 3


Now hese is 2.
nodes bluo i j j
So, we bave o Consruct wo2,
ua24
02 w PC2)+9t>) S+3+| =12
=

W13 u tPC3) +9(3) =1+1+1 9


ty tPL)t 9u) 3+1+1 =5
No tbeve is 3 nodeS blu
So, oe ba«e to CooStruct
j
u3,
t03 +P(3)+9(3) =1D tlt1= 1
02

w+P4)+9tu) 9t141- 11
Nou here i S o d e s blwiii
S0, we have o CooSlvuC
oy o t Plu) 9 t u ) - 1441+1 16
Nou
Now we bave lo
Computt cand Values fov

Coo O, Cu:0, c,,+0, C330, Cqu


Sioce 0

o0 0, Y0, 9 , ' 3 3 qyo


Now , oe have 4o Com pute C&R values fov

j-11

Si-(o,O,01,2)C2,3),3,4)}
have o ConSAruct Co
we
C12,23,3y
C +mio C:,k-1+ CkT
Ok

Co otmiO 0,1-1+

woto+0
Co +o +O - 8

Ck=2)
C12 2 +mio 11,1- +,1
OLK2

14C+
1to +0
o i o jC, , 3-1 3
O4K3

3
3
C34 tmin
34 y-+ u4

3+o+0
3
Fo
j-i2
Si-i -2to,),i,3), Ca,49
Nod we have lo Constvuct Co2,
Cia,Gu
C tmin C,x-+Ckjy
k
Co 2 in Co,1-1 +C12if (k=i)
OXk2

Co2 Wo2 +min Coo+ C2 y =)o41 =1


0<k22

o
O4k2

KMio Value 1

o212 t7
20

13 tmio c t c 2 :2)
k3
043 3
k3
tmio
<k3

K miO valut - 3

Ci3 q 43 )12

=)0+3 3
2 oio
2k
k
3+o -23
C 3 t Cuu

C 3

woo0
Coo 0 Cu0 C22=0 33-0
22 0 C30
-7 33 y3
1E8 C12- 7 C233 34
O 2 2

212 3
Coa19 C13 12 y-%
o2 Y132 3

P C o 3 2 5 C u 19

Woy l 6

Cou -32
Yoy 2
wwmb mi
Now o u wi be the O0 ith Cos t oy32
lee
Tbe
h e 1e f1 l+ec of ; iS K I and qhl
S K,

Oo

4 int

híle
,3-
o,

You might also like