Download Data Compression: Huffman Code and Lossless Decompositions and more Slides Database Management Systems (DBMS) in PDF only on Docsity!
Huffman code and Lossless
Decomposition
- Data discussed so far have used FIXED length for
representation
- For data transfer (in particular), this method is
inefficient.
- For speed and storage efficiencies, data symbols
should use the minimum number of bits possible for
representation.
Average code length
Instead of the length of individual code
symbols or words, we want to know the
behavior of the complete information source
Average code length
Assume that symbols of a source alphabet
{ a 1 , a 2 ,…, aM } are generated with probabilities p 1 , p 2 ,…, pM
P ( a i ) = p i ( i = 1, 2, …, M )
- Assume that each symbol of the source
alphabet is encoded with codes of lengths l 1 , l 2 ,…, lM
Variable Length Bit Codings
Rules:
1. Use minimum number of bits
AND
2. No code is the prefix of another code
AND
3. Enables left-to-right, unambiguous decoding
Variable Length Bit Codings
- No code is a prefix of another
- For example, can’t have ‘A’ map to 10 and ‘B’ map to 100, because 10 is a prefix (the start of) 100.
Variable Length Bit Codings
- Suppose ‘A’ appears 50 times in text,
but ‘B’ appears only 10 times
- ASCII coding assigns 8 bits per character, so
total bits for ‘A’ and ‘B’ is 60 * 8 = 480
- If ‘A’ gets a 4-bit code and ‘B’ gets a 12-bit
code, total is 50 * 4 + 10 * 12 = 320
Variable Length Bit CodingsData Compression
Example:
Source Symbol
P C 1 C 2 C 3 C 4 C 5 C 6
A 0.6 00 0 0 0 0 0 B 0.25 01 10 10 01 10 10 C 0.1 10 110 110 011 11 11 D 0.05 11 1110 111 111 01 0
Average code length = 1.
Huffman code
- Constructed by using a code tree, but starting at
the leaves
- A compact code constructed using the binary
Huffman code construction method
Huffman code Algorithm
① Make a leaf node for each code symbol
Add the generation probability of each symbol to the leaf node
② Take the two leaf nodes with the smallest probability
and connect them into a new node
Add 1 or 0 to each of the two branches The probability of the new node is the sum of the probabilities of the two connecting nodes
③ If there is only one node left, the code construction
is completed. If not, go back to (2)
C .
A .
D .
F .
B .
E .
Huffman code Example
- Symbols and their associated frequencies.
- Now we combine the two least common symbols (those
with the smallest frequencies) to make a new symbol string and corresponding frequency.
C .
A .
D .
F .
BF .
B .
E .
Huffman code Example
- Here’s the result of combining symbols once.
- Now repeat until you’ve combined all the symbols into a single string.
Data Compression
- Now assign 0 s/ 1 s to each branch
- Codes (reading from top to bottom)
- A: 010
- B: 0000
- C: 011
- D: 001
- E: 1
- F: 0001
- Note
- None are prefixes of another
ABCDEF 1.
E .
C .
A .
D .
F .
BF .
AC .
BFD .
ABCDF .
B .
0
0
0
0
0
1
1
(^11)
1
Average Code Length =?
Huffman code
- There is no unique Huffman code
- Assigning 0 and 1 to the branches is arbitrary
- If there are more nodes with the same probability,
it doesn’t matter how they are connected
- Every Huffman code has the same average
code length!