ADA - UNIT-3 - Chapter-1 - Greedy Method
ADA - UNIT-3 - Chapter-1 - Greedy Method
GuREEDY METHOD
feasible solvion.
whot Optimal Solution?
for 1 1 o n do
select Ca);
f Feasi ble. (soluton, ) Jhen
Soludon: = Solukion 4x
eluwn Soluion
3
rsodvandaa oGivecd Arproach
The ged melhod doen't awos produce
on oplimal Soludjon.
Jox
3o e Find he ony s palh in -lbe aph
()
(10
maximum
3 - 3, M= 20
, Pa, Ps)= (95, 24,15)
(, , W3) = (18, 15, 10)
R = 38 w 5= 6
T 5
X
w2 wS :6 1s >38
objech n) O 2
P3ofits CP) 25 24 15
e ghts () 18 15
25 24 15 = :38 :6 15
18
24 10 25
6 5 38
longe to shall
0
a IS 15 10
10
P 25 2 15
Maximum Siae M 20
M- W 20 185 = 5 (f 6 wraht 15
we have stace = e
No saa fa =0 but wigt 10
19, = 5
o (ox)+CIx1s) +(4x10)
O+15+ 5 = 20
= = 0x15)+(v2) +()
O94+ 75 =315
Totol Proit = 315
Constsan s; M
obiective max
P
Algorehm Gueed bnapsackm-n? i
lgorthn Gneedrkmapsa k(m,i)
3
kiedsdkjects 2 3 45 6
Prott)10 15 1 6 8 3
aeights(a) 23 5 4
W 5 5 :3 3 1 6 45 3
O 4 1 4
frachion X
MaX Si3t 15 K
15-I4 k%. (dhoom Max -6,thak weizlt =1 F3)
4-2 12
12-4 8
8-5 3
3-
2- 0
,(Ns) +(%3)+(sx)+(x0)+Cw1) +Hnt)
+(t*
2+2+5+0+IU+
15
(to) +(%*5)+(n)+Ó9+6:9)tina)}+0«i)
10+ 2xls + 15+0 +6+18 + 3
546
SEQUENCTNG TTH DEAD LINES
JOB
The Paoblem h e no. of s5obs, +heir profit
Omd deadlines obe Jiven md we have to find
a Sequence oSobs, which i be. Compleled rhin ts
o prot
Selecth e 1st sob oud txeude t bassniQt
a slot.
Le w omume hat -Ih procus daadine is
hen we hove Ho exetute he procus bä a i g n - t
-3, 1-2
-3 sot -3-3, 3-9] - [o,J
Jobs n = 5-
5 190
5Js decdli=
O,)%abead
Occuied
o,2)=())
2Nexd Job s
deadh
(0-1)-3-1,3)-,3)
Next 5ob J3
deadtis 2 (-0,)- chead aoued
(-2,76-2,9-1)- (o,1)
n-5
(R, B,s, e, )=(70,15, 10, 5,1)
(dn,d, ds, d,ds) - (,9,1,3,
Assianed
slot selected
Acion
None Assgn o , 23 20
Assjn to [®,j 35 ot15
ectt 35
Assgn to , 3 35+5
(0,1)adscad oupd
4)Jy duodh -3
3-1,3) (13)
J s doadline = 3
Cnauad ocuped>rect
Job Sequncin th Deadlanes
Let n-4 (R, , s, Ae)= (100, 10, 15, 97)
, di, ds, do)- , 1, 2, 1)
The feasible Solutions oud i r values ase
Seasi ble
Paocmina volu
Souion Seuen ce
COonbe deadane
conpleted b
The volue of a feas;ble Solution the.
J Sum o
he prots o he sob, n , o)
An oplimal solulion a -fcastble solutioni
wrh
maximum vaue
Hene e fied out all posstble ponmutakos the
o he
ohethen -he Sobs CAu be
Jamd check J
in
Soos
Sobs in
hu Pomuations (seguences
Psocd in a one o
whot violadhog the deadines.
J =
i nsonthe solo in Seuan te
he ob coUnt
3
etto
oplimal sBorage on -Hapes
hene ae nProRrams hat ane to be Sloed on a
în.
Timet needed to Yelreve proAKan s Proporkionol to
k-1
Lena4h of h pogam
Tfall Jhe programw Verived often he expected/
Mean Retyfeval Tme (MRT)
MRT
Eguàvalent b minimiging
Minimi* MRT
d)
e let n-3 (lh,la, ls) = (5,10, 3)
Fon hee 3 pronows, hou ma ovdein ane
fossible ?
os9ible Solulion le
3 6
SNo onde in
,2,3 5+(5+10-+(5+10+3) 38
,3, 2 5+ (5+3) +(5+3+10) =3
43
10+104+5)+o+5+
-
2, 3
d) =R=l
n 4hun
d , i =(n-+)4iy,0
,
R=
a Pomutation T wilh
h e ondi0 nol in ind1 easin odey
n
n-k+1)ik+-a4D4, +n-btDlia.
Ka
k#b
Find
Find camukakion I (increasina otder olung4h) oud
TC olur PonuBakion)
da)
(-141)4)+-4)¥s)-+(-34)19)+(-ty)
16+15+ 16 +12 59
n b- 3 (4, 8, 5, 12
a-2
auinlo chanyd
d)-(-+)A)+-24
) *®) + (-3+DK5H ogi
-y-+Da12)
10412
161 24 4
62
da)-dr) = (b-a(!ia)
3 70
- ) (8-5) -
Assi3
Algartlhm
provaws to tapes
Store (n, m)
n h e no: of proqrams and. m he no: of tapes ,
for I n do
write append PJiam
Ho Poimuotion for tope", )
(i+1) moo m
3)10 s, 4, ls, le, la, ls, la, Jo
10, 20,45,40, I, 3, 4, 54,23, 67
n Astending oder1 , 3 , 7 , 10, 20,93, 45, Su, 64, 10.
No-oHapes =3.
Tape : 1 10 45 70 +(+1)+0e+10tus}t
t4«)+(41o4+70)=R
To 3 5 8 12 3
T 4 6 10 18
Ta 5 7I 96
Tlal elicva kiw for T 3t(t5)+ B+5+8)+ (3t5+8t12) +
Co-+5+8412t32)= l15
T F 4+ (ut6)+(ut6+10)+ (ut6tlo +18) =12
T2 =5+ 5+)+ (5+7t1) + (3+7+|1-+26) 81
276
MRT 92
STNGLE- SoURCE SHORTEST PATHS
The stogle-Souce shotest path (sssp) Psohlen
Consists o findioa he shorlest patm be tween a iven
odex v Cmd all o-h votices in he
Jraph.
DijKsta's algorihm one of he bet
ud fo solving single Sownce
ahcritha
shovtest palh
problem.
Pigkstaa's Alaovihm:
BiksBa's adgo-thm an stoadave agorsthm hat
fmd he shorBest path Arom sounce voHex to all
othn votites in the
Jiah.
S t dousn't 0ok on
ziapm ih -ve weigsa sd
ta Feeda dg becawe t akoa elo on
ocal. optiwum.
APpli cattonbt Ma, Com puttn letuovking (RouBin
Sstem etc.
Rules
)Make Sune thue no -Ve Edges. Set distance to
SounCe Vo1tex a Zono Onnd set all olhwt distances to
dstinatiofn,
Relaxalion
iected roph
if do) + c(ov)< dv)
20
dv)- dcu) + c(o,v)
dtu c(
40
30
35
Q0 BrePre cted Gxaph
15
0
0o 0
0 30 0
2 -306 o 30
,3 45 35 Bo]
6
,6 45 135 -65
5
12.96,53 45 42
2,36,5A
,219,6,574
0
Votr Cost
de) + ctuN)< d)
0
0+20 0
306 30
35
6 30
42
45
20 1 20
15 3
Sekcded visted
votex Set
50 45
1,43 50 45 25
5 4,53 45
2 1,4,5, 45
3 14,5,3,3
Thuae no momins
edas to node
Pah
10
8)
6 10
Selc cled 3 4 5 6
Vot ex Set
io
6o0 8 t
2
0,143D 12 5 5
5 0,43 1 2 bo
0 15
o-1/A5A3 41 12 5 1 15
19
8
3 4sAPsD 1 B
,3,43
P odh0 1 4 5 6 2
)+cCu,v) dt) 8 327
O+4 t
rawbach
kstsa ag ma
n ma not wotk n ca of
-ve dge
fail on -ve eihts 9
L doo Dkslba
3
Considen
- tlee f e
but 4his
alread
vtsted.
J Suppo we consido als0
3 so
S0 Even hou a-Ve dge, ft wi) work,
3-2
H also alread
Vsted.
Gut fwe conside
3
also
Le av
ttin -93
0 3
-3
Ho Acdual -3 i ws but
kstra ave 3 be cauw lsea
V Ste d
ikata's
shortestlalh% (V, Cost, dist, n)
Algorthm
dist], 1 4i*n, s set to the lengih of he shortest
n vwrtices.
Path rom votex v to á in a dpaph G wHh
Set to 3o10. G eprsented ba its cost adjacenog
dist V
matix Cost[1:n, l:n.
fox i=l to n do
s[Jfalse;
distCi3= Cost(v,3
3
S:tYue; dist[vJ: = O.0 /put v n S
fo num = to n do
djst[UJ msnimum
uch that
sLUJ ue Put U n S
update distances
cost fum) hun
H (diot C> distoJ+
drst CUJ+ costCu,w];
ist C =
3
3
Aiiksta's alg ow detemins he lenghy the
of
shortest paths fvom No to all olhu voticon in G.
Oplimal Moe fallens:-
Grivenn soled files, thue one. maM wo o
Pa wve mee hem pBo a Sinle saled file. As
dal rent painings eguire | etOmovms af computing
ime. 1e. band to del oimine an oplimad (e, one epuirin
he fewest Cemparisons) o pa ww moge n'sorted
l s tpgdho. Th o- wmi % called a 2-0t
m patlems.
To meoge an n- e cord frle amd m-Yecovd frle
A B C (Moged A B)
3
5
8 5
shoudbe 2
8
n Ascendi
20 16
order 11
22
90
Hene
Hene Mag cam be done o on soded lists
To4al ti - bm+n)
hat hapened we have moTe-h on List?
List?
List A C D
Si3 2 6 5
ewe showld follohe 9-o mena list.
A
6 5
CD
23
A 3
1 A 6cD
6 52 3
1+13+1C= u0 I+5+1632 1
16
6+10 +5= 3|
Jaomhis obsovakion, we cOm Sothat
Gaed malhod ha we should -folloo' ans
We should mge apaiy of small Siaed s t to
&t the oestesuls.
aenunate wo-wOA Mon Tree -
Algovdhm to
stmct Teenode
kveenode *Jchild;
txec node. rchild
3
Algovthm TREE
node.
list b a alobal hsB of n sirge
for = | to n-1
do
heuw tsenode
/Muge hces wth smallkst
(PHlbald):. Least(kt) longthrs
PA-Ychild) := least (ksi);
(P1->cild)-> wught) -+
(P4- ig#): =
rchild)- wiakt)
Insut (kst, Pt);
lept in liut he
velunn Least (kst); /Tree mrge tree
Xs, Xu, Xs) w-th Si3t
Given 5 ales (M, Xa,
(2030, lo,5, 30). Ana geedue o kind opfimal uo
af Paiy ww
mogio1 ive an optimal solurthion
Aee epesentation
usibina mne
.
X3Xy X5
2030 ]
ondey
Axenge huu in nckeasing
5 10 20 30 30
X X3 X X2
A1s)
30
0st ist > 30 4 30
selct sze
30 30
(95
5+95+ 1s+ 60
35
905
Go
Xo X 5
The. total no: af e Covd moV forhis linar
moye tiee
- uaf ned
h e lngh of .
eto n a ln o d e
(leaf node)
t oYOot
12 13 15 1 Gro e
15 1 25
95 3
Toa no: of gs
mdhod-4 Add allnlennal rody
I +19t25+34+59= 148
wehod: (uu) + (u* 7)-+(348)+(12»1)+(a13)H(«)
16+28+24 +24 f26+15
9 ( s, 13, Xu, Y5, «)
(IS, 3, 8, 12, 13, 4)
Awone the all he camnd, n am ascendi
Anony order.
2Moge. two demnts amd place 4he. vesultant lamt
n he Sa ist
Repat sBep-2 untsk gt On eleme st
X Xu 5
We have -to ronfomm
muaaopoation
on -elna
Aerdagn8 12 1 I5
) 1 1315 19 Bro et
5
15 19 25
95 3
chanalurs.
Hujman Coding ued fos he losles Compression o
doda.
Voaioble. length Encoding
Oigom vanioble kgth code to all the chonacber
The Code lnth o chanaclen dependa on howeuent
toccu in he ven dext
8-bit
bina fom
A 65 Ol Dooobol 8-bit
66 O1 0000 10
C 6 7
68
Th abOVe mwaje Contoun 0 bits
20 x 8 16o bits
cho bit L41ed lwzh
he Huan Codang Hechn9ue, we. ca
Com he shing to Small s13e
5 Sh0 O01
C 6 6/20 O 10
3bt
4/20 000
/0 O0
20 habdo
6 C CA AG DDAGCC B S A 10
oo olo ol0 000 ool ool 011 bl
Now h Me ohe mouage will be
3 Go bith
36 (ode
ohen e Send a
meuag in Sncoludke above
How he YCcievo will Know abous Cods mmam
A- O00 6-O01 C Olo
Tolad 5 cham A, B,c, D, e
Sx8-bdh=yo rts
Joy Each 3-bit tede 5*3z 15
4o +15 = 55
Mna 60 biis
Table 55
I1s brt
D
chon A
Cot
6
A C
E
E A C
E
A C
A D C
C
This ree will hup ws to define the code
A C
30 Sending he. above shp OV a nedwoTk,
Comprd - Code.
(OUd
chanaclor Code S3
cguun
A 001 33 9
6
I 6x2 1 2
2 O00 x 3 6
5X$4H0 + Totalsi3e- ys bith
Message 913e 4 5 bts
8-bityX 5 o bity
AscTL lphabo
o d e
o +12 5bitbit
uaTt62 4bib
disAawle
Nole: Fon se also we ca find out - Sie?
Ed*; 342 313 + 4x 2 62+ 6 2
6 +9 48+10 4|2
45 bt
Hulman Aecoding
BC C DA c CB D A BCC DEAAEDA
(20
A C
10
Joom wis we cam gt th
Ov gna menag
A
Abgug
Bevdopd kj Bavid Hullman in 195
encodina followshe. pef:x ule
Most no1aded chonacler illet -Jhe. Small code
eat m Code
T e comkrit B{nlogn)
umhCodn ovlho
Tdiaigekion Put all Jmboh on a list soted
atcosding to they oegueo Coumt
Repeot untl he. kst has on one SJmbol left:
Fyomhe ist ck duwo $Jmbtols toilh -he
venuenco%
) o m a H)man sub ec.ha has -hos
oJmbok as child node amd CTeote. a aut nde,
Aign the Sum
ohc childaen% fepuenc
counM o he Panet Omd nsert tilo he ist
uch hat he 01dei i mantained.
9eletehe children -fom he Jist
3 Auian a Code woid foFach eaf bae on
he Palhe -Hsom -hu Yoot
Holman Codng compvilg The time. Compkxidj for
cncodigach
cncodi urigue choiaclor bad on t fsequenc
minimym fve9 fYOm -hu
poiita
uwc aks tlace 2 (n-1) imes Omd to Complexig i Clon)
Th im Complux O(nlojn).
Otain a se of oplimal Hulman codes fo he
Mewages MM M) wh eaive-fe9uencies
( (li,5,1, 8, 10,12, 10). Daw he decode
ce. for kis se of code,
Avnange he. dota tn ascending oTder in a table.
Avnanae
,5,4,8, 10, 12, 20
SuM he wo Aeouk freguenciu do make one node.
19,20,4
31)
(B
24, 31
66
A
1
20
0
Now agn ut cktd ob oandand
0 ( (34
(15
0
Freuencie Code
000
5
OlO
8 Ol1
lol
12 O0