Docsity

Huffman Codes: A Prefix Code for Data Compression, Study notes of Data Compression

Partial preview of the text

Download Huffman Codes: A Prefix Code for Data Compression and more Study notes Data Compression in PDF only on Docsity!

I (^) used for data compression, typically saving 20%–90%

I (^) used for data compression, typically saving 20%–90%

I (^) Basic idea: represent often encountered characters by shorter (binary) codes

Example I (^) Suppose we have the following data file with total 100 characters: Char. a b c d e f Freq. 45 13 12 16 9 5 3-bit fixed length code 000 001 010 011 100 101 variable length code 0 101 100 111 1101 1100 I (^) Total number of bits required to encode the file: I (^) Fixed-length code: 100 × 3 = 300

Example I (^) Suppose we have the following data file with total 100 characters: Char. a b c d e f Freq. 45 13 12 16 9 5 3-bit fixed length code 000 001 010 011 100 101 variable length code 0 101 100 111 1101 1100 I (^) Total number of bits required to encode the file: I (^) Fixed-length code: 100 × 3 = 300 I (^) Variable-length code: 1 ·45 +3·13 + 3·12 + 3·16 + 4·9 + 4·5 = 225

Prefix(-free) codes:

(^1) a word, letter or number placed before another

Prefix(-free) codes:

  1. No codeword is also a prefix^1 of some other code.

(^1) a word, letter or number placed before another

Prefix(-free) codes:

  1. No codeword is also a prefix^1 of some other code.
  2. A prefix code for Example: Char. a b c d e f Code 0 101 100 111 1101 1100
  3. Encoding and decoding with a prefix code.
  4. Example, cont’d. I (^) Encode: I (^) beef −→ 101110111011100 I (^) face −→ 110001001101

(^1) a word, letter or number placed before another

Prefix(-free) codes:

  1. No codeword is also a prefix^1 of some other code.
  2. A prefix code for Example: Char. a b c d e f Code 0 101 100 111 1101 1100
  3. Encoding and decoding with a prefix code.
  4. Example, cont’d. I (^) Encode: I (^) beef −→ 101110111011100 I (^) face −→ 110001001101 I (^) Decode: I (^101110111011100) −→ beef I (^110001001101) −→ face

(^1) a word, letter or number placed before another

  1. Representation of prefix code: I (^) full binary tree: every nonleaf node has two children. I (^) All legal codes are at the leaves, since no prefix is shared
  1. Representation of prefix code: I (^) full binary tree: every nonleaf node has two children. I (^) All legal codes are at the leaves, since no prefix is shared
  2. Example, cont’d (a) the (not-full-binary) tree corresponding to the fixed-legnth code, (b) the (full-binary) tree corresponding to the prefix code:

Cost and optimality

Let C = alphabet (set of characters), then

Cost and optimality

Let C = alphabet (set of characters), then I (^) A code = a binary tree T

Cost and optimality

Let C = alphabet (set of characters), then I (^) A code = a binary tree T I (^) For each character c ∈ C, define f (c) = frequency of c in the file dT (c) = length of the code for c = number of bits = depth of c’ leave in the tree T

Cost and optimality

Let C = alphabet (set of characters), then I (^) A code = a binary tree T I (^) For each character c ∈ C, define f (c) = frequency of c in the file dT (c) = length of the code for c = number of bits = depth of c’ leave in the tree T Then the number of bits (“cost of the tree/code T ”) required to encode the file B(T ) =

c∈C

f (c) · dT (c),