Kraft–McMillan inequality

From testwiki
Jump to navigation Jump to search

Template:Short description 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 Template:Harvtxt. 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 Template:Harvtxt. 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

S={s1,s2,…,sn}

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

ℓ1,ℓ2,…,ℓn.

Then

∑i=1nr−ℓ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

9, 14, 19, 67 and 76 are leaf nodes at depths of 3, 3, 3, 3 and 2, respectively.

Any binary tree can be viewed as defining a prefix code for the leaves of the tree. Kraft's inequality states that

∑ℓ∈leaves2−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

14+4(18)=34⩽1.

Proof

Proof for prefix codes

Example for binary tree. Red nodes represent a prefix tree. The method for calculating the number of descendant leaf nodes in the full tree is shown.

First, let us show that the Kraft inequality holds whenever the code for 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 ith word in the prefix code corresponds to a node vi; let Ai be the set of all leaf nodes (i.e. of nodes at depth ℓn) in the subtree of A rooted at vi. That subtree being of height ℓn−ℓi, we have

|Ai|=rℓn−ℓi.

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

Ai∩Aj=∅,i≠j.

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

|⋃i=1nAi|=∑i=1n|Ai|=∑i=1nrℓ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=1nrℓ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=1nrℓ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.) 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 C=∑i=1nr−li. The idea of the proof is to get an upper bound on Cm for m∈ℕ and show that it can only hold for all m if C≤1. Rewrite Cm as

Cm=(∑i=1nr−li)m=∑i1=1n∑i2=1n⋯∑im=1nr−(li1+li2+⋯+lim)

Consider all m-powers Sm, in the form of words si1si2…sim, where i1,i2,…,im are indices between 1 and n. Note that, since S was assumed to uniquely decodable, si1si2…sim=sj1sj2…sjm implies i1=j1,i2=j2,…,im=jm. This means that each summand corresponds to exactly one word in Sm. This allows us to rewrite the equation to

Cm=∑ℓ=1m⋅ℓmaxqℓr−ℓ

where qℓ is the number of codewords in Sm of length ℓ and ℓmax is the length of the longest codeword in S. For an r-letter alphabet there are only rℓ possible words of length ℓ, so qℓ≤rℓ. Using this, we upper bound Cm:

Cm=∑ℓ=1m⋅ℓmaxqℓr−ℓ≤∑ℓ=1m⋅ℓmaxrℓr−ℓ=m⋅ℓmax

Taking the m-th root, we get

C=∑i=1nr−li≤(m⋅ℓmax)1m

This bound holds for any m∈ℕ. The right side is 1 asymptotically, so ∑i=1nr−li≤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,

ℓ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=1i−1r−ℓ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.

Generalizations

The following generalization is found in.[5]

Template:Math theoremThe previous theorem is the special case when D={a1,…,ar}.Template:Math proofThere is a generalization to quantum code.[6]

Notes

Template:Reflist

References

See also

  1. ↑ Cite error: Invalid <ref> tag; no text was provided for refs named "EIT".
  2. ↑ Cite error: Invalid <ref> tag; no text was provided for refs named "de2011luckiness".
  3. ↑ Template:Cite journal
  4. ↑ Template:Cite book
  5. ↑ Template:Cite arXiv
  6. ↑ Template:Cite journal