← The collection

Huffman Coding.

Build a prefix code, change symbol weights and encode your own short message.

Interactive experimentintuitiveField note ·
INTERACTIVE EXPERIMENT / P18

Huffman Coding

Build a prefix code, change symbol weights and encode your own short message.

PLAY & EXPLAIN

Build a binary prefix code for four symbols. Change A’s frequency and see how the tree assigns short codewords to frequent symbols.

590
Huffman codebook
SymbolWeightCodeLength
A5001
B25102
C151113
D101103

0 · 10 · 0 · 111 · 0 · 10

Weighted average bits / symbol
1.750
Message bits
10
Fixed-width message bits
12

The codebook itself costs space to transmit, and a particular short message can expand. Huffman coding optimises expected code length among binary prefix codes for the supplied weights, not every compression scheme.

Weights are supplied and codebook transmission costs are omitted. Huffman coding is not universally optimal compression across all source models and block methods.

THE SHORT VERSION

Huffman Coding, explained.

Huffman coding constructs a binary prefix code with minimum expected codeword length for supplied symbol probabilities among such codes.

01 / THE MECHANISM

Why it happens

The two least-weight nodes are repeatedly merged. Deterministic tie breaking gives codewords for four symbols; the board compares a message’s coded length with two-bit fixed-width encoding.

Huffman coding constructs a binary prefix code with minimum expected codeword length for supplied symbol probabilities among such codes.

Read the result

Weighted average code length describes the supplied source mix. The actual short message can have a different mix and need not become shorter.

02 / FOLLOW IT THROUGH

A worked example

Build and use a four-symbol prefix code

  1. Inspect codewords for weights fifty, twenty-five, fifteen and ten.

  2. Encode the default message and compare its bit count with two bits per symbol.

  3. Change A’s weight to rebuild the code and distinguish expected source savings from the length of this particular message.

OPTIONAL DEEPER DETAILGo deeper: inside the model

Inside this model

The two least-weight nodes are repeatedly merged. Deterministic tie breaking gives codewords for four symbols; the board compares a message’s coded length with two-bit fixed-width encoding.

03 / BEYOND THE EXPERIMENT

Where this idea is useful

A practical use

Distinguish savings on a typical source distribution from savings on one unusually composed short message.

CHECK YOUR INTUITION

A common misconception

THE TEMPTING CONCLUSION

“A compression code must shorten every possible input.”

THE MORE USEFUL DISTINCTION

A short code for frequent symbols can lengthen rare ones, and transmitting the codebook also costs space outside this toy comparison.

What this explanation leaves out

  • Weights are supplied and codebook transmission costs are omitted. Huffman coding is not universally optimal compression across all source models and block methods.
ONE MORE QUESTION

Can one codeword be the prefix of another here?

No. Huffman construction yields a binary prefix code, permitting unambiguous symbol boundaries without the displayed visual separators.

TAKE THE IDEA WITH YOU

Does the message you are encoding resemble the frequency model used to design its code?

Associated thinkers

Associated withDavid Huffman ↗

Associations marked provisional are awaiting source review.

Further reading

Explore the original research or the teaching reference behind this experiment.