![]() | ||
In coding theory, the Kraft–McMillan inequality gives a necessary and sufficient condition for the existence of a prefix code (in Kraft's version) or a uniquely decodable code (in 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.
Contents
- Applications and intuitions
- Formal statement
- Example binary trees
- Proof for prefix codes
- Proof of the general case
- Alternative construction for the converse
- References
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 of the result 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:
Formal statement
Let each source symbol from the alphabet
be encoded into a uniquely decodable code over an alphabet of size
Then
Conversely, for a given set of natural numbers
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
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
Proof for prefix codes
First, let us show that the Kraft inequality holds whenever
Suppose that
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
from which the result follows.
Conversely, given any ordered sequence of
satisfying the Kraft inequality, one can construct a prefix code with codeword lengths equal to each
nodes. The question is whether we need to remove more leaf nodes than we actually have available —
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
Consider the generating function in inverse of x for the code S
in which
Consider all m-powers Sm, in the form of words
Here, similarly as before,
Substituting the value x = r we have
for any positive integer
Alternative construction for the converse
Given a sequence of
satisfying the Kraft inequality, we can construct a prefix code as follows. Define the ith codeword, Ci, to be the first
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