Skip to content

Huffman coding

planning ahead

Level 3 of 3

Also called Huffman code and Optimal prefix code.

test yourself

Six questions, four to pick from and two for paper.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

How many comparisons does the sort make lining up the letters of deadbeef, the earlier letter first on a tie?

the answer, and why

the answer is D

In letter order a 1, b 1, d 2, e 3, f 1; on a tie, the earlier letter goes first. Round 1: a with b and d with e, 1 each. Round 2: a, b with d, e takes 2, as both of the right run weigh more. Round 3: a, b, d, e with f takes 3, f going after a and b. That is 7.

if you chose another

  • That counts the 8 letters of the text, but the sort lines up the 5 different letters.
  • That is the joins, one fewer than the 5 letters.
  • That counts the different letters, not the comparisons between them.

question 2

How many joins does the method make on committee?

the answer, and why

the answer is A

Committee has 6 different letters, c, e, i, m, o and t. Each join leaves one tree fewer, from 6 to 1.

if you chose another

  • That counts the different letters; the first join already leaves 5 trees.
  • That counts the letters of the text.
  • That is the comparisons the sort makes.

question 3

A word's letters weigh 1, 2, 3 and 5. The method's joins weigh 3, 6 and 11. How many bits does the word take, each letter's code written every time it appears?

the answer, and why

the answer is B

The bits are the joins' weights added up, 3 + 6 + 11. Check by depths. The 5 is 1 deep, the 3 is 2 deep, and the 1 and 2 are 3 deep. 5 + 6 + 3 + 6 is 20.

this tests where the formula comes from, on the learn page

if you chose another

  • That is the last join alone, every letter counted once.
  • That adds each code's length once, 1 + 2 + 3 + 3, not times its weight.
  • That counts the 11 letters again on top of the joins' 20.

question 4

Which word makes the sort compare the most?

the answer, and why

the answer is B

Its weights in letter order are 1, 3, 2, 4, the dearest order of 4. Round 1 makes 1 comparison in each of its 2 merges, and round 2 makes 3, so 5.

if you chose another

  • Weights falling, 4, 3, 2, 1, the fewest. 1 in each merge of round 1, then 2, so 4.
  • Weights 1, 2, 3, 4, already in order. 1 in each merge of round 1, then 2, so 4.
  • All weigh 2, and ties go in letter order, already in order. 4.

pen and paper

question 5about 8 minutes

Run the method by hand on peppers, keeping the two queues, one of letters and one of joins.

  1. Count the letters and line them up.
  2. Make each join, saying which queue each tree comes from.
  3. Without reading off the codes, give the bits from the joins alone. Then say which two letters share the deepest join, and why.
a hint

Take the lighter front of the two queues each time, and the letter on a tie.

a worked answer

p 3, e 2, r 1, s 1, lined up r, s, e, p. J1 joins r and s, both from the letters, weighing 2. J2: e and J1 both weigh 2, and the letter goes first; then J1, at 2, is lighter than p, at 3. J2 weighs 4. J3 joins p, a letter, and J2: 7. The bits are the joins added up, 2 + 4 + 7, which is 13. J1 holds r and s, J2 holds J1, J3 holds J2. So r and s sit under all 3 joins, 3 deep; e is 2 deep, p 1.

jointakesfromweighs
J1r 1 and s 1letters, letters2
J2e 2 and J1 2letters, joins4
J3p 3 and J2 4letters, joins7
The joins on peppers. At J2, e and J1 both weigh 2, and the letter is taken first.

this tests how it works, on the learn page

what a full answer includes
  • p 3, e 2, r 1, s 1; r, s, e, p.
  • J1 r and s; J2 e and J1; J3 p and J2, as drawn.
  • 13 bits, 2 + 4 + 7; r and s, under all 3 joins, 3 deep.

question 6about 8 minutes

A program builds a code for 1,024 different letters with the watch page's method.

  1. How many comparisons with weights falling?
  2. How many with weights in the dearest order?
  3. How many joins, whatever the weights?
a hint

1,024 is ten 2s multiplied together, so no run ever waits. Questions 1 to 3 of the learn page's section 4 count each.

a worked answer

No run waits, and each of the 10 rounds' right runs hold half the letters, 512. With weights falling, each right run is used up first: 10 times 512, which is 5,120. In the dearest order, 1,024 times 10, less 0 waiting, less 1,023 for the merges: 9,217. The joins are 1,023 either way.

Question 1: how many comparisons, FF, does the sort make on nn letters with weights falling in letter order, from 2 up?
  1. the move: how the sort goes

    the line:
    runs of 1,2,4,…merged in pairs\begin{gathered} \text{runs of } 1, 2, 4, \dots \\ \text{merged in pairs} \end{gathered}
    the row: 5, 4, 3, 2, 111111start221round 141round 25round 32a run, its length insideno partner, carried downthe first runall in one runa merge in round 2, its numbers45232345goes straight in, the other run used up
    5 numbers stand for 5 letters' weights, falling. Round 2's merge, 4, 5 with 2, 3, compares 2 with 4, then 3 with 4. Then the right run is used up, and 4 and 5 follow with no comparison.
    roundrun lengthmergescomparisons
    1122
    2212
    3411
    all45
    Five letters, weights falling. In rounds 1 and 2 the last run waits.

    why it is allowed: The watch page's sort is merge sort, from the bottom up. Each letter starts as a run of 1, in letter order. Each round merges neighbouring runs, comparing their front letters and taking the lighter. A last run with no partner waits. Every run is 1, 2, 4 and on long, except perhaps a round's last, which is shorter. A last run is a right run or waits, so a right run is never the longer. Which runs meet depends only on nn, never on the weights.

  2. the move: a merge's least

    the line:
    a merge compares at leastits right run’s length\begin{gathered} \text{a merge compares at least} \\ \text{its right run's length} \end{gathered}

    why it is allowed: A merge stops when one run is used up, and each letter of that run needed one comparison to leave it. By line 1, the right run is never the longer. So whichever run is used up holds at least the right run's length. For 4, 5 with 2, 3, at least 2 comparisons.

  3. the move: weights falling, the right run is used up first

    the line:
    F=b1+b2+b4+…F = b_1 + b_2 + b_4 + \dots
    run lengthright runscomparisons
    15 of 15
    22 of 24
    41 of 44
    81 of 22
    all15
    Ten letters, weights falling. At run length 2 the last run waits, and at 4 the last 2 letters wait. At 8 the right run holds only those 2.

    why it is allowed: With weights falling, each right run's letters weigh less than the left run's beside it. So the right run is used up first, one comparison each, the least line 2 allows. b1b_1 adds up the right runs in the round of runs of 1, b2b_2 of runs of 2, and so on. For 5, 2 + 2 + 1.

  4. the move: a ceiling

    the line:
    F≤n2⌈log⁡2 n⌉F \le \frac{n}{2} \lceil \log_2\, n \rceil
    numberlog× 2+ 11021428316452.32103.32
    Top, numbers, each double the last; bottom, their logs, each 1 more. 5 lies between 4 and 8, so its log, about 2.3, rounds up to 3. 10 lies between 8 and 16, so its log, about 3.3, rounds up to 4.

    why it is allowed: In a pair the right run is never the longer, so a round's right runs hold at most half the letters, n2\frac{n}{2}. Run lengths double until one run holds all nn, so there are ⌈log⁡2 n⌉\lceil \log_2\, n \rceil rounds. Multiply the two. For 5, half of 5 times 3 rounds is 7.5, and FF is 5.

  5. the move: a simpler ceiling

    the line:
    F≤n log⁡2 nF \le n \,\log_2\, n

    why it is allowed: Rounding up adds less than 1, and when nn is 2 or more, log⁡2 n\log_2\, n is at least 1. So ⌈log⁡2 n⌉\lceil \log_2\, n \rceil is at most twice log⁡2 n\log_2\, n, and n2\frac{n}{2} times twice log⁡2 n\log_2\, n is n log⁡2 nn \,\log_2\, n. For 5, FF is 5, at most 11.6.

  6. the move: read the bound

    the line:
    F∈O(n log⁡2 n)F \in O(n \,\log_2\, n)

    why it is allowed: By line 5, with the fixed multiple 1.

  7. the move: a floor

    the line:
    F≥n4⌊log⁡2 n⌋F \ge \frac{n}{4} \lfloor \log_2\, n \rfloor
    run lengthfull pairs holdtheir right runsa quarter of 5
    1421.25
    2421.25
    Five letters. In each round with a full pair, the full pairs hold 4 of the 5, at least half, and their right runs 2, at least a quarter. 2 such rounds, so at least 2.5.

    why it is allowed: Take a round whose runs are rr long, with 2r2r at most nn, so at least one full pair of runs fits, 2r2r letters. What is left over is fewer than 2r2r, so the full pairs hold at least half the letters, and their right runs at least a quarter, n4\frac{n}{4}. Such rounds have rr as 1, 2, 4 and on, while 2r2r is at most nn, so there are ⌊log⁡2 n⌋\lfloor \log_2\, n \rfloor of them. By line 3, FF adds up every round's right runs, so multiply the two. For 5, rounds of 1 and 2, so at least 2.5.

  8. the move: the rounded log against the exact one

    the line:
     log⁡2 n is below j+1,at most 2j\begin{gathered} \,\log_2\, n \text{ is below } j + 1, \\ \text{at most } 2j \end{gathered}

    why it is allowed: Call line 7's rounded-down log jj. Rounding down took off less than 1, so log⁡2 n\log_2\, n is below j+1j + 1. From 2 up jj is at least 1, so j+1j + 1 is at most 2j2j. For 5, jj is 2, and the log of 5, about 2.3, is below 3, and 3 is at most 4.

  9. the move: drop the rounding

    the line:
    F≥n log⁡2 n8F \ge \frac{n \,\log_2\, n}{8}

    why it is allowed: By line 8, jj is at least half of log⁡2 n\log_2\, n. Put that into line 7, and n4\frac{n}{4} times half of log⁡2 n\log_2\, n is n log⁡2 n8\frac{n \,\log_2\, n}{8}. For 5, FF is 5, at least 1.45.

  10. the move: read the bound

    the line:
    F∈Ω(n log⁡2 n)F \in \Omega(n \,\log_2\, n)

    why it is allowed: By line 9, with the fixed multiple an eighth.

  11. the move: both together

    the line:
    F∈Θ(n log⁡2 n)F \in \Theta(n \,\log_2\, n)

    why it is allowed: Lines 6 and 10 bound FF by the same growth.

try it with a small nn: at n=5n = 5, line 3 gives F=5F = 5, which you can work out by hand

this tests where the formula comes from, on the learn page

what a full answer includes
  • 5,120.
  • 9,217.
  • 1,023.