Docsity

Huffman Code, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Huffman Code and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Lecture 6:

Huffman Code

Thinh Nguyen Oregon State University

Review ‡^ Coding:

Assigning binary codewords to

(blocks of) source symbols. ‡ Variable-length

codes (VLC)

‡^ Tree codes (prefix code)

are instantaneous.

Creating a Code: The Data CompressionProblem ‡^ Assume a source with an alphabet

A^ and known

symbol probabilities

{ pi

‡^ Goal: Chose the codeword lengths as to minimizethe bitrate, i.e., the average number of bits persymbol

∑^ l

*^ pi^

. i

‡^ Trivial solution:

l^ = 0 i^

*^ i.

‡^ Restriction: We want an decodable code, so ∑^2

-li^ <=

1 (Kraft inequality) must be valid.

‡^ Solution (at least in theory):

l= –i^

log

p i

In practice… ‡^ Use some nice algorithm to find the codes

„^ Huffman coding „^ Tunnstall coding „^ Golomb coding

Huffman Coding ‡^

Two-step algorithm:1.^ Iterate:–^

Merge the least probable symbols.

-^

Sort.

2.^

Assign bits. a b c d

MergeSort Assign

0 1

0 10 11

0 10 110 111

Get code

More Examples of Huffman Code

More Examples of Huffman Code

More Examples of Huffman Code

Optimality of A Prefix Code ‡^ Necessary conditions for an optimal variable-length binarycode:

1.^ Given any two letters a

and aj

, if P(ak

) >= P(aj

) , then lk

<= lj

,k

where l

is the length of the codeword aj^

.j

2.^ The two least probable letters have codewords with the samemaximum length l

m.

3.^ In the tree corresponding to the optimum code, there must betwo branches stemming from each intermediate node.4.^ Suppose we change an intermediate node into a leaf node bycombining all the leaves descending from it into a compositeword of a reduced alphabet.

Then if the original tree was

optimal for the original alphabet, the reduced tree is optimal forthe reduced alphabet.

Condition 1:

If P(a

) >= P(aj

) , then lk

<= lj

, where lk

is the lengthj

of the codeword a

.j

‡^ Easy to see why? ‡^ Proof by contradiction:

„^ Suppose

a code X is optimal with P(a

) >= P(aj

), but lk

lj k

„^ By simply exchanging a

and aj^

we have a new code Y in whichk,

its average length

∑^ l
p^ is smaller than that of code X. ii^

„^ Hence, the contradition is reached.

Thus, condition must hold

Condition 3:

In the tree corresponding to the optimum code,

there must be two branches stemming from each intermediatenode.

.

‡^ Easy to see why?

„^ If there were any intermediate node with only one branchcoming from that node, we could remove it without affecting thedecodability of the code while reducing its average length.

a^

b

c a: 000b: 001c: 1

a^

b

c a: 00b: 01c: 1

Condition 4:^ ‡^

Suppose we change an intermediate node into a leaf node by combiningall the leaves descending from it into a composite word of a reducedalphabet.

Then if the orginal tree was optimal for the original alphabet, the reduced tree is optimal for the reduced alphabet.

a^

b

d 1 c a: 000b: 001c: 01d:

(^0) e

d 1 c

e: 00c: 01d:

Optimal Code Length (Huffman Code

Length)

1 )(

)(

< − ≤

S H l S H

source theof

Entropy:) ( log) ( )(

code optimalan of length Average:

1

2

i

m i

i^

aP

aP

l SH

∑= −

−=

Proof:

Extended Huffman Code

n SH l SH

/ 1 )(

)(

< − ≤

source the of

Entropy:) (

Code

Huffman of length

− Average: l^ SH

Proof: page 53 of the book

} ... ,..., ... , ... { }, ,... {^

2 (^11)

n times

1 (^11)

(^2) , 1

m mm

n m^

a aa a aa a aa A a aa A^

43 (^421) =

=

alphabet A in the symbols

n

nm