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 Solution 2026-10-03
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.
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 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 2022 ii Paper 1 3K b Solution Created 2026-09-24 Updated 2026-10-03
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.
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 ii Paper 1 3I a Solution Created 2026-09-24 Updated 2026-09-25
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 isIt 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 iscontradicting optimality. Thuswhich 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.
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
Shannon-Fano coding Created 2026-09-24 Updated 2026-09-24