In coding theory, the Kraft–McMillan inequality gives a necessary and sufficient condition for the existence of a prefix code (in Leon G. Kraft's version) or a uniquely decodable code (in Brockway McMillan's version) for a given set of codeword lengths. Its applications to prefix codes and trees often find use in computer science and information theory. The prefix code can contain either finitely many or infinitely many codewords.
Kraft's inequality was published in Kraft (1949). However, Kraft's paper discusses only prefix codes, and attributes the analysis leading to the inequality to Raymond Redheffer. The result was independently discovered in McMillan (1956). McMillan proves the result for the general case of uniquely decodable codes, and attributes the version for prefix codes to a spoken observation in 1955 by Joseph Leo Doob.
Kraft's inequality limits the lengths of codewords in a prefix code: if one takes an exponential of the length of each valid codeword, the resulting set of values must look like a probability mass function, that is, it must have total measure less than or equal to one. Kraft's inequality can be thought of in terms of a constrained budget to be spent on codewords, with shorter codewords being more expensive. Among the useful properties following from the inequality are the following statements:
If Kraft's inequality holds with strict inequality, the code has some redundancy.
If Kraft's inequality holds with equality, the code in question is a complete code.
If Kraft's inequality does not hold, the code is not uniquely decodable.
For every uniquely decodable code, there exists a prefix code with the same length distribution.
Let each source symbol from the alphabet
be encoded into a uniquely decodable code over an alphabet of size r {\displaystyle r} with codeword lengths
Then
Conversely, for a given set of natural numbers ℓ 1 , ℓ 2 , … , ℓ n {\displaystyle \ell _{1},\ell _{2},\ldots ,\ell _{n}} satisfying the above inequality, there exists a uniquely decodable code over an alphabet of size r {\displaystyle r} with those codeword lengths.
Any binary tree can be viewed as defining a prefix code for the leaves of the tree. Kraft's inequality states that
Here the sum is taken over the leaves of the tree, i.e. the nodes without any children. The depth is the distance to the root node. In the tree to the right, this sum is
First, let us show that the Kraft inequality holds whenever the code for S {\displaystyle S} is a prefix code.
Suppose that ℓ 1 ⩽ ℓ 2 ⩽ ⋯ ⩽ ℓ n {\displaystyle \ell _{1}\leqslant \ell _{2}\leqslant \cdots \leqslant \ell _{n}} . Let A {\displaystyle A} be the full r {\displaystyle r} -ary tree of depth ℓ n {\displaystyle \ell _{n}} (thus, every node of A {\displaystyle A} at level < ℓ n {\displaystyle <\ell _{n}} has r {\displaystyle r} children, while the nodes at level ℓ n {\displaystyle \ell _{n}} are leaves). Every word of length ℓ ⩽ ℓ n {\displaystyle \ell \leqslant \ell _{n}} over an r {\displaystyle r} -ary alphabet corresponds to a node in this tree at depth ℓ {\displaystyle \ell } . The i {\displaystyle i} th word in the prefix code corresponds to a node v i {\displaystyle v_{i}} ; let A i {\displaystyle A_{i}} be the set of all leaf nodes (i.e. of nodes at depth ℓ n {\displaystyle \ell _{n}} ) in the subtree of A {\displaystyle A} rooted at v i {\displaystyle v_{i}} . That subtree being of height ℓ n − ℓ i {\displaystyle \ell _{n}-\ell _{i}} , we have
Since the code is a prefix code, those subtrees cannot share any leaves, which means that
Thus, given that the total number of nodes at depth ℓ n {\displaystyle \ell _{n}} is r ℓ n {\displaystyle r^{\ell _{n}}} , we have
from which the result follows.
Conversely, given any ordered sequence of n {\displaystyle n} natural numbers,
satisfying the Kraft inequality, one can construct a prefix code with codeword lengths equal to each ℓ i {\displaystyle \ell _{i}} by choosing a word of length ℓ i {\displaystyle \ell _{i}} arbitrarily, then ruling out all words of greater length that have it as a prefix. There again, we shall interpret this in terms of leaf nodes of an r {\displaystyle r} -ary tree of depth ℓ n {\displaystyle \ell _{n}} . First choose any node from the full tree at depth ℓ 1 {\displaystyle \ell _{1}} ; it corresponds to the first word of our new code. Since we are building a prefix code, all the descendants of this node (i.e., all words that have this first word as a prefix) become unsuitable for inclusion in the code. We consider the descendants at depth ℓ n {\displaystyle \ell _{n}} (i.e., the leaf nodes among the descendants); there are r ℓ n − ℓ 1 {\displaystyle r^{\ell _{n}-\ell _{1}}} such descendant nodes that are removed from consideration. The next iteration picks a (surviving) node at depth ℓ 2 {\displaystyle \ell _{2}} and removes r ℓ n − ℓ 2 {\displaystyle r^{\ell _{n}-\ell _{2}}} further leaf nodes, and so on. After n {\displaystyle n} iterations, we have removed a total of
nodes. The question is whether we need to remove more leaf nodes than we actually have available — r ℓ n {\displaystyle r^{\ell _{n}}} in all — in the process of building the code. Since the Kraft inequality holds, we have indeed
and thus a prefix code can be built. Note that as the choice of nodes at each step is largely arbitrary, many different suitable prefix codes can be built, in general.
Now we will prove that the Kraft inequality holds whenever S {\displaystyle S} is a uniquely decodable code. (The converse needs not be proven, since we have already proven it for prefix codes, which is a stronger claim.) The proof is by Jack I. Karush.
We need only prove it when there are finitely many codewords. If there are infinitely many codewords, then any finite subset of it is also uniquely decodable, so it satisfies the Kraft–McMillan inequality. Taking the limit, we have the inequality for the full code.
Denote C = ∑ i = 1 n r − l i {\displaystyle C=\sum _{i=1}^{n}r^{-l_{i}}} . The idea of the proof is to get an upper bound on C m {\displaystyle C^{m}} for m ∈ N {\displaystyle m\in \mathbb {N} } and show that it can only hold for all m {\displaystyle m} if C ≤ 1 {\displaystyle C\leq 1} . Rewrite C m {\displaystyle C^{m}} as
Consider all m-powers S m {\displaystyle S^{m}} , in the form of words s i 1 s i 2 … s i m {\displaystyle s_{i_{1}}s_{i_{2}}\dots s_{i_{m}}} , where i 1 , i 2 , … , i m {\displaystyle i_{1},i_{2},\dots ,i_{m}} are indices between 1 and n {\displaystyle n} . Note that, since S was assumed to uniquely decodable, s i 1 s i 2 … s i m = s j 1 s j 2 … s j m {\displaystyle s_{i_{1}}s_{i_{2}}\dots s_{i_{m}}=s_{j_{1}}s_{j_{2}}\dots s_{j_{m}}} implies i 1 = j 1 , i 2 = j 2 , … , i m = j m {\displaystyle i_{1}=j_{1},i_{2}=j_{2},\dots ,i_{m}=j_{m}} . This means that each summand corresponds to exactly one word in S m {\displaystyle S^{m}} . This allows us to rewrite the equation to
where q ℓ {\displaystyle q_{\ell }} is the number of codewords in S m {\displaystyle S^{m}} of length ℓ {\displaystyle \ell } and ℓ m a x {\displaystyle \ell _{max}} is the length of the longest codeword in S {\displaystyle S} . For an r {\displaystyle r} -letter alphabet there are only r ℓ {\displaystyle r^{\ell }} possible words of length ℓ {\displaystyle \ell } , so q ℓ ≤ r ℓ {\displaystyle q_{\ell }\leq r^{\ell }} . Using this, we upper bound C m {\displaystyle C^{m}} :
Taking the m {\displaystyle m} -th root, we get
This bound holds for any m ∈ N {\displaystyle m\in \mathbb {N} } . The right side is 1 asymptotically, so ∑ i = 1 n r − l i ≤ 1 {\displaystyle \sum _{i=1}^{n}r^{-l_{i}}\leq 1} must hold (otherwise the inequality would be broken for a large enough m {\displaystyle m} ).
Given a sequence of n {\displaystyle n} natural numbers,
satisfying the Kraft inequality, we can construct a prefix code as follows. Define the ith codeword, Ci, to be the first ℓ i {\displaystyle \ell _{i}} digits after the radix point (e.g. decimal point) in the base r representation of
Note that by Kraft's inequality, this sum is never more than 1. Hence the codewords capture the entire value of the sum. Therefore, for j > i, the first ℓ i {\displaystyle \ell _{i}} digits of Cj form a larger number than Ci, so the code is prefix free.
The following generalization is found in.
Theorem—If C , D {\textstyle C,D} are uniquely decodable, and every codeword in C {\textstyle C} is a concatenation of codewords in D {\textstyle D} , then ∑ c ∈ C r − | c | ≤ ∑ c ∈ D r − | c | {\displaystyle \sum _{c\in C}r^{-|c|}\leq \sum _{c\in D}r^{-|c|}}
The previous theorem is the special case when D = { a 1 , … , a r } {\displaystyle D=\{a_{1},\dots ,a_{r}\}} .
Let Q C ( x ) {\textstyle Q_{C}(x)} be the generating function for the code. That is, Q C ( x ) := ∑ c ∈ C x | c | {\displaystyle Q_{C}(x):=\sum _{c\in C}x^{|c|}}
By a counting argument, the k {\textstyle k} -th coefficient of Q C n {\textstyle Q_{C}^{n}} is the number of strings of length n {\textstyle n} with code length k {\textstyle k} . That is, Q C n ( x ) = ∑ k ≥ 0 x k # ( strings of length n with C -codes of length k ) {\displaystyle Q_{C}^{n}(x)=\sum _{k\geq 0}x^{k}\#({\text{strings of length }}n{\text{ with }}C{\text{-codes of length }}k)} Similarly, 1 1 − Q C ( x ) = 1 + Q C ( x ) + Q C ( x ) 2 + ⋯ = ∑ k ≥ 0 x k # ( strings with C -codes of length k ) {\displaystyle {\frac {1}{1-Q_{C}(x)}}=1+Q_{C}(x)+Q_{C}(x)^{2}+\cdots =\sum _{k\geq 0}x^{k}\#({\text{strings with }}C{\text{-codes of length }}k)}
Since the code is uniquely decodable, any power of Q C {\textstyle Q_{C}} is absolutely bounded by r | x | + r 2 | x | 2 + ⋯ = r | x | 1 − r | x | {\textstyle r|x|+r^{2}|x|^{2}+\cdots ={\frac {r|x|}{1-r|x|}}} , so each of Q C , Q C 2 , … {\textstyle Q_{C},Q_{C}^{2},\dots } and 1 1 − Q C ( x ) {\textstyle {\frac {1}{1-Q_{C}(x)}}} is analytic in the disk | x | < 1 / r {\textstyle |x|<1/r} .
We claim that for all x ∈ ( 0 , 1 / r ) {\textstyle x\in (0,1/r)} , Q C n ≤ Q D n + Q D n + 1 + ⋯ {\displaystyle Q_{C}^{n}\leq Q_{D}^{n}+Q_{D}^{n+1}+\cdots }
The left side is ∑ k ≥ 0 x k # ( strings of length n with C -codes of length k ) {\displaystyle \sum _{k\geq 0}x^{k}\#({\text{strings of length }}n{\text{ with }}C{\text{-codes of length }}k)} and the right side is
∑ k ≥ 0 x k # ( strings of length ≥ n with D -codes of length k ) {\displaystyle \sum _{k\geq 0}x^{k}\#({\text{strings of length}}\geq n{\text{ with }}D{\text{-codes of length }}k)}
Now, since every codeword in C {\textstyle C} is a concatenation of codewords in D {\textstyle D} , and D {\textstyle D} is uniquely decodable, each string of length n {\textstyle n} with C {\textstyle C} -code c 1 … c n {\textstyle c_{1}\dots c_{n}} of length k {\textstyle k} corresponds to a unique string s c 1 … s c n {\textstyle s_{c_{1}}\dots s_{c_{n}}} whose D {\textstyle D} -code is c 1 … c n {\textstyle c_{1}\dots c_{n}} . The string has length at least n {\textstyle n} .
Therefore, the coefficients on the left are less or equal to the coefficients on the right.
Thus, for all x ∈ ( 0 , 1 / r ) {\textstyle x\in (0,1/r)} , and all n = 1 , 2 , … {\textstyle n=1,2,\dots } , we have Q C ≤ Q D ( 1 − Q D ) 1 / n {\displaystyle Q_{C}\leq {\frac {Q_{D}}{(1-Q_{D})^{1/n}}}} Taking n → ∞ {\textstyle n\to \infty } limit, we have Q C ( x ) ≤ Q D ( x ) {\textstyle Q_{C}(x)\leq Q_{D}(x)} for all x ∈ ( 0 , 1 / r ) {\textstyle x\in (0,1/r)} .
Since Q C ( 1 / r ) {\textstyle Q_{C}(1/r)} and Q D ( 1 / r ) {\textstyle Q_{D}(1/r)} both converge, we have Q C ( 1 / r ) ≤ Q D ( 1 / r ) {\textstyle Q_{C}(1/r)\leq Q_{D}(1/r)} by taking the limit and applying Abel's theorem.
There is a generalization to quantum code.
Chaitin's constant
Canonical Huffman code
