Docsity

Huffman Coding-Advance Analysis Design-Lecture Slides, Slides of Design and Analysis of Algorithms

Partial preview of the text

Download Huffman Coding-Advance Analysis Design-Lecture Slides and more Slides Design and Analysis of Algorithms in PDF only on Docsity!

Lecture No. 26

Huffman Coding

•^

Huffman Problem

-^

Problem Analysis– Binary coding techniques– Prefix codes

-^

Algorithm of Huffman Coding Problem

-^

Time Complexity

-^

Road Trip Problem– Analysis and Greedy Algorithm

-^

Conclusion

Today Covered

Using ASCII Code: Text Encoding

•^

Our objective is to develop a code that represents a giventext as compactly as possible.

-^

A standard encoding is ASCII, which represents everycharacter using 7 bits Example R

epresent “An English sentence” using ASCII code

  • 1000001 (A) 1101110 (n) 0100000 ( ) 1000101 (E)

1101110 (n)

1100111 (g) 1101100 (l) 1101001 (i)

1110011 (s)

1101000 (h)

1110011 (s)

1100101 (e) 1101110 (n)

1110100 (t)

1100101 (e)

1101110 (n) 1100011 (c) 1100101 (e)= 133 bits

17 bytes

Refinement in Text Encoding

  • Now a better code is given by the following encoding:

‹space› = 000, A = 0010,

E = 0011, s = 010,

c = 0110, g = 0111,

h = 1000, i = 1001,

l = 1010, t = 1011,

e = 110,

n = 111

  • Then we encode the phrase as

0010 (A)

111 (n)

0011 (E)

111 (n)

0111 (g)

1010 (l)

1001 (i)

010 (s)

1000 (h)

010 (s)

110 (e)

111 (n)

1011 (t)

110 (e)

111 (n)

0110 (c)

110 (e)

  • This requires 65 bits

9 bytes. Much improvement.

  • The technique behind this improvement, i.e., Huffman

coding which we will discuss later on.

•^

Fixed-Length Code –^

All possible combinations of three bit strings 

x

x

If there are less than nine characters then three bitstrings enough

If there are less than five characters then three bitstrings not economical and can be considered two bitstrings

If there are six characters then needs 3 bits torepresent, following could be one representation.

a = 000, b = 001, c = 010,d = 011, e = 100, f = 101

Fixed Length Code

•^

Variable-Length Code –^

better than a fixed-length code

It gives short code-words for frequentcharacters and

long code-words for infrequent characters

•^

Assigning variable code requires some skill

-^

Before we use variable codes we have to discussprefix codes to assign variable codes to set ofgiven characters

Variable Length Code

Why are prefix codes?

•^

Encoding simple for any binary character code;

-^

Decoding also easy in prefix codes. This is becauseno codeword is a prefix of any other. Example 1•^

If a = 0, b = 101, and c = 100 in prefix code then thestring: 0101100 is coded as 0·101· Example 2•^

In code words: {0, 1, 10, 11}, receiver reading “1” atthe start of a code word would not know whether– that was complete code word “1”, or– prefix of the code word “10” or of “11”

•^

Tree representation of prefix codes

Prefix codes and binary trees

A
B
C
D
E
F

A

0

0 B

0

0

0

1

1

1

1

1

C^

D E^

F

-^

If each character is assigned a 3-bit

fixed-length

codeword

, the file can be encoded in 300,000 bits.

-^

Using the

variable-length code

(45 · 1 + 13 · 3 + 12 · 3 + 16 · 3 + 9 · 4 + 5 · 4) · 1,000 = 224,000 bitswhich shows a savings of approximately 25%.

Example: Huffman Codes

a^

b^

c^

d

e^

f

Frequency (in thousands)

45

13

12

16

9

5

Fixed-length codeword

000

001

010

011

100

101

Variable-length codeword

0

101

100

111

1101

1100

A data file of 100,000 characters contains only the characters a–f,with the frequencies indicated above.

0

Binary Tree: Variable Length Codeword

100

55

25

30

14 f:

e:

c:

b:

d:

a:

1

0

1

0

1

0

1

0

1

Frequency

(in thousands)

Variable-lengthcodeword

a^

45

0

b^

13

101

c^

12

100

d^

16

111

e^

9

1101

f^

5

1100

The tree corresponding to the variable-length code is

shown for the data in table.

Huffman (

C

n

C
Q
C

for

i

to

n

do

allocate a new node

z

left

[ z

]^

x

Extract-Min (

Q

right

[ z

]^

y

Extract-Min (

Q

f^

[ z

]^

f

[

x

] +

f

[

y

]

Insert (

Q, z

return

Extract-Min(

Q
)^

Return root of the tree.

Algorithm: Constructing a Huffman Codes

Example: Constructing a Huffman Codes

a:

b:

c:

d:

e:

f:

The initial set of

n

= 6 nodes, one for each letter.

Number of iterations of loop are 1 to n-1 (6-1 = 5)

Q:

Constructing a Huffman Codes

for i

Allocate a new node z left

[ z

]^

x

Extract-Min (

Q

) = c:

right

[ z

]^

y

Extract-Min (

Q

) = b:

f^

[ z

]^

f

[

x ] +

f

[

y ] (12 + 13 = 25)

Insert (

Q, z

Q:

a:

d:

0

14 f:

(^1) e:

0

25

c:

b:

1

Q:

d:

a:

b:

c:

0

14 f:

(^1) e:

Constructing a Huffman Codes

for i

←

3 Allocate a new node z left

[ z

]^

←

x

←

Extract-Min (

Q

) = z:

right

[ z

]^

←

y

←

Extract-Min (

Q

) = d:

f^

[ z

]^

←

f

[

x ] +

f

[

y ] (14 + 16 = 30)

Insert (

Q, z

) Q:

a:

0

25

c:

b:

1

0

30

14 f:

e:

(^1) d:

0

1

Q:

a:

d:

0

14 f:

(^1) e:

0

25

c:

b:

1