# Kraft–McMillan inequality

(Redirected from Kraft inequality)

In coding theory, the Kraft–McMillan inequality gives a necessary and sufficient condition for the existence of a prefix code[1] (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.[2]
• 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

${\displaystyle S=\{\,s_{1},s_{2},\ldots ,s_{n}\,\}}$

be encoded into a uniquely decodable code over an alphabet of size ${\displaystyle r}$ with codeword lengths

${\displaystyle \ell _{1},\ell _{2},\ldots ,\ell _{n}.}$

Then

${\displaystyle \sum _{i=1}^{n}r^{-\ell _{i}}\leqslant 1.}$

Conversely, for a given set of natural numbers ${\displaystyle \ell _{1},\ell _{2},\ldots ,\ell _{n}}$ satisfying the above inequality, there exists a uniquely decodable code over an alphabet of size ${\displaystyle 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

${\displaystyle \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

${\displaystyle {\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 ${\displaystyle S}$ is a prefix code.

Suppose that ${\displaystyle \ell _{1}\leqslant \ell _{2}\leqslant \cdots \leqslant \ell _{n}}$. Let ${\displaystyle A}$ be the full ${\displaystyle r}$-ary tree of depth ${\displaystyle \ell _{n}}$ (thus, every node of ${\displaystyle A}$ at level ${\displaystyle <\ell _{n}}$ has ${\displaystyle r}$ children, while the nodes at level ${\displaystyle \ell _{n}}$ are leaves). Every word of length ${\displaystyle \ell \leqslant \ell _{n}}$ over an ${\displaystyle r}$-ary alphabet corresponds to a node in this tree at depth ${\displaystyle \ell }$. The ${\displaystyle i}$th word in the prefix code corresponds to a node ${\displaystyle v_{i}}$; let ${\displaystyle A_{i}}$ be the set of all leaf nodes (i.e. of nodes at depth ${\displaystyle \ell _{n}}$) in the subtree of ${\displaystyle A}$ rooted at ${\displaystyle v_{i}}$. That subtree being of height ${\displaystyle \ell _{n}-\ell _{i}}$, we have

${\displaystyle |A_{i}|=r^{\ell _{n}-\ell _{i}}.}$

Since the code is a prefix code, those subtrees cannot share any leaves, which means that

${\displaystyle A_{i}\cap A_{j}=\varnothing ,\quad i\neq j.}$

Thus, given that the total number of nodes at depth ${\displaystyle \ell _{n}}$ is ${\displaystyle r^{\ell _{n}}}$, we have

${\displaystyle \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 ${\displaystyle n}$ natural numbers,

${\displaystyle \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 ${\displaystyle \ell _{i}}$ by choosing a word of length ${\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 ${\displaystyle r}$-ary tree of depth ${\displaystyle \ell _{n}}$. First choose any node from the full tree at depth ${\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 ${\displaystyle \ell _{n}}$ (i.e., the leaf nodes among the descendants); there are ${\displaystyle r^{\ell _{n}-\ell _{1}}}$ such descendant nodes that are removed from consideration. The next iteration picks a (surviving) node at depth ${\displaystyle \ell _{2}}$ and removes ${\displaystyle r^{\ell _{n}-\ell _{2}}}$ further leaf nodes, and so on. After ${\displaystyle n}$ iterations, we have removed a total of

${\displaystyle \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 — ${\displaystyle r^{\ell _{n}}}$ in all — in the process of building the code. Since the Kraft inequality holds, we have indeed

${\displaystyle \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 ${\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.[3][4]

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 ${\displaystyle C=\sum _{i=1}^{n}r^{-l_{i}}}$. The idea of the proof is to get an upper bound on ${\displaystyle C^{m}}$ for ${\displaystyle m\in \mathbb {N} }$ and show that it can only hold for all ${\displaystyle m}$ if ${\displaystyle C\leq 1}$. Rewrite ${\displaystyle C^{m}}$ as

{\displaystyle {\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 ${\displaystyle S^{m}}$, in the form of words ${\displaystyle s_{i_{1}}s_{i_{2}}\dots s_{i_{m}}}$, where ${\displaystyle i_{1},i_{2},\dots ,i_{m}}$ are indices between 1 and ${\displaystyle n}$. Note that, since S was assumed to uniquely decodable, ${\displaystyle s_{i_{1}}s_{i_{2}}\dots s_{i_{m}}=s_{j_{1}}s_{j_{2}}\dots s_{j_{m}}}$ implies ${\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 ${\displaystyle S^{m}}$. This allows us to rewrite the equation to

${\displaystyle C^{m}=\sum _{\ell =1}^{m\cdot \ell _{max}}q_{\ell }\,r^{-\ell }}$

where ${\displaystyle q_{\ell }}$ is the number of codewords in ${\displaystyle S^{m}}$ of length ${\displaystyle \ell }$ and ${\displaystyle \ell _{max}}$ is the length of the longest codeword in ${\displaystyle S}$. For an ${\displaystyle r}$-letter alphabet there are only ${\displaystyle r^{\ell }}$ possible words of length ${\displaystyle \ell }$, so ${\displaystyle q_{\ell }\leq r^{\ell }}$. Using this, we upper bound ${\displaystyle C^{m}}$:

{\displaystyle {\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 ${\displaystyle m}$-th root, we get

${\displaystyle C=\sum _{i=1}^{n}r^{-l_{i}}\leq \left(m\cdot \ell _{max}\right)^{\frac {1}{m}}}$

This bound holds for any ${\displaystyle m\in \mathbb {N} }$. The right side is 1 asymptotically, so ${\displaystyle \sum _{i=1}^{n}r^{-l_{i}}\leq 1}$ must hold (otherwise the inequality would be broken for a large enough ${\displaystyle m}$).

### Alternative construction for the converse

Given a sequence of ${\displaystyle n}$ natural numbers,

${\displaystyle \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, Ci, to be the first ${\displaystyle \ell _{i}}$ digits after the radix point (e.g. decimal point) in the base r representation of

${\displaystyle \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 ${\displaystyle \ell _{i}}$ digits of Cj form a larger number than Ci, so the code is prefix free.

## Generalizations

The following generalization is found in.[5]

Theorem — If ${\textstyle C,D}$ are uniquely decodable, and every codeword in ${\textstyle C}$ is a concatenation of codewords in ${\textstyle D}$, then ${\displaystyle \sum _{c\in C}r^{-|c|}\leq \sum _{c\in D}r^{-|c|}}$

The previous theorem is the special case when ${\displaystyle D=\{a_{1},\dots ,a_{r}\}}$.

Proof

Let ${\textstyle Q_{C}(x)}$ be the generating function for the code. That is, ${\displaystyle Q_{C}(x):=\sum _{c\in C}x^{|c|}}$

By a counting argument, the ${\textstyle k}$-th coefficient of ${\textstyle Q_{C}^{n}}$ is the number of strings of length ${\textstyle n}$ with code length ${\textstyle k}$. That is, ${\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,
${\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 ${\textstyle Q_{C}}$ is absolutely bounded by ${\textstyle r|x|+r^{2}|x|^{2}+\cdots ={\frac {r|x|}{1-r|x|}}}$, so each of ${\textstyle Q_{C},Q_{C}^{2},\dots }$ and ${\textstyle {\frac {1}{1-Q_{C}(x)}}}$ is analytic in the disk ${\textstyle |x|<1/r}$.

We claim that for all ${\textstyle x\in (0,1/r)}$, ${\displaystyle Q_{C}^{n}\leq Q_{D}^{n}+Q_{D}^{n+1}+\cdots }$

The left side is ${\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

${\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 ${\textstyle C}$ is a concatenation of codewords in ${\textstyle D}$, and ${\textstyle D}$ is uniquely decodable, each string of length ${\textstyle n}$ with ${\textstyle C}$-code ${\textstyle c_{1}\dots c_{n}}$ of length ${\textstyle k}$ corresponds to a unique string ${\textstyle s_{c_{1}}\dots s_{c_{n}}}$ whose ${\textstyle D}$-code is ${\textstyle c_{1}\dots c_{n}}$. The string has length at least ${\textstyle n}$.

Therefore, the coefficients on the left are less or equal to the coefficients on the right.

Thus, for all ${\textstyle x\in (0,1/r)}$, and all ${\textstyle n=1,2,\dots }$, we have ${\displaystyle Q_{C}\leq {\frac {Q_{D}}{(1-Q_{D})^{1/n}}}}$ Taking ${\textstyle n\to \infty }$ limit, we have ${\textstyle Q_{C}(x)\leq Q_{D}(x)}$ for all ${\textstyle x\in (0,1/r)}$.

Since ${\textstyle Q_{C}(1/r)}$ and ${\textstyle Q_{D}(1/r)}$ both converge, we have ${\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.[6]

## Notes

1. ^ Cover, Thomas M.; Thomas, Joy A. (2006), "Data Compression", Elements of Information Theory (2nd ed.), John Wiley & Sons, Inc, pp. 108–109, doi:10.1002/047174882X.ch5, ISBN 978-0-471-24195-9
2. ^ De Rooij, Steven; Grünwald, Peter D. (2011), "LUCKINESS AND REGRET IN MINIMUM DESCRIPTION LENGTH INFERENCE", Philosophy of Statistics (1st ed.), Elsevier, p. 875, ISBN 978-0-080-93096-1
3. ^ Karush, J. (April 1961). "A simple proof of an inequality of McMillan (Corresp.)". IEEE Transactions on Information Theory. 7 (2): 118. doi:10.1109/TIT.1961.1057625. ISSN 0018-9448.
4. ^ Cover, Thomas M.; Thomas, Joy A. (2006). Elements of information theory (2nd ed.). Hoboken, N.J: Wiley-Interscience. ISBN 978-0-471-24195-9.
5. ^ Foldes, Stephan (2008-06-21). "On McMillan's theorem about uniquely decipherable codes". arXiv:0806.3277 [math.CO].
6. ^ Schumacher, Benjamin; Westmoreland, Michael D. (2001-09-10). "Indeterminate-length quantum coding". Physical Review A. 64 (4): 042304. arXiv:quant-ph/0011014. Bibcode:2001PhRvA..64d2304S. doi:10.1103/PhysRevA.64.042304. S2CID 53488312.

## References

• McMillan, Brockway (1956), "Two inequalities implied by unique decipherability", IEEE Trans. Inf. Theory, 2 (4): 115–116, doi:10.1109/TIT.1956.1056818.