Kraft's Inequality

lean4-proofinformation-theoryvisualization
i=1n2i1\sum_{i=1}^{n} 2^{-\ell_i} \le 1

Statement

A binary code is prefix-free (instantaneous) if no codeword is a prefix of another. For any prefix-free code with codeword lengths 1,2,,n\ell_1, \ell_2, \ldots, \ell_n:

i=1n2i1.\sum_{i=1}^{n} 2^{-\ell_i} \le 1.

Conversely (McMillan's theorem), the same bound holds for all uniquely decodable codes, and for any lengths satisfying the bound there exists a prefix-free code achieving them.

Visualization

The code C={0,  10,  110,  111}\mathcal{C} = \{0,\; 10,\; 110,\; 111\} with lengths (1,2,3,3)(1, 2, 3, 3) is prefix-free. Verification:

21+22+23+23=12+14+18+18=1.2^{-1} + 2^{-2} + 2^{-3} + 2^{-3} = \frac{1}{2} + \frac{1}{4} + \frac{1}{8} + \frac{1}{8} = 1.

This code is complete (sum equals 1), meaning it is a full binary tree:

root
 |-- 0  (codeword)
 |-- 1
      |-- 10  (codeword)
      |-- 11
           |-- 110  (codeword)
           |-- 111  (codeword)

Each leaf at depth dd contributes 2d2^{-d}; the sum over all leaves of a full binary tree is exactly 1.

A code that fails the inequality, e.g. {0,01,10,11}\{0, 01, 10, 11\} (lengths 1,2,2,21, 2, 2, 2):

21+22+22+22=12+34=54>1.2^{-1} + 2^{-2} + 2^{-2} + 2^{-2} = \frac{1}{2} + \frac{3}{4} = \frac{5}{4} > 1.

Indeed 00 is a prefix of 0101, so this code is not prefix-free.

Proof Sketch

  1. Assign to each codeword ww of length \ell the set SwS_w of binary strings of length L=maxiL = \max \ell_i that extend ww: there are 2L2^{L - \ell} such strings.
  2. Prefix-freeness means the sets {Sw}\{S_w\} are pairwise disjoint subsets of {0,1}L\{0,1\}^L.
  3. Since all sets fit inside {0,1}L\{0,1\}^L (which has 2L2^L elements),
i2Li2L.\sum_i 2^{L - \ell_i} \le 2^L.
  1. Divide both sides by 2L2^L to obtain the inequality.

Connections

Kraft's Inequality is the feasibility constraint in the Source Coding TheoremSource Coding TheoremH(X)E[]<H(X)+1H(X) \le \mathbb{E}[\ell] < H(X) + 1Shannon's noiseless coding theorem: optimal prefix code lengths satisfy H(X) <= E[l] < H(X) + 1Read more → (Shannon's noiseless coding theorem). The counting argument mirrors the Pigeonhole PrinciplePigeonhole Principlef1({y})2  for some  yB  when  A>B|f^{-1}(\{y\})| \geq 2 \;\text{for some}\; y \in B \;\text{when}\; |A| > |B|If n+1 objects are placed into n boxes, some box contains at least two objectsRead more → applied to the set of binary strings.

Lean4 Proof

/-- Verify Kraft's inequality for the specific code {0, 10, 110, 111}
    with lengths (1, 2, 3, 3) using norm_num. -/
theorem kraft_code_eq_one :
    (2 : )⁻¹ ^ 1 + (2 : )⁻¹ ^ 2 + (2 : )⁻¹ ^ 3 + (2 : )⁻¹ ^ 3 = 1 := by
  norm_num

/-- The sum is at most 1 follows immediately from equality. -/
theorem kraft_code_le_one :
    (2 : )⁻¹ ^ 1 + (2 : )⁻¹ ^ 2 + (2 : )⁻¹ ^ 3 + (2 : )⁻¹ ^ 3  1 :=
  kraft_code_eq_one.le