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 TheoremArithmetic 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 ZeroThe Number ZeroCounting to FiveCounting to 10Counting to 20Counting a Set of Objects Up to 20Cardinality: The Last Number CountedMatching Numerals to QuantitiesSubitizing Small QuantitiesAddition Within 10Number Bonds to 10Addition Within 20Doubles and Near DoublesDoubles Facts Within 10Near Doubles Facts Within 20Mental Math Strategies for AdditionMental Math: Adding and Subtracting TensAddition Within 100Repeated Addition as MultiplicationMultiplication as Equal GroupsMultiplication: ArraysBasic Multiplication Facts (0s, 1s, 2s, 5s, 10s)Multiplication Facts Within 100Division as Equal SharingDivision as Grouping (Measurement Division)Division: Grouping (Repeated Subtraction) ModelDivision: Fair Sharing ModelDivision as Equal SharingDivision as GroupingBasic Division FactsDivision Facts Within 100Multiplication and Division Fact FamiliesRelationship Between Multiplication and DivisionDivision Facts as Inverse of MultiplicationRemainders and Quotients in DivisionDivision Word ProblemsMulti-Step Word ProblemsSolving Multi-Step Word ProblemsMultiplication Word ProblemsDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueIntegers and the Number LineComparing and Ordering IntegersAbsolute ValueAdding IntegersSubtracting IntegersMultiplying IntegersDividing IntegersUnit RatesProportionsPercent ConceptConverting Between Fractions, Decimals, and PercentsOperations with Rational NumbersTwo-Step EquationsSolving Multi-Step EquationsEquations with Variables on Both SidesAngle Pairs: Complementary, Supplementary, and VerticalParallel Lines and TransversalsCorresponding AnglesAlternate Interior AnglesTriangle Angle Sum TheoremExterior Angle TheoremTriangle Inequality TheoremSimilar Triangles: AA SimilaritySimilar Triangles: SSS and SAS SimilarityProportions in Similar TrianglesRight Triangle Trigonometry IntroductionSine, Cosine, and Tangent RatiosTrigonometric Ratios ReviewRadian MeasureConverting Between Degrees and RadiansThe Unit CircleGraphing Sine and CosineGraphing Tangent and Reciprocal Trigonometric FunctionsDerivatives of Trigonometric FunctionsAntiderivativesIndefinite IntegralsBasic Integration RulesRiemann SumsDefinite Integral DefinitionProbability Density Functions and Continuous DistributionsCumulative Distribution FunctionsContinuous Random VariablesProbability Density FunctionsExpected ValueShannon EntropySource Coding TheoremHuffman Coding

Longest path: 93 steps · 414 total prerequisite topics

Prerequisites (2)

Leads To (2)