0% found this document useful (0 votes)
47 views

ADA - UNIT-3 - Chapter-1 - Greedy Method

The document discusses the greedy method algorithm. It describes that the greedy method makes locally optimal choices at each step to arrive at a solution. It is applied to problems like finding the shortest path or minimum spanning tree. The method may not always produce an optimal solution.

Uploaded by

Zeenath
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)
47 views

ADA - UNIT-3 - Chapter-1 - Greedy Method

The document discusses the greedy method algorithm. It describes that the greedy method makes locally optimal choices at each step to arrive at a solution. It is applied to problems like finding the shortest path or minimum spanning tree. The method may not always produce an optimal solution.

Uploaded by

Zeenath
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/ 47

UNIT-3

GuREEDY METHOD

The Aenoal Mehod


The GucedNedhod he implest oud shuoikt-
fowand approah.
t h e main function of -lhis approach , hos -lu
decision taken on -he kasis of-lhe Cuenl availobb
rfomation.
whatevU he. cuwnent
infotmatiov i pre sent, -he
decision s made wtHhout woaing about the
Eet
o the cuwuent decision fukune.
in

Jhs dechniue b used to deltmine. he feasile


So lution ha mq maj not be oplimal.
Lahat feasible Solution ?
So Me o he
ohe roblew have nmpu and Yequire

to otoin a sub hot Sedtsfie Some Constroints.


Ana subut hat Sa sftus 4hu constiaints b calle d

feasible solvion.
whot Optimal Solution?

The soluton which he best aud he most


favoxale solution in -lhe Subt knon as ophmal
Solukion
Tn ca o feasible, it more han one Solution

Satisfiuhe ven Cteria hen h o Solutions will be

Consiaored. a Jhe casible., ohoean, -he oplimal solukion


h e best Solukion among all he solutios.
The gecd mehod ma not produe he best
sesult ft all he piobles. % be.cmuu it akeooy joes
f8 he local best choste. to produce he global best result

Appicadion o Gved Melhod


used o findng the shortest path.
4 md o find he. minimum
spann tree usina
he prm's odg he kruskals aa.
W e d in a Job sequen cio h a deadkine.
Th also used do solve he frachonal knapsack
problem.
Algovithm Geddga)
asi:] Contai n nputS. as u 0

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

Hoe Root node is 20


The wergt o he nght chald : 3

To find he langest palh, he optmal solution


ot he moment 3. So, The areed ala wll choose 3.
20+34+| = 4 .

Th not an optmal solution.


Thote an
anolho paBh Jhat cavoues move welght
90+2+lo= 32.

Geed alg do not aluo Jive an


opknal/ feosible Soluion.
Knapsack problem
Also called as he factional knapsack problem
The Hes ae bioken in ordes to matimi3e

he Propti called fracional lnapsack (o ba) poblem.


This acblem Con be solved wth he help of usp 9techriyus

saute fovce appi0ach


Thi Opproach aitb al he possible. soluions with

al he dil\uent fiachom bd t a tme -(onsumin


appoach
)Gted appioach i
sn 4his, we calcwode he vaBio orofteght,
accoYdin e will select he em. The tHem wth
he htahest aho would be selecded first.
There 031e
basicall 3 approaches Ho solve he problem ; -
)select he tem bouo on he maximum pioit
Profit
minmuM wught

calulate he roto o pnoltoeigkt.


we One 3iven nobject omd a knapsack ey bag
Hh 4he capaciM obiecti has a weight k
whoe vaes som o N.

The pxoblem Ho A 4he hog Lwh he hlp o


Nokhecth nd dhe vesuding paoit ho to be
maxmm
Jomal, he pvoblem co be sAated as

maximum

M Omd 0£%;<1, l<i<n


in

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)

foX:=1 to n do X[J: =00,


m
fo i to n do

f (iCI>U) Jhr beak

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

eadtns aud h o uld ielda maXimum poit

Supfose Let u conside, a se of n johs.


Associaked oth sach scb i , dealline dzo Omd
a profit >o.
Fol an ob h e P1oft P Eanecd
% i
he 3ob completed b rts deadkne.
onl one machone s available for pro cewing
=

iobb An ophmal solution is he feasible Solution oh


maXimm proit.
machine quise "1ut of time in oderto
-the eretution af job.
Complete
Sort he obs m deed ba 4hen deadhnes,
An omo
Govo di:n]» md to elore -he. deadknes af th
otder othuir P-Valuey
Awonge oll the iobs in deceasinoider
/descendibauel

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

slot -1, 3 #-B,3 , 3


Jor Suppo, Or slot alread Occupied
hen we have to amange he slot -2, 3-11
3 slot 3-2, 3-1] [L2
he albove slot adso occupied, hen
Aain u

-3, 1-2
-3 sot -3-3, 3-9] - [o,J

Jobs n = 5-

,Pa, BPu, 1s) (100, 19, 28, 27, 52


Catyda, ds,du, ds) =
C2, 1,, 1, 3)
Sep-1 Amange all sobs in descendkng ovd
bud on
he pxost
,5,3, ,B)= 00, 5, 38, 27, 19
, ds, ds, du, dd) = , 3,, 1, 1)
step-2 Now ehave to oMign all he 3obs to
he coesponding slas.
Assioed Job Aclion prot
slot seleched
None asaijnHo Da] 100 T
To
*7unte ign do ,3J 159 (100t52)
i3f2.3 lign to , 190 100+53+ 38)
eject it 90

5 190

The ob quence - Oi, J5,


So he machine xetutes in dhe S , I5, 37 orde
Lweh he oit af 190.
Explanakion_

, s , Ro, R) - 100, 52, 38, , 119)


( alrady
(y ds, ds,dlu,d) - ( 3,, 1, OCcupied.
1 dadn
Max slot Dee oted

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

The sob Squeena % jd,54


The opkmal Soluion
duadlbw=2 J,2,43 h h
-1,0=1,)= (,2)
2) deadkne =2
C,adcodyauijnud
-3,T- a,-1)-(o)

(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

(0,1) 110 (loot1)


C3) 1,3 oY 3,1 15
3
L4 124 OPTIMAL
,3 25
5.
42
6 (1
00
(o
8. (3
4 24
O
(2, L No feaibk st
12 u
Hote he soukon 3 phmal.
Afeasible solukion for hin probem a
Sulut o8obs uch hat Eah sob in Jhs subt

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.

Agash edob (d, J, n)


/T a sel o scbs -hat con be completcd ka
has deadlanes.
Send 1sSob Ho Jarotoingo -hurrokt
fo% i: 2 t n do drod uatd

H (all 3oba in U i Cau be Completed


hcis deodlbnes) -Jhen J: J o 3 ;
3
3
To
To Complete a ob, one. has b piocu he sob/an acton
for one Unt of tîme.
0n one mackine y avai labe. od
for Proceing obs
on
3A feasible Solution for hi roblem a sobset af

s 0t Sob uch had £ach dob tn hia subut cau be

Completed hi. deadle.


MgasdhmJs(d, a,n)
Job must aomoged uch
be that 2 P
dfsts d[ol+J), 11<K
doj: Jlo: o snitali salion
J11 Agn he st Jo
No: 0 sobs takn
481 i 2 to n do

uhle((dCsjzdl) omd (A[ao] #) do =y-1


#wchchur dithna uired
if (CA[O J<Jt) and dCi]>hen 1 check 4he
feasibilbg
insert 1 nto Jl 3.

step-1 sat exiskg 3ob


downwcds.

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

Ccomputet dape. of deggh


Proaam I has. lenzlh 12f<n
Let pvoaams ane sdored in he ovde T- iu, iz -

î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

obieclive. to fand Pomutation for n progvams

Sothat Mean Retieval Time (MRT) % mimnt zed.

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

23,1 10+ to++ (10+3+5= 41


3,1,2 3 +(t5+ (3+5+10) -2
6 3,2,1 3+ (t10) +
(3+1o+5)-34
The Opimal oydein
3,1, 2
Find all he pormutaltom af posrams Cmo
na
find
opkimal solukon wtn mum MRT).
MRT
rawback:-
38 383 126
31 31/3 10.33 JO loe value of
3 ime b e hgh to fid
e3- l433
l s = 13:33 al Po9sble þomutations.
The othn approach
29
34/3 1:33 Gied Apooach to ollaan
opkimal solurton.
GTT eed Approachto obtan gBimal solution
sHove programs in înceasig o1des Of +hür lergths
oidey
(Axcendtra odey) USin o sodio1
Time eguired for Sovin nlogn
Mhe8iven Example,
(, , )= (6, 10,3)
TIncreasin ovder of
mceasing lengths ane 3,5,10

So he Cvderina 0f p1oq1a% i 3, 1,2

Theovem T < - --n hen the orderina


1 4 j < n , minimt zes

d) =R=l

OV all possible Pzumutatiom af


he i

Prool L e I î,, ia,--- in be ar Pormutation o

index set S1,2, - -


-

n 4hun

d , i =(n-+)4iy,0
,
R=

K=l n-1+1 - ntimy st pr wil be


Ted

Tis 4ha Odo slore


-Ihv p1o35 in -h
ínteasin Order
hone Exsts a amd b uch hat a<b amd

a li, the nlnchaggo ia ond ib yesult in

a Pomutation T wilh
h e ondi0 nol in ind1 easin odey
n

n-k+1)ik+-a4D4, +n-btDlia.
Ka
k#b

Subbsacng d(I) Aom da),we obtain


d)- d) = t-a+)Cli-k) +0-b+)(h-a
=- C a - )
0
Hence no Pmuloation that n
not în íncyeastn9
oxder o Ce have maimum d.

Let n-4 Cnd lenths =


12,5,8,4

Find
Find camukakion I (increasina otder olung4h) oud
TC olur PonuBakion)

I(1,2,3,4 (4,5,8, 12) inyea Si09 olde


(, 2,9,9 C4,8, 5, 19)
on inCo Sip dey

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

d(a-d()= G-34 3 3>o

da)-dr) = (b-a(!ia)
3 70
- ) (8-5) -

I w e ake hat am odher Combi natiorn

not stored in am ncreasq Oder


Odey,
Jahelher lenghs ane

hn 4hu time 9uired aveattn.


stoles
Thgh he Grerd appr0ach, alwaxs
n its inoseasin odey to Jet he miimam
he roaram
etyievol ime.

Assi3
Algartlhm
provaws to tapes
Store (n, m)
n h e no: of proqrams and. m he no: of tapes ,

- 0 / Nest lape do stoe on

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

Tape i 3 9O 54 3t(342) + (3420159)-lo3


Tape2 23 64 + (1423)+(4423+69)-134
Tolal Rebieval timt = 4 + lo 3+ J34 = 43

Mcon Rebtewal Tme=43= 143


Find an oplimal lacemeret for 3 programs on
4opu To,Ti,T2 hone the progsams ane lrgths
12,5,8,39,7,5,18, 26, 4,3, 11,10 amd 6.

n 3 No: of tapes 3 (Te, Ti, Ta)


Auanto he pogsom ngtm in mcaeasing9 otder of

length 3,4,5,5, 6,7,8, 10,11,12, 18, 26, 32

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

Relox all voHice


adsacent o hu Cwonent Vortor.
9 choo the clout Vonex a next c wwlent Vo1tex.
9 Repeat Ruls 2 omd 3udil tke to each he

dstinatiofn,
Relaxalion
iected roph
if do) + c(ov)< dv)
20
dv)- dcu) + c(o,v)

Edg» Yepesent cost


SounveHexdU) -0
A +c(u,v) < d)
O+20
20 0 hern
d)dtu)+ ctvav)
O+90 =20
20
20
o

dtu c(

du) + c(u,v) < d)


.0 +10 <po
30 24o.
20
20 46

40
30

35
Q0 BrePre cted Gxaph

15
0

sde cted Vstled


dod dsg drg d drgjd
vOHeX y Set

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

Poh ->4 >52 3 6

Pah
10

1,4,5 95 shonest poaths


,4,5,2 fvom
1,3 I4,5,2,3
undiecrd
Cnnaph

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

node 4an dh edg


3

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

Relunmine n- pah Aom V.

ChooL U fom n on-hos Vertscs not în 3

djst[UJ msnimum
uch that
sLUJ ue Put U n S

adsacent to u tth s l ] - fahe) do


fox (Each w

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

cutr Possib ntm kcovd moves-the obviou, chorce

b at fach step mege he Hwo Smallestiles tog-+het.


TheooO mege pateins con be epe sented b

binao meqe +rets

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

Toal cost (13

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

Muae Xuy A X hich ane Smallen m si3e to et


15 ecoTd moves

select leaut si list }IS A 20


35

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

disBance -from Jhe Tool to he extonal node


d for file .

h e lngh of .
eto n a ln o d e
(leaf node)

dg5)+ (axo)-+(onao) +(xso)+xao


yow Lecf

t oYOot

15+30+ 4o +60 +60 =


205
0
( x 1 , X1, 13, Xu, Y5, «)
(15,1, 8, 12, 13, 4)
)Awan the all lkmend n an ascendi order.
Muge. tuo cdmnts amd place 4he vesultont dumt
in he SaME ist
Repat step-9 untsl Jt One elenaent
X2X3 Xu X5
We have to
Por-fom
merga opo1otion
On2-elnants
) 8 11 12 13 15
Aerdi

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

.'.Tota no: of wug


wdhod-1_ Add all talonal nodn
1
+19+25+34+51 = 148
wehod. (u*u) + (us )+(3»8)+(2v)+(113)F(w1)
=6+28+2 +24 t26 +15 8-
Human Codes:
Hu man Codip y a Hechni2ue of Compressing
daka to veduce. it s13e. wthout losing a o he
dedas
Hulman coding geocnalla wfu o Compras
he. dala n which lhoe one fequentl occuing

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

943 oo Encodng Fincd- length Encoding


Schemt
Vaaiobe-lnh encodin
Fixed lth encodina Evot chanacdo amijnud
bino Code same no: COf bits.
BccABBDDA ECC BBAEDD CC
Meoge
Lleh 20
Thib meae S n t using AscIT Code.

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

>Human codin fist Ceots a tree using hh


heguencios o he chanactur md thon genuiates Code
foEach chanactot
Once he dlata Encoded, ray to be decoded.
Pecodlin done usinhe Same tvee
Moaje BccAB6DDAECC BBAEPDCC

chono clu Count/ reguen Code


1-bt
A 320 O00
2-bth 0 0

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

This allabou ed -ungh Encading

ususin Human codin,


e Cam
Stil
Scduce he Size ofhe muua
Human code olouas Optmal Muge Parlem
done wh he help of th
Iman Codin
oltloroma steps.
Caltulote he eguenc o fach chanacdo in -he sihr

D
chon A
Cot
6

2 Scllhe chonacon in incveasinorder o he

CoUnd e Tha Oote elored in pron oueUe.

A C
E

3)Moke Each vmue chanatn a a deaf nod

Actordirgoptima Mu Patonn nuhod, Mun two


Smallest One to make one node.

E A C

E
A C
A D C

C
This ree will hup ws to define the code

Jor Each, lust- side Edge maaks hum 0s O


raht homd side edges
20

A C
30 Sending he. above shp OV a nedwoTk,

we have. a to Send he hee. wel1 as the above

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

Along wh mewag, e have to Send ha.


4atchot e (0 tree for * de code t h muaze

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

Hlullman codzgg will akop Emae thu prfir dule,

eush, Hulman codip prevonty


Prefx Code

Onbjgutn the decodinz proco


Pefix Code meo No Code n prefir of modher
Code a Code amociaded widh a chanado

Should not be pesemt in -he prefr of arodlhen


Code A-O O0
-
C- 01
A A
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.

Now theaut noduy OUe 4, 8


12,15, 19,20

19,20,4
31)
(B

24, 31
66

A
1
20

0
Now agn ut cktd ob oandand

saht chilol t o Encode epuencits,

0 ( (34
(15
0

Codt fSl venresuenci 0te

Freuencie Code

000
5

OlO

8 Ol1
lol
12 O0

You might also like