Equivalence of decipherable and prefix code lengths Created 2026-10-05 Updated 2026-10-06
Prescribed positive word lengths admit a decipherable code if and only if they admit a prefix code. Counting length- concatenations of words proves the Kraft inequality for any decipherable code; choosing free vertices of the ordered -ary tree constructs a prefix code whenever this inequality holds. Each prefix code has unique decodability.
Order the prescribed lengths as . Construct a prefix code on the binary tree. At depth , previously assigned codewords exclude of the vertices. The Kraft inequality implies
so an available vertex can be assigned to the next symbol. None of the assigned words is a prefix of another. Reading each word up to the next assigned vertex gives unique decoding, so this constructs the required uniquely decodable code.
For completeness the same length inequality is necessary even for a uniquely decodable code which is not a prefix code. Write and . For successive symbols, distinct symbol strings have distinct concatenated codewords. At total length there are at most such strings. Therefore
Taking th roots and letting gives . This is the McMillan inequality.
Set . The relative entropy inequality gives
Here Shannon entropy uses base , with . The relative entropy is nonnegative, with equality precisely when the two probability distributions coincide; this follows from . Consequently equality holds exactly when
In particular all alphabet symbols must have positive probability for equality with finite prescribed lengths.
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.
True. The Huffman tree is a full binary tree, so its leaves form a complete prefix code. The Kraft inequality is therefore an equality:
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.
The Shannon lengths are . Their Competitive optimality of the Shannon code says that for every binary uniquely decodable code of lengths and every positive integer ,
On this event, . Summing and applying the Kraft inequality proves
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.
For the direct half of the code-distribution correspondence, let be the length function of a binary prefix code and put
Then is a probability mass function and
Thus every prefix code determines a distribution whose ideal description lengths do not exceed the codeword lengths.
Conversely, given a probability mass function , set
for . Then , so the Kraft inequality is satisfied. Its converse supplies a binary prefix code with these lengths, and
If real lengths are allowed, the ideal choice satisfies Kraft with equality.
Write , , and
On the support of , define the probability mass function
For any real length function satisfying the Kraft inequality, put and . The Gibbs inequality gives
Equality holds for the ideal weighted lengths
Thus the smallest average weighted description length is
When has full support, the displayed attains this minimum. If some strings have zero probability, the same value is the infimum over finite lengths and is attained by the extended-real ideal assignment there; finite codewords of arbitrarily large length approach it.
The code-distribution correspondence starts from the Kraft inequality. For codeword lengths put
Conversely, a probability mass function determines ideal lengths , up to integer rounding. For the source law ,
using nonnegativity of Kullback-Leibler divergence and .
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