
















































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 savings are between 20% - 90%
Image Source : plus.maths.org/issue23/ features/data/data.jpg
Example:
Space = (453 + 133 + 123 + 163 + 93 + 53) * 1000
Can we do better ??
YES !!
a b c d e f
Frequency (in 1000s)
45 13 12 16 9 5
Fixed-length codeword
000 001 010 011 100 101
Variable-length codeword
0 101 100 111 1101 1100
Space = (451 + 133 + 123 + 163 + 94 + 54) * 1000
F A C E
Encoded as 1100 0 100 1101 = 110001001101
To decode, we have to decide where each code begins and ends, since they are no longer all the same length. But this is easy, since, no codes share a prefix. This means we need only scan the input string from left to right, and as soon as we recognize a code, we can print the corresponding character and start looking for the next code.
In the above case, the only code that begins with “1100.." or a prefix is “f", so we can print “f" and start decoding “0100...", get “a", etc.
Example:
a b c d e f
Variable-length codeword
0 101 100 111 1101 1100
FACE = 11000100110
When we try to decode “1100"; we could not tell whether
1100 = 110 0 = “f"
or
1100 = 110 + 0 = “ea"
Example:
To see why the no-common prefix property is essential, suppose that we encoded “e" with the shorter code “110“
a b c d e f
Variable-length codeword
0 101 100 111 1101 1100
Variable-length codeword
0 101 100 111 110 1100
100
58 28 14
86 14
a:45 b:13^ c:12^ d:16 e:9^ f:
a b c d e f
Frequency (in 1000s)
45 13 12 16 9 5
Fixed-length codeword
000 001 010 011 100 101
a b c d e f Frequency (in 1000s)
45 13 12 16 9 5
Variable- length codeword
0 101 100 111 1101 1100
100
(^2530)
14
a:45 55
b:
f:5 e:
c:12 d:
Labeling :
leaf -> character it represents : frequency with which it appears in the text.
internal node -> frequency with which all leaf nodes under it appear in the text (i.e. the sum of their frequencies).
0 0
0
0
0
0 0
1
0
0
0
0 1
1
1
1
1
1
1
1
1 1
An optimal code for a file is always represented by a full binary tree , in which every non-leaf node has two children.
The fixed-length code in our example is not optimal since its tree, is not a full binary tree: there are codewords beginning 10... , but none beginning 11 ..
Since we can now restrict our attention to full binary trees, we can say that if C is the alphabet from which the characters are drawn, then the tree for an optimal prefix code has exactly |C| leaves, one for each letter of the alphabet, and exactly | C| - 1 internal nodes.
100
58 28 14
86 14
a:45 b:13^ c:12^ d:16 e:9^ f:
100
(^2530)
14
a:45 55
b:
f:5 e:
c:12 d:
0 0
0
0
0
0 0
1
0
0
0
0 1
1
1
1
1
1 1
1 1
Greedy Choice?
HUFFMAN( C)
∈
f:5 e:9 c:12 b:13d:16 a:
25 30
14
a:
b:
f:5 e:
c:12 d: 0
0 1 0 1
1
(^14 25) a:
f:5 e:9 c:12^ b:
d: 0 1 0 1
b:13 14 a:
f:5 e:
c:12 d: 0 1
55
25 30
14
a:
b:
f:5 e:
c:12 d:
0 0
0
0
1 1
1
1
100 55
25 30
14
a:
b:
f:5 e:
c:12 d:
0 0
0
0
1 1
1
1
0 1
∈
a b
y
x
Let a and b be two characters that are are sibling leaves of maximum depth in T. Without loss in generality, assume that f[a] < f[b] and f[x] < f[y]
Since f[x] and f[y] are the two lowest frequencies in that order, and f[a] and f[b] are two arbitrary frequencies in that order, we have f[x] < f[a] and f[y] < f[b].
x b
y
a
T T’
The difference in cost between T and T’ is
B(T) – B(T’) = Σ f(c) d (^) T(c) - Σ f(c) d (^) T’ (c)
≥
= f[x] d (^) T(x) + f[a] d (^) T(a) - f[x] d (^) T’ (x) - f[a] d (^) T’ (a)
= f[x] d (^) T(x) + f[a] d (^) T(a) - f[x] d (^) T(a) - f[a] d (^) T(x) = (f[a] - f[x]) (d (^) T(a) - d (^) T(x))
0 (cost is not increased)
Exchange the positions of a and x in T, to produce T’.