The Kraft-McMillan Inequality

The Kraft-McMillan inequality is a foundational result in information theory that establishes a crucial constraint on the lengths of codewords in uniquely decodable codes. Developed independently by Leon Kraft in 1949 and Brockway McMillan in 1956, the inequality is essential for understanding how information can be efficiently encoded while ensuring that no ambiguities arise during decoding.

In essence, the Kraft-McMillan inequality provides a mathematical condition that must be satisfied by any prefix-free or uniquely decodable code. A code is prefix-free if no codeword is a prefix of another, and it is uniquely decodable if encoded messages can be uniquely reconstructed without ambiguity. The inequality states that for a code over an alphabet of size DD, the sum of DliD^{-l_i} (where lil_i is the length of the ii-th codeword) must not exceed 1.

This result has profound implications for data compression, enabling the design of optimal coding schemes, such as Huffman codes, which achieve the minimum average codeword length for a given probability distribution. The Kraft-McMillan inequality also forms the theoretical foundation for more advanced topics like entropy, channel coding, and the efficiency of communication systems.

Understanding this inequality is not just critical for theoretical insights but also for practical applications in areas such as file compression, error correction, and telecommunications.

1. Introduction

Efficient encoding of information relies on the ability to assign codewords to messages in a manner that ensures unique decodability. Uniquely decodable codes are a broader class of codes where no sequence of codewords can be confused with another. Within this class lies prefix codes, a subset where no codeword is a prefix of another.

The Kraft-McMillan inequality provides:

  1. A necessary condition for the lengths of codewords in uniquely decodable codes.
  2. A constructive proof that any set of lengths satisfying the inequality can always form a prefix code.

2. The Kraft-McMillan Inequality

Theorem

Let C\mathcal{C} be a code with NN codewords, each of length l1,l2,,lNl_1, l_2, \ldots, l_N. If C\mathcal{C} is uniquely decodable, then:

K(C)=i=1N2li1K(\mathcal{C}) = \sum_{i=1}^N 2^{-l_i} \leq 1

Interpretation

  • Necessary Condition: For any uniquely decodable code, the inequality must hold.
  • Sufficient Condition: If the inequality is satisfied, a prefix code can always be constructed with the same codeword lengths.

3. Proof of Necessity

Key Idea

If K(C)>1K(\mathcal{C}) > 1, the sum K(C)nK(\mathcal{C})^n grows exponentially with nn. However, the growth rate of the combined lengths of nn-codeword sequences must remain bounded by the structure of the encoding tree.

Proof

  1. Expression for K(C)nK(\mathcal{C})^n:
K(C)n=(i=1N2li)nK(\mathcal{C})^n = \left( \sum_{i=1}^N 2^{-l_i} \right)^n

Expanding the nn-fold product:

K(C)n=i1=1Ni2=1Nin=1N2(li1+li2++lin)K(\mathcal{C})^n = \sum_{i_1=1}^N \sum_{i_2=1}^N \cdots \sum_{i_n=1}^N 2^{-(l_{i_1} + l_{i_2} + \cdots + l_{i_n})}

Here, li1+li2++linl_{i_1} + l_{i_2} + \cdots + l_{i_n} represents the total length of an nn-codeword sequence.

  1. Bounds on Total Lengths:

    • The smallest total length is nn, achievable if every codeword length is 1.
    • The largest total length is nlnl, where l=max(l1,l2,,lN)l = \max(l_1, l_2, \ldots, l_N).

    Therefore, the sum can be expressed as:

K(C)n=k=nnlAk2kK(\mathcal{C})^n = \sum_{k=n}^{nl} A_k 2^{-k}

where AkA_k is the number of combinations of nn-codewords whose total length equals kk.

  1. Bound on AkA_k: The number of distinct binary sequences of length kk is 2k2^k. If the code is uniquely decodable, no two sequences of codewords can map to the same binary sequence. Hence:
Ak2kA_k \leq 2^k
  1. Simplification: Using Ak2kA_k \leq 2^k:
K(C)nk=nnl2k2k=nln+1K(\mathcal{C})^n \leq \sum_{k=n}^{nl} 2^k \cdot 2^{-k} = nl – n + 1
  1. Growth Contradiction: If K(C)>1K(\mathcal{C}) > 1, then K(C)nK(\mathcal{C})^n grows exponentially with nn, while nln+1nl – n + 1 grows linearly. For sufficiently large nn, this leads to a contradiction.

    Therefore, K(C)1K(\mathcal{C}) \leq 1 for any uniquely decodable code.

4. Proof of Sufficiency

Theorem

Given a set of integers l1,l2,,lNl_1, l_2, \ldots, l_N satisfying:

i=1N2li1\sum_{i=1}^N 2^{-l_i} \leq 1

a prefix code can always be constructed with these lengths.

Proof by Construction

  1. Assume Sorted Lengths: Without loss of generality, let l1l2lNl_1 \leq l_2 \leq \cdots \leq l_N.

  2. Construct Codewords: Define a sequence w1,w2,,wNw_1, w_2, \ldots, w_N as follows:

    • w1=0w_1 = 0
    • wj=i=1j12ljli,j>1w_j = \sum_{i=1}^{j-1} 2^{l_j – l_i}, \, j > 1
  3. Binary Representation:

    • Convert each wjw_j to its binary representation.
    • Pad with zeros to ensure the length is ljl_j.
  4. Prefix Property: To prove the prefix property, consider any j<kj < k. By construction, wjw_j and wkw_k are distinct, and wkw_k cannot start with the bits of wjw_j. Therefore, the code is a prefix code.

5. Applications

5.1 Data Compression

The inequality underpins optimal compression techniques like Huffman coding, ensuring that the codewords generated are both efficient and decodable.

5.2 Uniquely Decodable Codes

The inequality provides a bridge between uniquely decodable codes and prefix codes, showing that focusing on prefix codes does not limit the design space of uniquely decodable codes.

6. Python Implementation

python
import math def verify_kraft_mcmillan(lengths): """Verify the Kraft-McMillan inequality.""" kraft_sum = sum(2**-l for l in lengths) return kraft_sum, kraft_sum <= 1 def construct_prefix_code(lengths): """Construct a prefix code with given lengths.""" lengths = sorted(lengths) w = [] prefix_code = [] current = 0 for l in lengths: w.append(current) code = format(current, f'0{l}b') prefix_code.append(code) current += 1 << (max(lengths) - l) return prefix_code # Example code_lengths = [2, 3, 3, 4] kraft_sum, is_valid = verify_kraft_mcmillan(code_lengths) print(f"Kraft-McMillan Sum: {kraft_sum}") print(f"Inequality Valid: {is_valid}") if is_valid: prefix_code = construct_prefix_code(code_lengths) print("Prefix Code:", prefix_code) else: print("Cannot construct prefix code.")

References

  • “Elements of Information Theory” by Thomas M. Cover and Joy A. Thomas, which provides a comprehensive explanation of the Kraft-McMillan inequality and its applications.
  • McMillan, B. (1956). Two inequalities implied by unique decipherability, introducing the mathematical foundation of the inequality.
  • Kraft, L.G. (1949). A Device for Quantizing, Grouping, and Coding Amplitude Modulated Pulses, which formalized the original Kraft inequality.
  • “Introduction to Coding Theory” by J.H. van Lint, discussing the mathematical basis of uniquely decodable codes and prefix codes.
  • MIT OpenCourseWare on Information Theory (https://ocw.mit.edu), which offers free lectures and notes on the topic.
  • NPTEL Online Course on Information Theory and Coding (https://nptel.ac.in), which includes detailed discussions and examples of the Kraft-McMillan inequality.

Leave a Comment