A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Huffman Coding

Graduate Depth 92 in the knowledge graph ☐ I know this ☆ Set as goal
4topics build on this
414prerequisites beneath it
See this on the map →
Shannon EntropySource Coding Theorem→→Arithmetic CodingData Compression Basics
Huffman coding prefix code optimal code variable-length lossless compression

Core Idea

Huffman coding is an optimal prefix-free variable-length coding scheme for lossless compression of a known discrete source. It assigns shorter codewords to more probable symbols and longer codewords to less probable ones, achieving the minimum expected codeword length among all prefix codes. The algorithm builds a binary tree bottom-up by repeatedly merging the two least probable symbols. The resulting code satisfies H(X) <= L < H(X) + 1, where L is the average code length and H(X) is the source entropy. Huffman coding is optimal per-symbol; arithmetic coding can outperform it by encoding sequences as a whole.

Explainer

The source coding theorem says you can compress a source to its entropy rate, but it doesn't tell you how. Huffman coding, invented by David Huffman in 1952 as a student at MIT, provides a concrete, optimal algorithm for constructing variable-length binary codes for known discrete sources.

The algorithm is simple and elegant. Start with each symbol as a leaf node weighted by its probability. Repeatedly find the two nodes with the smallest weights, merge them into a new internal node whose weight is the sum, and assign the left branch a 0 and the right branch a 1 (or vice versa). Continue until only one root node remains. Each symbol's codeword is the sequence of bits on the path from the root to its leaf. More probable symbols end up near the root (short codewords); less probable symbols end up deeper in the tree (long codewords).

The resulting code is prefix-free — no codeword is a prefix of any other — which means the decoder can identify each symbol immediately without needing lookahead or delimiters. It is optimal among all prefix-free codes for that source: no other assignment of variable-length codewords achieves a lower expected length. The average code length L satisfies H(X) <= L < H(X) + 1. The gap comes from the restriction to integer-length codewords. When all symbol probabilities are powers of 2, the gap is zero and Huffman achieves entropy exactly.

Despite its elegance, Huffman coding has limitations. It works on one symbol at a time, so it cannot exploit correlations between symbols. It requires knowing the source statistics in advance (or using a two-pass approach). And the integer-length constraint means it can waste nearly 1 bit per symbol for highly skewed distributions. Arithmetic coding addresses all three limitations: it encodes entire sequences as a single number in [0,1), achieving rates arbitrarily close to entropy for any distribution. In practice, Huffman coding remains widely used (JPEG, DEFLATE/gzip) because it is fast, simple to implement, and the per-symbol overhead is acceptable for many applications.

Practice Questions 4 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Dividing Integers → Unit Rates → Proportions → Percent Concept → Converting Between Fractions, Decimals, and Percents → Operations with Rational Numbers → Two-Step Equations → Solving Multi-Step Equations → Equations with Variables on Both Sides → Angle Pairs: Complementary, Supplementary, and Vertical → Parallel Lines and Transversals → Corresponding Angles → Alternate Interior Angles → Triangle Angle Sum Theorem → Exterior Angle Theorem → Triangle Inequality Theorem → Similar Triangles: AA Similarity → Similar Triangles: SSS and SAS Similarity → Proportions in Similar Triangles → Right Triangle Trigonometry Introduction → Sine, Cosine, and Tangent Ratios → Trigonometric Ratios Review → Radian Measure → Converting Between Degrees and Radians → The Unit Circle → Graphing Sine and Cosine → Graphing Tangent and Reciprocal Trigonometric Functions → Derivatives of Trigonometric Functions → Antiderivatives → Indefinite Integrals → Basic Integration Rules → Riemann Sums → Definite Integral Definition → Probability Density Functions and Continuous Distributions → Cumulative Distribution Functions → Continuous Random Variables → Probability Density Functions → Expected Value → Shannon Entropy → Source Coding Theorem → Huffman Coding

Longest path: 93 steps · 414 total prerequisite topics

Prerequisites (2)

Leads To (2)