# Kraft–McMillan inequality

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.

## Applications and intuitions

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.

## Formal statement

Let each source symbol from the alphabet
    $S=\{\,s_{1},s_{2},\ldots ,s_{n}\,\}$
be encoded into a uniquely decodable code over an alphabet of size $r$ with codeword lengths
    $\ell _{1},\ell _{2},\ldots ,\ell _{n}.$
Then
    $\sum _{i=1}^{n}r^{-\ell _{i}}\leqslant 1.$
Conversely, for a given set of natural numbers $\ell _{1},\ell _{2},\ldots ,\ell _{n}$ satisfying the above inequality, there exists a uniquely decodable code over an alphabet of size $r$ with those codeword lengths.

## Example: binary trees

Any binary tree can be viewed as defining a prefix code for the leaves of the tree. Kraft's inequality states that
    $\sum _{\ell \in {\text{leaves}}}2^{-{\text{depth}}(\ell )}\leqslant 1.$
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
    ${\frac {1}{4}}+4\left({\frac {1}{8}}\right)={\frac {3}{4}}\leqslant 1.$

## Proof
### Proof for prefix codes

First, let us show that the Kraft inequality holds whenever the code for $S$ is a prefix code.
Suppose that $\ell _{1}\leqslant \ell _{2}\leqslant \cdots \leqslant \ell _{n}$. Let $A$ be the full $r$-ary tree of depth $\ell _{n}$ (thus, every node of $A$ at level $<\ell _{n}$ has $r$ children, while the nodes at level $\ell _{n}$ are leaves). Every word of length $\ell \leqslant \ell _{n}$ over an $r$-ary alphabet corresponds to a node in this tree at depth $\ell$. The $i$th word in the prefix code corresponds to a node $v_{i}$; let $A_{i}$ be the set of all leaf nodes (i.e. of nodes at depth $\ell _{n}$) in the subtree of $A$ rooted at $v_{i}$. That subtree being of height $\ell _{n}-\ell _{i}$, we have
    $|A_{i}|=r^{\ell _{n}-\ell _{i}}.$
Since the code is a prefix code, those subtrees cannot share any leaves, which means that
    $A_{i}\cap A_{j}=\varnothing ,\quad i\neq j.$
Thus, given that the total number of nodes at depth $\ell _{n}$ is $r^{\ell _{n}}$, we have
    $\left|\bigcup _{i=1}^{n}A_{i}\right|=\sum _{i=1}^{n}|A_{i}|=\sum _{i=1}^{n}r^{\ell _{n}-\ell _{i}}\leqslant r^{\ell _{n}}$
from which the result follows.
Conversely, given any ordered sequence of $n$ natural numbers,
    $\ell _{1}\leqslant \ell _{2}\leqslant \cdots \leqslant \ell _{n}$
satisfying the Kraft inequality, one can construct a prefix code with codeword lengths equal to each $\ell _{i}$ by choosing a word of length $\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$-ary tree of depth $\ell _{n}$. First choose any node from the full tree at depth $\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 $\ell _{n}$ (i.e., the leaf nodes among the descendants); there are $r^{\ell _{n}-\ell _{1}}$ such descendant nodes that are removed from consideration. The next iteration picks a (surviving) node at depth $\ell _{2}$ and removes $r^{\ell _{n}-\ell _{2}}$ further leaf nodes, and so on. After $n$ iterations, we have removed a total of
    $\sum _{i=1}^{n}r^{\ell _{n}-\ell _{i}}$
nodes. The question is whether we need to remove more leaf nodes than we actually have available — $r^{\ell _{n}}$ in all — in the process of building the code. Since the Kraft inequality holds, we have indeed
    $\sum _{i=1}^{n}r^{\ell _{n}-\ell _{i}}\leqslant r^{\ell _{n}}$
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.

### Proof of the general case

Now we will prove that the Kraft inequality holds whenever $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=\sum _{i=1}^{n}r^{-l_{i}}$. The idea of the proof is to get an upper bound on $C^{m}$ for $m\in \mathbb {N}$ and show that it can only hold for all $m$ if $C\leq 1$. Rewrite $C^{m}$ as
    ${\begin{aligned}C^{m}&=\left(\sum _{i=1}^{n}r^{-l_{i}}\right)^{m}\\&=\sum _{i_{1}=1}^{n}\sum _{i_{2}=1}^{n}\cdots \sum _{i_{m}=1}^{n}r^{-\left(l_{i_{1}}+l_{i_{2}}+\cdots +l_{i_{m}}\right)}\\\end{aligned}}$
Consider all m-powers $S^{m}$, in the form of words $s_{i_{1}}s_{i_{2}}\dots s_{i_{m}}$, where $i_{1},i_{2},\dots ,i_{m}$ are indices between 1 and $n$. Note that, since S was assumed to uniquely decodable, $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},\dots ,i_{m}=j_{m}$. This means that each summand corresponds to exactly one word in $S^{m}$. This allows us to rewrite the equation to
    $C^{m}=\sum _{\ell =1}^{m\cdot \ell _{max}}q_{\ell }\,r^{-\ell }$
where $q_{\ell }$ is the number of codewords in $S^{m}$ of length $\ell$ and $\ell _{max}$ is the length of the longest codeword in $S$. For an $r$-letter alphabet there are only $r^{\ell }$ possible words of length $\ell$, so $q_{\ell }\leq r^{\ell }$. Using this, we upper bound $C^{m}$:
    ${\begin{aligned}C^{m}&=\sum _{\ell =1}^{m\cdot \ell _{max}}q_{\ell }\,r^{-\ell }\\&\leq \sum _{\ell =1}^{m\cdot \ell _{max}}r^{\ell }\,r^{-\ell }=m\cdot \ell _{max}\end{aligned}}$
Taking the $m$-th root, we get
    $C=\sum _{i=1}^{n}r^{-l_{i}}\leq \left(m\cdot \ell _{max}\right)^{\frac {1}{m}}$
This bound holds for any $m\in \mathbb {N}$. The right side is 1 asymptotically, so $\sum _{i=1}^{n}r^{-l_{i}}\leq 1$ must hold (otherwise the inequality would be broken for a large enough $m$).

### Alternative construction for the converse

Given a sequence of $n$ natural numbers,
    $\ell _{1}\leqslant \ell _{2}\leqslant \cdots \leqslant \ell _{n}$
satisfying the Kraft inequality, we can construct a prefix code as follows. Define the ith codeword, C$_i$, to be the first $\ell _{i}$ digits after the radix point (e.g. decimal point) in the base r representation of
    $\sum _{j=1}^{i-1}r^{-\ell _{j}}.$
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 $\ell _{i}$ digits of C$_j$ form a larger number than C$_i$, so the code is prefix free.

## Generalizations

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 $\sum _{c\in C}r^{-|c|}\leq \sum _{c\in D}r^{-|c|}$
The previous theorem is the special case when $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):=\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)=\sum _{k\geq 0}x^{k}\#({\text{strings of length }}n{\text{ with }}C{\text{-codes of length }}k)$ Similarly,
${\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}\leq Q_{D}^{n}+Q_{D}^{n+1}+\cdots$
The left side is $\sum _{k\geq 0}x^{k}\#({\text{strings of length }}n{\text{ with }}C{\text{-codes of length }}k)$ and the right side is
$\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$_{c1}$…s$_{cn}${\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}\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.