Math 2.0

Information and communication theory, circuits Source coding and data compression Theorem

Kraft–McMillan inequality

Statement

Let D≥2D \ge 2 be an integer, let Σ\Sigma be an alphabet with DD symbols, and let ℓ1,…,ℓn\ell_1, \dots, \ell_n be positive integers.

  1. If there is a uniquely decodable code over Σ\Sigma with nn codewords of lengths ℓ1,…,ℓn\ell_1, \dots, \ell_n, then ∑i=1nD−ℓi≤1\sum_{i=1}^{n} D^{-\ell_i} \le 1.
  2. If ∑i=1nD−ℓi≤1\sum_{i=1}^{n} D^{-\ell_i} \le 1, then there is a prefix code over Σ\Sigma with nn codewords of lengths ℓ1,…,ℓn\ell_1, \dots, \ell_n.

Remarks

Part 1 is due to McMillan and part 2 to Kraft. Since every prefix code is uniquely decodable, the inequality, the existence of a prefix code and the existence of a uniquely decodable code with the given lengths are all equivalent.

For part 2, sort the lengths so that ℓ1≤⋯≤ℓn\ell_1 \le \dots \le \ell_n and pick codewords one by one as leaves of the complete DD-ary tree. The inequality guarantees that a free node of depth ℓi\ell_i remains at each step. For part 1, let S=∑iD−ℓiS = \sum_i D^{-\ell_i} and ℓ=max⁡iℓi\ell = \max_i \ell_i. Expanding SkS^k groups the terms by the total length mm of kk codewords, and unique decodability allows at most DmD^m such sequences for each mm. So Sk≤kℓS^k \le k \ell for every kk, which forces S≤1S \le 1.

Example: the binary variable-length code {0,10,110,111}\set{0, 10, 110, 111} is a prefix code, and 2−1+2−2+2−3+2−3=12^{-1} + 2^{-2} + 2^{-3} + 2^{-3} = 1. Without unique decodability part 1 fails: the code {0,1,01}\set{0, 1, 01} has 2−1+2−1+2−2=5/4>12^{-1} + 2^{-1} + 2^{-2} = 5/4 > 1, and the string 0101 can be read as one codeword or as two.

Going deeper

For a student

The inequality turns questions about codes into questions about numbers. For example, the minimum expected length of a uniquely decodable code is found by minimizing ∑ipiℓi\sum_i p_i \ell_i subject to ∑iD−ℓi≤1\sum_i D^{-\ell_i} \le 1. This leads to the lower bound by the entropy in Shannon's source coding theorem. Part 1 also shows that nothing is lost by restricting to prefix codes, which can be decoded symbol by symbol.

Where this leads

3 pages build on this one.

Further reading

  1. Web https://en.wikipedia.org/wiki/Kraft%E2%80%93McMillan_inequality
About this page
TagG1ZAWMBNE1
URIm2://G1ZAWMBNE1
Kindstatement, theorem
WritingW0 stub
MSC94A45, 94A29, 68P30
StatusThis copy of the site has no build data. Run m2 status G1ZAWMBNE1.
Same as
Changed3 November 2026

Markdown · JSON · Edit on GitHub

What this page needs for W1

  • an inventory row: the row of the inventory that the node accounts for (0 of 1)
  • a source with a locator (0 of 1)
  • edge uses out: each definition that the statement needs (0 of 1)

The writing levels say how complete the text of a page is. They do not measure its truth.

What links here

Used by