Optimal source code 2026-10-06
For a fixed source distribution and code alphabet, an optimal uniquely decodable code minimizes expected codeword length. Huffman coding gives an optimal binary prefix code, and the equivalence of attainable uniquely-decodable and prefix-code lengths gives the same minimum for both classes.
Past exam of the mathematics course of the University of Cambridge 2005 ii Paper 1 4J Solution Created 2026-09-24 Updated 2026-09-24
Past exam of the mathematics course of the University of Cambridge 2008 ii Paper 2 4G Solution Created 2026-09-24 Updated 2026-09-24
Past exam of the mathematics course of the University of Cambridge 2009 ii Paper 1 4H ii Solution Created 2026-09-24 Updated 2026-09-24
Past exam of the mathematics course of the University of Cambridge 2009 ii Paper 1 4H i Solution Created 2026-09-24 Updated 2026-09-24
Past exam of the mathematics course of the University of Cambridge 2015 ii Paper 2 3G Solution Created 2026-09-24 Updated 2026-10-06
The Shannon entropy, in bits, isFor a prescribed alphabet distribution and code alphabet, an optimal code is a uniquely decodable code minimizing expected codeword length. Huffman coding produces an optimal binary prefix code, and its minimum is also the minimum among binary uniquely decodable codes.
Two optimal binary prefix codes, listing words in the order , areTheir length vectors are and , and both have expected length bits. To verify optimality rather than just equal length, run Huffman coding. First combine into a node of weight . Among the resulting three weight- nodes, combining and then combining the remaining weight- node with one weight- node gives the first length vector. Instead combine with the node, then combine that weight- node with , and finally with ; this gives the second. Each step combines two smallest current weights, so both constructions are Huffman coding trees. Ties therefore allow different optimal length profiles.
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 2020 ii Paper 1 3I b ii Solution Created 2026-09-24 Updated 2026-09-29
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 2023 ii Paper 1 3I b Solution Created 2026-09-24 Updated 2026-09-25
The Huffman coding merges may be chosen asResolving the tied weights so that the original symbol is merged only at the final step gives the codeIts lengths are and its expected length isThe Huffman coding theorem proves optimality, so an optimal coding with all but one word of the same length does exist. This is an instance of a nonunique optimal prefix code: a different resolution of the ties also gives the optimal profile .
Past exam of the mathematics course of the University of Cambridge 2024 ii Paper 1 3K Solution Created 2026-09-23 Updated 2026-09-25
Binary Huffman coding repeatedly merges the two least probable current symbols, assigns opposite bits to the two children of every merge, and reads each original symbol's codeword from the root. The resulting prefix code minimizes expected word length.
Past exam of the mathematics course of the University of Cambridge 2026 ii Paper 1 3H a Solution Created 2026-09-24 Updated 2026-09-25
Apply Huffman coding. Combining weightsgives, for example,Any consistent interchange of siblings is equivalent.