Take a code alphabet of size and prescribed positive integer lengths . A decipherable code means that concatenation of codewords determines the entire finite sequence of codewords uniquely. First prove the necessary Kraft inequality. If counts sequences of codewords with total length , unique decodability gives . Therefore
Taking th roots and letting gives .
Conversely, order the lengths increasingly and construct a prefix code in the rooted -ary tree. At depth , previously selected codewords forbid exactly vertices. The partial Kraft inequality implies
so an available vertex exists. Select it as the next codeword. Earlier lengths are no greater, so this preserves the prefix code property. A prefix code has unique decodability: in two purported parsings, the first unequal codewords would force one to be a prefix of the other. This proves the equivalence of decipherable and prefix code lengths: the two existence conditions are equivalent.
For a one-symbol alphabet, at most one positive-length codeword can be decipherable: two different unary codewords commute under concatenation. At most one is also exactly the prefix-code condition, so the equivalence still holds. The same argument covers countably many prescribed positive lengths for : necessity applies to each finite subset, and ensures finitely many words at each length, allowing the ordered construction. Empty codewords are excluded, as required for unique decodability of arbitrary sequences.
Let a memoryless source have probabilities , and let a uniquely decodable -ary code have word lengths and mean length . The Shannon source coding theorem states
and that some prefix code satisfies
For the lower bound, the Kraft inequality gives . Set . The Gibbs inequality, or nonnegativity of relative entropy, gives
Therefore .
For achievability choose . Then , so the converse part of the Kraft theorem supplies a prefix code with these lengths, and
Hence the entropy is the optimal mean code length up to less than one code symbol.
The binary Huffman coding algorithm repeatedly merges the two currently least probable symbols into one meta-symbol whose probability is their sum. After recursively constructing a code for the smaller alphabet, append and to the meta-symbol's codeword to obtain codewords for the merged pair. The result is a binary prefix code.
To prove optimality, begin with an optimal prefix-code tree. It may be taken to be full, since an internal vertex with one child can be suppressed and all codewords below it shortened. Two leaves of maximum depth are siblings. By exchanging labels, without changing lengths, assign two least probable symbols to such a sibling pair: moving a smaller probability to a no-shorter codeword cannot increase the expected length. Contracting this pair to one leaf replaces their contribution by plus the constant . If the contracted code were not optimal for the reduced alphabet, replacing it by a better one and then expanding the merged leaf would improve the original code. Mathematical induction on the number of symbols now proves that the Huffman construction minimizes expected word length.
In Shannon-Fano coding, a symbol of probability is assigned length
and codewords of those lengths are chosen in lexicographic probability intervals; the Kraft inequality guarantees that a binary prefix code exists. Huffman coding repeatedly merges the two least probable current symbols, builds a binary tree from the resulting merges, and labels its two branches by zero and one. Reading from the root to each leaf gives an optimal prefix code.
The binary Kraft inequality states that codeword lengths of a prefix code, or more generally a uniquely decodable code, satisfy
Conversely, positive integer lengths obeying this inequality can be realized by a binary prefix code.
Take every logarithm in base two. The information entropy of is
with . If the codeword has length , then its expected codeword length is
For any decipherable code, the Kraft inequality gives
Define the probability distribution . By Gibbs inequality,
Since ,
Conversely, choose the integer lengths
Then , so their Kraft sum is at most one and the converse part of Kraft's inequality supplies a binary prefix code. Moreover,
Thus the minimum expected length satisfies the Shannon noiseless coding theorem
For a decipherable code with arbitrary lengths , apply the lower bound to the uniform source . Its entropy is and its expected length is . Hence
the total length lower bound for a decipherable binary code.
Now consider the cumulative construction. Since
and cannot have the same first binary digits: numbers sharing those digits lie in one half-open dyadic interval of length . Also, implies . Therefore no earlier codeword is a prefix of a later one, and no later, weakly longer codeword can be a prefix of an earlier one. The cumulative Shannon code is thus a prefix code, hence decipherable.
An optimal code minimizes expected codeword length over all decipherable codes for the specified source probabilities. The cumulative construction need not be optimal. For example, if
then and the construction gives codewords and , with expected length . The prefix code has expected length , so the constructed code is not optimal. In general, Huffman coding produces an optimal prefix code.
The binary Kraft inequality says that codeword lengths of a prefix code satisfy
Conversely, suppose positive integer lengths obey this inequality and arrange them in nondecreasing order. Construct codewords greedily in the infinite binary tree. Before assigning length , each earlier codeword of length excludes exactly nodes at depth . Thus the number excluded is
where strictness follows because the remaining term occurs in the full Kraft sum. A free depth- node therefore exists. Assign it as the next codeword; choosing a node not below an earlier codeword preserves prefix-freeness. Induction constructs the required prefix code.
For a -ary code with codeword lengths , Kraft inequality is
Choose an infinite random -ary string with independent uniform symbols. The event that it begins with codeword has probability . For a prefix-free code, these events are disjoint, so their probabilities sum to at most one. This proves necessity.
If one comma codeword were a prefix of another, its terminal comma would occur inside the longer codeword, contrary to the definition. Thus every comma code is prefix-free. Directly, the disjoint cylinder sets of infinite strings beginning with the respective comma-terminated words have measures , so their total measure gives Kraft's inequality.
Kraft's inequality does not imply unique decipherability. Over the binary alphabet,
has Kraft sum , but
Thus it is not uniquely decipherable.
A binary code is a prefix code when no codeword is a proper prefix of another. If the source letters have probabilities and codeword lengths , the expected value of its codeword length is
It is an optimal prefix code when it minimizes among all binary prefix codes for that source.
Suppose but . Exchanging the codewords assigned to and leaves the set of words, and hence prefix-freeness, unchanged. The change in expected length is
contradicting optimality. Thus
which is the probability monotonicity of optimal prefix-code lengths.
Represent the code by its binary prefix tree and choose a codeword of maximal length, where . Its sibling is . If that sibling were not a codeword, it could not have a codeword below it either, since such a word would be longer than . Replacing by would then preserve prefix-freeness and strictly reduce . This is impossible for an optimal code, so is also a maximal-length codeword. The pair differs only in its last digit, proving the deepest sibling property of an optimal prefix code.
The direct codes-distributions correspondence says that every mass function on a finite alphabet admits a binary prefix code with lengths . Conversely, every binary prefix code has lengths satisfying the Kraft inequality , and hence defines the mass function .
For a binary prefix code with word lengths , choose . Each codeword is the prefix of exactly words of length , and these descendant sets are disjoint. Hence
This is Kraft inequality. Conversely, if integer lengths satisfy this inequality, place words greedily as leaves of the binary tree; the unused capacity ensures that the requested leaves can all be chosen, giving a prefix code.
For Shannon--Fano coding, order symbols by probability and assign
Kraft's inequality applies because and therefore . Thus codewords of those lengths exist. Since
the expected length obeys
Shannon-Fano coding Created 2026-09-24 Updated 2026-09-24