Two symbols only — Huffman coding

corethe floor Huffman cannot break

Answer

average 1.0000 bits/symbol, 100 bits total

Why this example is worth doing

With only two symbols every code is one bit, no matter how lopsided the probabilities. Huffman assigns whole bits, so it can never spend less than one bit per symbol, which is where its optimality claim runs out. The page states the bound precisely — Huffman is within one bit per symbol of the entropy — and points at arithmetic coding as the method that goes below it. This is the cleanest possible illustration of the gap.

Try your own input in the Huffman coding. Build the tree from text or from frequencies, with the merge order and canonical codes.

How the answer is reached

Codebook

Codebook — columns symbol, code
symbolcode
b0
a1

Merge order

Merge order — columns step, left, right, weight
stepleftrightweight
11090100

Compare with

Open this example in the Huffman coding

The field arrives filled in with this example’s input.

Note:

Notation this page assumes

  • Bit strings are written most significant bit first, and bit 0 is the least significant bit.
  • A width is stated explicitly wherever it changes the answer; nothing is silently sign-extended or truncated.
  • Ties in the merge queue are broken by the fully specified key the engine documents, so the tree is reproducible rather than merely valid.

Sources

  • Huffman, “A Method for the Construction of Minimum-Redundancy Codes” (1952)
  • Shannon, “A Mathematical Theory of Communication” (1948)