Introduction to Algorithms, Third Edition


cost of the tree T . Constructing a Huffman code



Download 4,84 Mb.
Pdf ko'rish
bet287/618
Sana07.04.2022
Hajmi4,84 Mb.
#534272
1   ...   283   284   285   286   287   288   289   290   ...   618
Bog'liq
Introduction-to-algorithms-3rd-edition

cost
of the tree
T
.
Constructing a Huffman code
Huffman invented a greedy algorithm that constructs an optimal prefix code called
a
Huffman code
. In line with our observations in Section 16.2, its proof of cor-
rectness relies on the greedy-choice property and optimal substructure. Rather
than demonstrating that these properties hold and then developing pseudocode, we
present the pseudocode first. Doing so will help clarify how the algorithm makes
greedy choices.
In the pseudocode that follows, we assume that
C
is a set of
n
characters and
that each character
c
2
C
is an object with an attribute
c:
freq
giving its frequency.
The algorithm builds the tree
T
corresponding to the optimal code in a bottom-up
manner. It begins with a set of
j
C
j
leaves and performs a sequence of
j
C

1
“merging” operations to create the final tree. The algorithm uses a min-priority
queue
Q
, keyed on the
freq
attribute, to identify the two least-frequent objects to
merge together. When we merge two objects, the result is a new object whose
frequency is the sum of the frequencies of the two objects that were merged.
H
UFFMAN
.C /
1
n
D j
C
j
2
Q
D
C
3
for
i
D
1
to
n
1
4
allocate a new node
´
5
´:
left
D
x
D
E
XTRACT
-M
IN
.Q/
6
´:
right
D
y
D
E
XTRACT
-M
IN
.Q/
7
´:
freq
D
x:
freq
C
y:
freq
8
I
NSERT
.Q; ´/
9

Download 4,84 Mb.

Do'stlaringiz bilan baham:
1   ...   283   284   285   286   287   288   289   290   ...   618




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish