Learning path

Full curriculum

Full curriculum

Arrows go from each prerequisite to the units that depend on it. Hover or focus a unit to highlight its path.

Unit content

Prefix-free codes and Huffman coding

A variable-length binary code must let the decoder determine where one codeword ends and the next begins.

A code is prefix-free when no valid codeword is the prefix of another. A sequence of prefix-free codewords can therefore be decoded from left to right without separators.

Huffman coding constructs a prefix-free code for known symbol probabilities by repeatedly combining the two least probable items into a new parent whose probability is their sum.

The resulting binary tree assigns shorter root-to-leaf paths to more probable symbols and longer paths to less probable ones.

For a memoryless source and symbol-by-symbol binary prefix coding, Huffman's greedy construction minimizes expected codeword length.

Huffman coding does not generally make the average length equal exactly to entropy because codeword lengths are integers. Coding blocks of symbols or using arithmetic-style coding can approach the entropy rate more closely.