Puneet Varma (Editor)

Kraft–McMillan inequality

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit
Kraft–McMillan inequality

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

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:

  • 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 , , s n }

    be encoded into a uniquely decodable code over an alphabet of size r with codeword lengths

    1 , 2 , , n .

    Then

    i = 1 n r i 1.

    Conversely, for a given set of natural numbers 1 , 2 , , 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

    leaves 2 depth ( ) 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

    1 4 + 4 ( 1 8 ) = 3 4 1.

    Proof for prefix codes

    First, let us show that the Kraft inequality holds whenever S is a prefix code.

    Suppose that 1 2 n . Let A be the full r -ary tree of depth n (thus, every node of A at level < n has r children, while the nodes at level n are leaves). Every word of length n over an r -ary alphabet corresponds to a node in this tree at depth . 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 n ) in the subtree of A rooted at v i . That subtree being of height n i , we have

    | A i | = r n i .

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

    A i A j = , i j .

    Thus, given that the total number of nodes at depth n is r n , we have

    | i = 1 n A i | = i = 1 n | A i | = i = 1 n r n i r n

    from which the result follows.

    Conversely, given any ordered sequence of n natural numbers,

    1 2 n

    satisfying the Kraft inequality, one can construct a prefix code with codeword lengths equal to each i by choosing a word of length 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 n . First choose any node from the full tree at depth 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 (i.e., the leaf nodes among the descendants); there are r n 1 such descendant nodes that are removed from consideration. The next iteration picks a (surviving) node at depth 2 and removes r n 2 further leaf nodes, and so on. After n iterations, we have removed a total of

    i = 1 n r n i

    nodes. The question is whether we need to remove more leaf nodes than we actually have available — r n in all — in the process of building the code. Since the Kraft inequality holds, we have indeed

    i = 1 n r n i r 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.)

    Consider the generating function in inverse of x for the code S

    F ( x ) = i = 1 n x | s i | = = min max p x

    in which p —the coefficient in front of x —is the number of distinct codewords of length . Here min is the length of the shortest codeword in S, and max is the length of the longest codeword in S.

    Consider all m-powers Sm, in the form of words s i 1 s i 2 s i m , where i 1 , i 2 , , i m are indices between 1 and 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 implies i 1 = j 1 , i 2 = j 2 , , i m = j m . Because of this property, one can compute the generating function G ( x ) for S m from the generating function F ( x ) as

    G ( x ) = ( F ( x ) ) m = ( i = 1 n x | s i | ) m = i 1 = 1 n i 2 = 1 n i m = 1 n x ( | s i 1 | + | s i 2 | + + | s i m | ) = i 1 = 1 n i 2 = 1 n i m = 1 n x | s i 1 s i 2 s i m | = = m min m max q x .

    Here, similarly as before, q — the coefficient in front of x in G ( x ) — is the number of words of length in S m . Clearly, q cannot exceed r . Hence for any positive x,

    ( F ( x ) ) m = m min m max r x .

    Substituting the value x = r we have

    ( F ( r ) ) m m ( max min ) + 1

    for any positive integer m . The left side of the inequality grows exponentially in m and the right side only linearly. The only possibility for the inequality to be valid for all m is that F ( r ) 1 . Looking back on the definition of F ( x ) we finally get the inequality.

    i = 1 n r i = i = 1 n r | s i | = F ( r ) 1 .

    Alternative construction for the converse

    Given a sequence of n natural numbers,

    1 2 n

    satisfying the Kraft inequality, we can construct a prefix code as follows. Define the ith codeword, Ci, to be the first i digits after the radix point (e.g. decimal point) in the base r representation of

    j = 1 i 1 r 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 i digits of Cj form a larger number than Ci, so the code is prefix free.

    References

    Kraft–McMillan inequality Wikipedia