Kraft's Inequality
Statement
A binary code is prefix-free (instantaneous) if no codeword is a prefix of another. For any prefix-free code with codeword lengths :
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 with lengths is prefix-free. Verification:
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 contributes ; the sum over all leaves of a full binary tree is exactly 1.
A code that fails the inequality, e.g. (lengths ):
Indeed is a prefix of , so this code is not prefix-free.
Proof Sketch
- Assign to each codeword of length the set of binary strings of length that extend : there are such strings.
- Prefix-freeness means the sets are pairwise disjoint subsets of .
- Since all sets fit inside (which has elements),
- Divide both sides by to obtain the inequality.
Connections
Kraft's Inequality is the feasibility constraint in the Source Coding TheoremSource Coding TheoremShannon'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 PrincipleIf 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.leReferenced by
- Source Coding TheoremInformation Theory