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.
Past exam of the mathematics course of the University of Cambridge 2015 ii Paper 3 3G Solution Created 2026-09-24 Updated 2026-10-06
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 impliesso 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. ThereforeTaking th roots and letting gives . This is the McMillan inequality.
Set . The relative entropy inequality givesHere 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 whenIn particular all alphabet symbols must have positive probability for equality with finite prescribed lengths.
Past exam of the mathematics course of the University of Cambridge 2017 ii Paper 2 3G Solution Created 2026-09-24 Updated 2026-10-05
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 . ThereforeTaking 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 impliesso 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.
Past exam of the mathematics course of the University of Cambridge 2018 ii Paper 1 3H Solution Created 2026-09-24 Updated 2026-10-03
Let a memoryless source have probabilities , and let a uniquely decodable -ary code have word lengths and mean length . The Shannon source coding theorem statesand that some prefix code satisfies
For the lower bound, the Kraft inequality gives . Set . The Gibbs inequality, or nonnegativity of relative entropy, givesTherefore .
Past exam of the mathematics course of the University of Cambridge 2019 ii Paper 2 12G iii Solution Created 2026-09-24 Updated 2026-10-03
True. The Huffman tree is a full binary tree, so its leaves form a complete prefix code. The Kraft inequality is therefore an equality:
Past exam of the mathematics course of the University of Cambridge 2020 ii Paper 1 3I a Solution Created 2026-09-24 Updated 2026-09-29
In Shannon-Fano coding, a symbol of probability is assigned lengthand 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.
Past exam of the mathematics course of the University of Cambridge 2021 iii Paper 224 4 a Solution 2026-09-28
The binary Kraft inequality states that codeword lengths of a prefix code, or more generally a uniquely decodable code, satisfyConversely, positive integer lengths obeying this inequality can be realized by a binary prefix code.
Past exam of the mathematics course of the University of Cambridge 2021 iii Paper 224 4 b Solution Created 2026-09-28 Updated 2026-10-03
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
Past exam of the mathematics course of the University of Cambridge 2021 ii Paper 1 11K Solution Created 2026-09-24 Updated 2026-10-03
Take every logarithm in base two. The information entropy of iswith . If the codeword has length , then its expected codeword length is
For any decipherable code, the Kraft inequality givesDefine the probability distribution . By Gibbs inequality,Since ,
Conversely, choose the integer lengthsThen , 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 . Hencethe 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, ifthen 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.
Past exam of the mathematics course of the University of Cambridge 2022 iii Paper 224 4 a Solution 2026-09-28
The binary Kraft inequality says that codeword lengths of a prefix code satisfyConversely, 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 iswhere 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.
Past exam of the mathematics course of the University of Cambridge 2022 ii Paper 1 3K a Solution Created 2026-09-24 Updated 2026-09-25
For a -ary code with codeword lengths , Kraft inequality isChoose 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.
Past exam of the mathematics course of the University of Cambridge 2023 iii Paper 224 3 a Solution Created 2026-09-28 Updated 2026-10-03
Past exam of the mathematics course of the University of Cambridge 2023 iii Paper 224 3 b Solution 2026-09-28
For the direct half of the code-distribution correspondence, let be the length function of a binary prefix code and putThen is a probability mass function andThus every prefix code determines a distribution whose ideal description lengths do not exceed the codeword lengths.
Conversely, given a probability mass function , setfor . Then , so the Kraft inequality is satisfied. Its converse supplies a binary prefix code with these lengths, andIf real lengths are allowed, the ideal choice satisfies Kraft with equality.
Past exam of the mathematics course of the University of Cambridge 2023 iii Paper 224 3 c Solution 2026-09-28
Write , , andOn the support of , define the probability mass functionFor any real length function satisfying the Kraft inequality, put and . The Gibbs inequality givesEquality holds for the ideal weighted lengthsThus the smallest average weighted description length isWhen 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.
Past exam of the mathematics course of the University of Cambridge 2024 iii Paper 224 2 b Solution Created 2026-09-24 Updated 2026-09-25
The code-distribution correspondence starts from the Kraft inequality. For codeword lengths putConversely, a probability mass function determines ideal lengths , up to integer rounding. For the source law ,using nonnegativity of Kullback-Leibler divergence and .
Past exam of the mathematics course of the University of Cambridge 2025 iii Paper 224 4 a Solution Created 2026-09-24 Updated 2026-09-25
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 .
Past exam of the mathematics course of the University of Cambridge 2025 ii Paper 1 3K Solution Created 2026-09-23 Updated 2026-09-25
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. HenceThis 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 assignKraft's inequality applies because and therefore . Thus codewords of those lengths exist. Sincethe expected length obeys