Unit-4 Greedy Method
Unit-4 Greedy Method
G1REEDY METHOo
G11eedy Method
Gjre edy Me Ihod iS
iod ao
a
lecboigue used to
op timal Solulioo lo a
qiveo Problero
om Sel of all feasible Solutio0S
Solulions
Most
ofproblems have o iPuls
iopuls aod
eguiieS us o oblaio he Subsel
Satisfies Some that
ConSl aiol,aoy Subset that
Salisfies these
CooSliainls iS Calied
feasible Soluion .
A easible Solution tha
O mioimi 3es a
eithe maiimies
qiven objective funcii on is
Called Optimaa Solutioo
O6 20
OPimal Solutioo - 6
Task abbcd fg e
Slat
o 3 976
Finis 211 10 5 8J
M3
M
R
a b d
M
O 23 G 5 6 189 10 1
G7enesal Method or Cooliol abslracdion ov
qreed me-thod
Alqorithm eedy
I a :o) cooiainS the o
ioputsS
fov i l 4o o do
Seleci Ca)
Fea Sible (Solulioo, 1)lbeo
Solution: = Unioo (Solulion 1);
reluro Solu-lion
Job Sequencioq with Dead Lioes
Lo
o Process lo find oplinal Solulioo.
Sol Feasible
Soluion
PoceSSinq Value
Sequeoce
C1,) 2,
C1,3) 3,
,4) 127
(2,3 3,2
34)
(3 3
5
Lu)
21
Optimal value S 21
Let oz5 aod (P, P2,Pa,Py P5) (20, 15,1o, 5,) and
dead lines
(d,,d2,d3,du,d5) (2,2,1,3,3)
: What is
he optimal Cos{ and
lhat iobS to Pro cess
o find optimal Solutioo.
Feasible Processin9 Value
Solution Sequeoce
C,2 2,1 35
1,3) 3,1 30
4, 5
,5) 5,
,3) 3,2
4,2
(,4)
5,2
(2,5)
4,3
(3,4) ,3
3,5)
v,5) ,4
3
Hiqh Level
discvip4ion of
job Sequencin9
alqovitbm.
T:13
fo = 2 Ao o do
Prim'S Algovilhm
Krusbkar's Olqovi-lhm
Prim's Alqoithm
3)
5 12
22
Slaqe3 Slage
Sage 6
Staqe 5
16 16
2 12
Mio Cos 99
Coostruct mio Cost Spanninq Ares fo the belouo
qapb uSinq P1imS Aiqorihm.
5
3
30
55
6-
Slaqe Staqe2
0
slaqe 3
staqe4
5
6 5
-
35
6
Mio Cost 1o5
5
matri of o vetei
1adiaoCu an
aod stose d se of
Coo puted as a
3
6
12
io kushkar'S Alqovithm
Staqes
slage Slaqea
O
1O
10
12
Slaqe 3 sAage
y
12
SAaqe 5 slaqe6
(
Algoilhm
(0-0
50
30 35
25
5 5
20Y6
SHaqes io KruShka's Alqoithm
Staqe slaqe
O1
Staqe3 Staqe4
O
25
20 6
Staqe 5
O-
25 5)
Mio Cost 10 5
Algovihm KruShkal ( ¬, cost,o,,)
S the Sel of edes io G
I G haso veslices Cos [U,v) is be
I Cos of edqe Cuv) iS Ihe Sel of
+1
i,J:zo,4 [i,2):v
mio CoS mio Cosl 4 Cosl[u,v)
Uni0nLj,KJ)
G4n-t tbeo wite "'No
Spanninq -ee*)
e1Se
e l u o mio cosl;
Koapsack Problem
>iiven objecis aod a Knapsack objeci "i
as a
weight aod koapsack has Capacit
m1 a actioo i , 04xigi 6f objecl
S Placed iolo be koapsac k
Tben a Proi of Piiis eavoed
1 Ao o PiX Subjected to
14ison
Co,213) 20 3
- -16
15
Co,1, 'a) 20 31.5
3115 o
Oplimal Profit =31.5
a
Gveo ivecdc d qvaph Gi:(V,C)
weigb-linq funcdion Cos fov be edqe o
verter of
and SouCe veci The Stavtinq
he poblem iS efee d as Souce and the
as vertex S deslinalioo
PoSitivve
and Some
The Shotest path blw Vo
KoowO V is ao Ovdevinq amooq a
othe
Subset of edqes.
30
6)
2-10
3 -lo 4+20 30
2 3 4 -1o +2o +15 -45
12 3 5 - 1D+2o+5 3 5
1 66- 30
2 395 1- 10 +20+5 +1 = 42
) l o i i a l l oe need to ideotify the Staitu
Nete aod Sou< Ce and deStination
1,2,
1,2,5
1,2,6
1,2,1
)loclude the vevtei 3
2,3, y y5
1,2,3,5'y 35
2,3,6y
12,3,1
5) loclude vete
1,2,3,5,6
1,2,3,5,1y- 42
DiFereoce between Divide G cooque, and
Geedy Method
melbod a
2 lo bis lechnique 2 qxe edy
he pvo blem
S Gel of easible
enerattd
divided iolo Small Solu-lo iS q
and op-timal Soluloo
Sub pioblemS and th ese
Sub problemS ave iS Picked up
Solvedndepeodeotl
fioally i CombioeS
a Sub -proble mS o
qel So lutionS
the
3) 9reedy me-lhod
3) lo Ahis melhod
op-li mum 'selecli oo iS
dupiCalioos io h o u t eviSioq p1eYiouSl
Sub Solu iion S ae qeneated Solu-liooS
neqlec+ed
ae 4 APPH Calioos
ae Koa psack
4 APpi Ca-liooS lwi-lh dead lineS
Binay Seavch meq, Problem
ioimum CoS SPannioq -lve es
uick StrasSen s roalii
etc
mullip 1i Calion
1D3
20
u)CusDSHI -
2 4S
I3 uS
no Pcth
I-6
II int iali3c S
SCi3 falsc dist Cil: (ost lV,)
fos num 2 n do
i Put u in S
SCuj:* bue;
for Ceach w adjacent to u with SCwj-kalse
Iupdate distants
i Cdist Cw> dist Cu3 + Co5t Tu,w)) hhen
o
3D
2 20
3 15
-3 3-y I9
-2,2-S 30
-3 3-6 25