0% found this document useful (0 votes)
8 views21 pages

Unit-4 Greedy Method

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)
8 views21 pages

Unit-4 Greedy Method

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/ 21

1

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

To Tavel fo One to ano-lher


Place
PIace ith e ss time o kilometey

O6 20

OPimal Solutioo - 6

Feasible Solulion 2o,10,3,69


Hou maoy mioimum Oumber o machines
equired o Cooplete he below Aask

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

Solution: 6 , 1l \otiali3e the Solution

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

Giveo Sel o oobs associ ated uwith job


S ao
ioleqe dead line di>0 aod Profi
Po Fov
an job i The Profil PiS
earoed an d only be ob i Completed
its Dead lioe
by
To Coroplete a job one has to po cess
b
hee ob and a machioe for unit olime

Ooly ooe machioe iS available fo


PocesSinq job
Afeasible Solution fohis P«oblem is a
Subsel ofiobS Sucb thal each job
o thbe Subset Can be Completed by i s
dead lioe

The value of feasible Solution is


the Sum Profits of jobS in

Ao opdimal Soludion S a e aSible


90lulioo he madim Nalu e

E)Let ou aod P, Pa ,Pa,Py) - lioo, 10,15,2)


and dead hoes (d,,da, da3, du) = ( , i,2,)
Whal is he optimal Cos and ha jobs

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.

Alqovithm Giveedyjob (d,J,o)


1S S a Sel of obs hal Cao be Compiecd
by thei deadlioes

T:13
fo = 2 Ao o do

f a o b S in Jv[:3 Can be Completed by


(their dead lines) theo J: Ju y

Mioimum CoSt Spanoio9 TreeS'

tet G ( v , ¬) be a undivected Coooecte d

aph.A Subqraph t: (v, E) of Gis a

Spanoioq tree of G ifand oolyi ' i s


a ee

> o a oioinMum CoSt Spanninq e e tbe


hae oeiqhtS assiqned Ao
Ho them
edqes
the CoSt
these oeighls naepeseot
aod
of Construc-tio0,length of be iok
of
So on
Giveo Such a
weiqhttd qaph 0ne wou d
heo wisb lo Sele cciAtes lo havve iDi) uro
olal CoSt O mioimum lo lal
leoqth
Tbe CoSt o a Spaooioq -lree 1 he Sum 0
Cosof Ahe edqes io bal Aree.
t o ewe bave vedices io 4he
-2
qaph then
there ae o PoSSible CombioalionS off
SPaonioq lve eS

O:54beve ave 5 PoSSibiles Ho


mioimum Cost Spaooioq tveeS
To evaluat+e the oioioum weig Ao
hose vees oilliake dime o qe
be Solutionn

To OveCme -the PoblernS oe ha e


o o alqosithm S.

Prim'S Algovilhm

Krusbkar's Olqovi-lhm
Prim's Alqoithm

3)
5 12

22

StageS in PrimS Alqo»ithm


e2 O
Staqe

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-

Slaqes io prims Alqosithm

Slaqe Staqe2
0

slaqe 3
staqe4
5

6 5

-
35

6
Mio Cost 1o5
5

Aigo1iHbm Prim (¬,cos,o,)


E s he Set of edqes in G

I Cos [n,nJis Ahe Cosl

matri of o vetei
1adiaoCu an

aph Such thal cost [i,3is


A mioimum Spanoiog lree is

aod stose d se of
Coo puted as a

Edqes io the a a +[i:o-1,1:2J


t,3,4[i,23) 1s an edqe in

the mioimum-CoS{ SPanoioq t e e

thefina CoS is eusned

Let Ck, ) be ao edqe ofmoimum Cos in ¬,


mio CoS CoS [k,1J:
Ci,:=K3 + [i,2]: -l
oY z to o do 1 ioitialie oeav

ifCcost Ci,17< Cos4 li,k]) -theo oea L7:

eise nea [i]:=kj


oea [k: nea [AJ: =0
Tor i2 lo o-l do
IFiod o- addiliooal edqes fo
Lel be ao íodez Suchlhal ne a LiJ to

CoS Cj,nea CiJ)is oioimum


i , : ji4 Ci,2): near ti3;
oio cos: mio cosl + Cos Cj ,oea [i1);
oea LjJ: =oj
fo k ' = l o o do / upddatt oeav
f oear k to) aod (cost [k, oea [k
Cos 4 Tk.j3))
theo oea K): =

eduro mio coS

3
6

Krushkar's Alqov ilhm

12

io kushkar'S Alqovithm
Staqes

slage Slaqea
O
1O
10

12

Slaqe 3 sAage
y

12

SAaqe 5 slaqe6
(

Mio CoSt = 99A


a)ConSstucl mo Cosl Spanninq le eg fov he
belo q1ap uSinq Pim KruShkats

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 ¬dqes inlhe miimum coSt


1Spanoioq vee The inal CoS S eturned

Construcd a beapP out o he edqe


Cost S uSinq heapi
fo t-1 to o do Paeot Ci)i= -1;
ICach verdea is io a dffeven set
i 0 mio CoS := 0.0

hile CG <o-1 and Cheap oot


empty) do
Delete a nioimum CoSt edge (uv)
rom
om heap and ve bea pify usinq djusi;
i:find (u); k: fiod (v)
k) Aheo

+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

The objeclive 1s Ao obiaio a


ioq
ofKoapsack tha oartimi 3es Abe
o-lal Profit eavoed Sioce the Knapsack
Capacily is m we equise Holal weiqht
oa chooseo objecs do be aimosl m.

T h e Poblem Cao be slated aS maimi3es

1 Ao o PiX Subjected to

14ison

The PofS and weiqhiS are neqalive


numbeS
A feaSi ble Solulion o aoy Sel iS Posiliv
ofweiqhl Sel (, ) Sal:ainq h
above CondilionS
An feasible oPlimal Solulioo is
>
feaSible whih is_atmi3cd
Conside the koapsack problen o:, m-20,P,P,Pa)
C2u,25,5) and (w,,w,w3 (18,15,10) Find he
optima Profil
C7,7,13) i1 Pi
"h,'3. J4) 1652425 - -13
(1,2/15,0) 0 28 2

Co,213) 20 3
- -16
15
Co,1, 'a) 20 31.5
3115 o
Oplimal Profit =31.5

Algovithm oreedy Koap sack (m,o)


11 P:n) and I o ) ConAaios the pofil and
weiqhtS xespecAively
Iof he n objecis o desed Such thal
PCi3jJ> i+i)lw Ci+
I MiS Ahe koapSack i3e aod [i:nJ is ihe
Solutio0 vecAo

fov 1 l Ao [iJ: =o.0, |


o
1oi Li ali3e
O.m)
o =I Ao o do

f w t:J >u) heo break


iJ: 10 U: U- w[iJ

i i o ) Aheo 1ti) v u[i


Sioqle Soute Shortest path Poblem

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

deler mine lhe Sh0T-est


blem iS o
The PTO
eticeS
lo all thR emaioioq
Pa-lhs YOm Vo

HS asSumed lhat a weiqhiS a

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

Sousee Stavtinq ve14tii


Destinalioo ve1tei 7
vev+ei1
)Dete.mine be Sbotest Palb from
, 2 y 10

3) loclude th e vetei 2 1,2,3y -30

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

Divide Conques eedy method

Divide C o oque is Geedy me-lhod is


uSed l o ob-ain
Jo obtaio a So lution
So a qiveo Problem op-limu 0 Solution

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

5)1 S . more efficieot


5 1 iS less effigol
Sinas SDuy Ce Sh0vtest path Pobl)F
Caiven
Caiven a directed g3aph C (v, E ; aweighi
Punction COSt Fo the edge o C and a Souce
Ve stex A eferned
Vevtex Vo The Stasting
as SduxCc and the ast Vestex is destination
The
problem detcomine the shostest
Paths om Vo t al the remain ing Vesticeg

assumed That, au weights arc Positiv

1D3
20

u)CusDSHI -
2 4S
I3 uS

no Pcth
I-6

Algor ithm Shortest paths l V. cost, dist,n)

dist Cj3. i sjin, is Set to the Length of the s hortes


I Path rom
rom Vertex V to ve stex J h a diagraph G

l ver tices di st Cv3 As Set to 3exo n is Cpresenttd


Il by its Co st odi ateny matix Cost C n, 1:j
o7 i td n do

II int iali3c S
SCi3 falsc dist Cil: (ost lV,)

SCV tue diSt Cv]: o.o Pud ins

fos num 2 n do

11deteimine n-I Pams 7om V


Choose x 0m among thosc VeCytices not
in s Su th thoat Cu mininMum

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

dist Cwl d i s t Culr Co st Cuwl:

o
3D

Shovttut PaThs to obtain in


in
USC augorihm
non dcCveasing ODder the Lengths of he Sho tcst
hom VCite tD cmaining VCIhs
VC In he
7thes in he
Parh hom
dYCcted Japh

2 20
3 15
-3 3-y I9
-2,2-S 30
-3 3-6 25

You might also like