Docsity

Data Compression: Huffman Code and Information Gain, Slides of Database Management Systems (DBMS)

Partial preview of the text

Download Data Compression: Huffman Code and Information Gain and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

  • Huffman code and ID

Methods Used For Compression:

  • Encode high probability symbols with fewer bits
    • Shannon-Fano, Huffman, UNIX compact
  • Encode sequences of symbols with location of sequence in a dictionary
    • PKZIP, ARC, GIF, UNIX compress, V.42bis
  • Lossy compression
    • JPEG and MPEG

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

Average code length

Then the Average code length, L , of an

information source is given by:

M M

= l p + l p ++ l p

1 1 2 2

L

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

  • Enables left-to-right, unambiguous

decoding

  • That is, if you see 10, you know it’s ‘A’, not the start of another character.

Variable Length Bit Codings

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.

Variable Length Bit Codings

Question:

Is this the best that we can get?

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)

Huffman code Example

Character (or symbol) frequencies

  • A : 20% (.20) e.g., ‘A’ occurs 20 times in a 100 character document, 1000 times in a 5000 character document, etc.
  • B : 9% (.09)
  • C : 15% (.15)
  • D : 11% (.11)
  • E : 40% (.40)
  • F : 5% (.05)
  • Also works if you use character counts
  • Must know frequency of every character in the document

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.

C .

A .

D .

F .

BF .

B .

BFD .

AC .

E .

ABCDF .

ABCDEF Data Compression 1.

Huffman code Example

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!

Huffman code

Quiz:

  • Symbols A , B , C , D , E , F are being produced by the

information source with probabilities 0.3, 0.4, 0.06,

0.1, 0.1, 0.04 respectively.

What is the binary Huffman code?

  1. A = 00, B = 1, C = 0110, D = 0100, E = 0101, F = 0111
  2. A = 00, B = 1, C = 01000, D = 011, E = 0101, F = 01001
  3. A = 11, B = 0, C = 10111, D = 100, E = 1010, F = 10110