Docsity

Huffman Trees - Data Base Management System - Lecture Slides, Slides of Database Management Systems (DBMS)

Partial preview of the text

Download Huffman Trees - Data Base Management System - Lecture Slides and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

  • Huffman Trees and ID

Motivation

The motivations for data compression are obvious:

 reducing the space

required to store files

on disk or tape

 reducing the time

to transmit large files.

Huffman savings are between 20% - 90%

Image Source : plus.maths.org/issue23/ features/data/data.jpg

Example:

Suppose you have a file with 100K characters.

For simplicity assume that there are only 6 distinct

characters in the file from a through f, with frequencies as

indicated below.

We represent the file using a unique binary string for each

character.

a b c d e f
Frequency
(in 1000s)
Fixed-length
codeword

Space = (453 + 133 + 123 + 163 + 93 + 53) * 1000

= 300K b its

Can we do better ??

By using variable-length codes instead of fixed-length codes.
Idea : Giving frequent characters short codewords, and infrequent
characters long codewords.
i.e. The length of the encoded character is inversely proportional to
that character's frequency.

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

= 224K bits ( Savings = 25%)

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.

Benefits of using Prefix Codes:

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"

Benefits of using Prefix Codes:

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

0ptimal Code

Constructing a Huffman code

Huffman invented a greedy algorithm that constructs an

optimal prefix code called a Huffman code. The algorithm

builds the tree T corresponding to the optimal code in a

bottom-up manner.

It begins with a set of |C| leaves and performs a sequence of

|C| - 1 "merging" operations to create the final tree.

Greedy Choice?

The two smallest nodes are chosen at each step, and this local decision
results in a globally optimal encoding tree.
In general, greedy algorithms use small-grained, or local
minimal/maximal choices to result in a global minimum/maximum

HUFFMAN( C)

1 n | C|
2 Q C
3 for i 1 to n - 1
4 do ALLOCATE-NODE(z)
5 left [ z ] x EXTRACT-MIN( Q )
6 right [ z ] y EXTRACT-MIN( Q )
7 f [ z ] f [ x ] + f[y]
8 INSERT( Q , z )
9 return EXTRACT-MIN( Q )
C is a set of n characters and that each character c C is an object
with a defined frequency f [ c ].
A min-priority queue Q , keyed on f , is used to identify the two least-
frequent objects to merge together. The result of the merger of two
objects is a new object whose frequency is the sum of the frequencies
of the two objects that were merged.

∈

The steps of Huffman's algorithm

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

Running Time Analysis

The analysis of the running time of Huffman's algorithm

assumes that Q is implemented as a binary min-heap.

  • For a set C of n characters, the initialization of Q in

line 2 can be performed in O ( n ) time using the

BUILD-MIN-HEAP procedure.

  • The for loop in lines 3-8 is executed exactly | n| - 1

times. Each heap operation requires time O(log n).

The loop contributes = (| n| - 1) * O(log n)

= O(nlog n)

Thus, the total running time of HUFFMAN on a set of n

characters = O ( n ) + O(nlog n)

= O ( n log n )

Lemma that shows that the greedy-choice property holds.

Lemma :

Let C be an alphabet in which each character c C has frequency f [ c ].
Let x and y be two characters in C having the lowest frequencies. Then
there exists an optimal prefix code for C in which the codewords for x
and y have the same length and differ only in the last bit.

Proof:

The idea of the proof is to take the tree T representing an arbitrary
optimal prefix code and modify it to make a tree representing another
optimal prefix code such that the characters x and y appear as sibling
leaves of maximum depth in the new tree. If we can do this, then their
codewords will have the same length and differ only in the last bit.
Why?
Must be on the bottom (least frequent)
Full tree, so they must be siblings, and so differ in one bit.

∈

Proof

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’.