SimuVeda/ Computer Science

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.

CollegeCollege information theory

Experiments 0 of 0 Challenge not tried

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.

Related simulators

Worked example

Text: MISSISSIPPI, 11 letters: I × 4, S × 4, P × 2, M × 1.

Build the tree: join the two rarest, M (1) and P (2), into 3. Join 3 with I (4) into 7. Join 7 with S (4) into 11.

Codes: S 1 bit, I 2 bits, P and M 3 bits. Total: 4 × 1 + 4 × 2 + 2 × 3 + 1 × 3 = 21 bits, about 1.91 bits per letter.

Compare: a fixed 2-bit code needs 22 bits, plain 8-bit text 88 bits. The entropy is about 1.82 bits per letter, 20 bits in all, so Huffman is within one bit of the limit.

Type MISSISSIPPI into the text box and check the tree and bit count.

Common questions

How does Huffman coding work?

It repeatedly joins the two least frequent symbols into a node until one tree remains. Frequent symbols end up near the top with short codes; rare ones get long codes.

What is entropy in information theory?

The average information per symbol, −Σ p log₂ p bits. No lossless code can use fewer bits per symbol on average.

Why can't random data be compressed?

When every symbol is equally likely there is no pattern to exploit, and the entropy equals the plain bit length. Compressing such data usually makes it slightly larger.

Keyboard shortcuts

Space
Play or pause
S
Step
R
Reset
F
Full screen
?
Show this list