

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


















Huffman Problem
-^
Problem Analysis– Binary coding techniques– Prefix codes
-^
Algorithm of Huffman Coding Problem
-^
Time Complexity
-^
Road Trip Problem– Analysis and Greedy Algorithm
-^
Conclusion
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
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
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
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 (
n
for
i
to
n
do
allocate a new node
z
left
[ z
x
Extract-Min (
right
[ z
y
Extract-Min (
f^
[ z
f
x
f
y
Insert (
Q, z
return
Extract-Min(
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)
Constructing a Huffman Codes
for i
Allocate a new node z left
[ z
x
Extract-Min (
) = c:
right
[ z
y
Extract-Min (
) = b:
f^
[ z
f
x ] +
f
y ] (12 + 13 = 25)
Insert (
Q, z
a:
d:
0
14 f:
(^1) e:
0
25
c:
b:
1
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
a:
d:
0
14 f:
(^1) e:
0
25
c:
b:
1