SimuVeda

Computer Science · Simulator 100

Compression Coding

Common letters deserve short codes and rare ones can have long codes. Type some text and watch a Huffman tree being built from the letter counts. Compare how many bits it needs with plain 8-bit text and the theoretical limit, then press Play to decode the bit stream one bit at a time by walking the tree.

How the model works

Huffman coding (1952) builds an optimal prefix code from symbol counts. Start with one node per symbol, weighted by its count. Repeatedly join the two lightest nodes under a new parent whose weight is their sum, until one tree remains. Reading the path from the root to a leaf gives that symbol's code: 0 for left, 1 for right.

No code is the start of another (a prefix code), so the bit stream can be decoded without separators: walk from the root, follow each bit, and output a symbol whenever you reach a leaf.

The total length equals the sum of all the internal node weights. Shannon's entropy gives the lower limit on the average bits per symbol for any code that treats symbols independently:

H = − Σ pᵢ log₂ pᵢ H ≤ Huffman average < H + 1

A fixed-length code needs ⌈log₂ k⌉ bits per symbol for k different symbols; plain text uses 8 bits per character (ASCII or UTF-8 bytes).

Counts are taken from the text itself, and the code table would also have to be stored or sent, which this count ignores. Ties between equal weights are broken by a fixed rule, so other valid trees exist with the same total length. Real compressors (ZIP, PNG) combine Huffman coding with LZ77 repeat matching.

Guided experiments

  1. ABRACADABRA. 11 letters: A × 5, B × 2, R × 2, C × 1, D × 1. Prediction: Huffman needs 23 bits (about 2.09 per letter), against 33 for a 3-bit fixed code and 88 as plain text. The entropy limit is about 2.04 bits per letter, 22.4 bits in all.
  2. Very uneven. AAAAAAAB. Prediction: Huffman uses 1 bit per letter (8 bits), but the entropy is only about 0.54 bits per letter: symbol-by-symbol codes cannot use fractions of a bit. Arithmetic coding or coding pairs of letters can.
  3. All different. ABCDEFGH. Prediction: every letter gets exactly 3 bits (24 bits), no better than a fixed-length code: compression needs some symbols to be more common than others.
  4. English. A short sentence. Prediction: spaces and E get the shortest codes; the text shrinks to a little over half of its 8-bit size.

Challenge

Four symbols appear with counts A 8, B 4, C 2, D 2. Build the Huffman code by hand. How many bits does the whole message need, and how does that compare with the entropy?

Reveal the explanation

Join C and D (4), then that node with B (8), then with A (16). Codes: A = 1 bit, B = 2 bits, C and D = 3 bits each. Total = 8 × 1 + 4 × 2 + 2 × 3 + 2 × 3 = 28 bits, 1.75 bits per symbol. The probabilities are 1/2, 1/4, 1/8, 1/8, all powers of two, so the entropy is also exactly 1.75: Huffman is perfect here. Type AAAAAAAABBBBCCDD to check.

References

  • Huffman, D. A. (1952), "A method for the construction of minimum-redundancy codes", Proceedings of the IRE 40(9): 1098–1101.
  • Shannon, C. E. (1948), "A mathematical theory of communication", Bell System Technical Journal 27: 379–423.
  • Cover, T. M. and Thomas, J. A., Elements of Information Theory, 2nd ed. (2006), ch. 5.