5.6 Huffman Codes

Huffman-Codes are equivalent to the game “20 questions”:

  • the code = sequences of answers to the questions
    • 0101 = no, yes, no, yes for example
  • we want to find the questions that allow us to minimise the number of questions we have to ask
    • i.e. recursively split the set of codewords in half (yes or no)

Note: main property of a Huffman-Code

  • by construction, any huffman-code assigns the two least likely symbols the same code, with only the last digit differing!

We have to do it from the back greedily like this because forward (or any other order) doesn’t work:

  • Counterexample for the forward procedure. Take , symbols .
  • Forward insertion. Start with , . Add by splitting the least likely leaf, : , . Add by splitting the least likely leaf, now : , . The lengths are , so
  • Huffman. Merge into , giving . Merge into , giving . Expanding back gives lengths , so

Huffman-Codes are optimal → via proof

  • reduction
  • induction on the construction of the code
    • start with 2 least likely → obvious code
    • add next
      • it’s more likely
      • so the two from before are the ones we extend (from to via the extension by one symbol)
    • thus we get next step of Huffman-construction
    • this is still optimal
    • repeat