Optimality of Huffman Coding #
This module formalizes the relational specification of Huffman tree construction (IsHuffmanBuild)
and proves its optimality over:
- All full binary
CodeTree αs (IsHuffmanBuild.weightedPathLength_le, formalizing [Huf52] and [CLRS09], Lemmas 16.2–16.3 and Theorem 16.4), over any ordered additive commutative monoidβ(ℕ,ℤ,ℚ,ℝ, ...) without requiringDecidableEq α,LinearOrder β, or nonnegativity of symbol weights. - All finite injective
InformationTheory.IsPrefixFreebinary codes (IsHuffmanBuild.weightedPathLength_le_of_isPrefixFree) under nonnegative weights, via unary trie compaction (exists_codeTree_of_isPrefixFree). - All finite injective
InformationTheory.IsUniquelyDecodablebinary codes on at least two symbols (IsHuffmanBuild.weightedPathLength_le_of_isUniquelyDecodable, requiring2 ≤ syms.lengthand provingInformationTheory.IsUniquelyDecodable T.codewordSet) under nonnegative weights, via Mathlib's Kraft–McMillan inequality and our constructive binary Kraft converse (exists_codeTree_of_isUniquelyDecodable, [McM56]; [CT06], Theorem 5.8.1, specialized to binary alphabetBool, $D = 2$, and generalized from real probability weights to any ordered additive commutative monoidβ). On a 1-symbol alphabet,CodeTree.leaf ahascodewordSet = {[]}, which satisfies Mathlib'sIsPrefixFreebut notIsUniquelyDecodable.
Proof architecture #
Rather than indexing into trees via [DecidableEq α] (which creates nine-way aliasing casework and
breaks on duplicate symbols during intermediate merges), we represent a competitor over an
intermediate forest F : List (CodeTree α) as a skeleton S : CodeTree (CodeTree α) with
S.leaves.Perm F. Rotating deeper subtrees leftward (rotateDeepestLeft) places a maximum-depth
sibling cherry at the first two leaf slots without changing weightedPathLength, relabeling moves a
minimum-weight pair (t₁, t₂) into that cherry without increasing cost (Lemma 16.2), and
contracting the cherry via LeftCherryAtDepth leaves CodeTree.join unchanged
(LeftCherryAtDepth.join_contract_eq) while also satisfying the additive shift identity of
Lemma 16.3.
Main definitions #
Cslib.Algorithms.Lean.Huffman.IsMinPair: two trees in a forest have minimum weight among all remaining trees.Cslib.Algorithms.Lean.Huffman.IsHuffmanBuild: relational trace of greedy Huffman forest merges.Cslib.Algorithms.Lean.Huffman.LeftCherryAtDepth: witness that aCodeTreehas a sibling cherry at the bottom of its leftmost spine at a specified depth, paired with its contraction context.
Main results #
Cslib.Algorithms.Lean.Huffman.exists_cherry_weightedPathLength_le: greedy-choice exchange lemma ([CLRS09], Lemma 16.2).Cslib.Algorithms.Lean.Huffman.LeftCherryAtDepth.weightedPathLength_eq_contract_add: cherry contraction path-length identity ([CLRS09], Lemma 16.3).Cslib.Algorithms.Lean.Huffman.IsHuffmanBuild.weightedPathLength_le: optimality among all full binaryCodeTrees ([CLRS09], Theorem 16.4).Cslib.Algorithms.Lean.Huffman.IsHuffmanBuild.weightedPathLength_le_of_isPrefixFree: optimality among all injectiveInformationTheory.IsPrefixFreebinary codes.Cslib.Algorithms.Lean.Huffman.IsHuffmanBuild.weightedPathLength_le_of_isUniquelyDecodable: feasibility (IsUniquelyDecodable T.codewordSet) and optimality among all injectiveInformationTheory.IsUniquelyDecodablebinary codes for2 ≤ syms.length.
References #
- [Huf52] David A. Huffman, A Method for the Construction of Minimum-Redundancy Codes, 1952.
- [CLRS09] Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein, Introduction to Algorithms (3rd ed.), Section 16.3, Lemmas 16.2–16.3 and Theorem 16.4, 2009.
- [CT06] Thomas M. Cover and Joy A. Thomas, Elements of Information Theory (2nd ed.), Section 5.8, Theorem 5.8.1, 2006.
Relational specification of Huffman tree construction #
(t₁, t₂) is a minimum-weight pair extracted from forest F leaving F_rest.
Extracting
t₁andt₂fromFleavesF_restup to permutation.- weight_le (t : CodeTree α) : t ∈ F_rest → CodeTree.weight w t₁ ≤ CodeTree.weight w t ∧ CodeTree.weight w t₂ ≤ CodeTree.weight w t
Both
t₁andt₂have weight at most every remaining tree inF_rest.
IsHuffmanBuild w F T holds when T is obtained from the non-empty forest F by repeatedly
merging a minimum-weight pair of trees ([Huf52]; [CLRS09], Section 16.3).
- single {α : Type u_1} {β : Type u_2} [Add β] [LE β] {w : α → β} (t : CodeTree α) : IsHuffmanBuild w [t] t
- step {α : Type u_1} {β : Type u_2} [Add β] [LE β] {w : α → β} {F F_rest : List (CodeTree α)} {t₁ t₂ T : CodeTree α} : IsMinPair w F t₁ t₂ F_rest → IsHuffmanBuild w (t₁.branch t₂ :: F_rest) T → IsHuffmanBuild w F T
Monoid rearrangement and depth-weighted list swaps #
Subtraction-free rearrangement inequality: if d₁ ≤ d₂ and w₁ ≤ w₂, assigning the larger
depth d₂ to the smaller weight w₁ does not exceed assigning d₂ to w₂.
Maximum-depth leftward rotation, relabeling, and cherry contraction #
Rotate deeper subtrees to the left so that the leftmost spine ends in a sibling cherry at depth
T.maxDepth.
LeftCherryAtDepth T D u v C witnesses that T has the sibling cherry
.branch (.leaf u) (.leaf v) at depth D along its leftmost spine, with contraction context C
replacing that cherry by .leaf z.
- base {δ : Type u_1} (u v : δ) : LeftCherryAtDepth ((CodeTree.leaf u).branch (CodeTree.leaf v)) 1 u v fun (z : δ) => CodeTree.leaf z
- left {δ : Type u_1} {l r : CodeTree δ} {d : ℕ} {u v : δ} {C : δ → CodeTree δ} : LeftCherryAtDepth l d u v C → LeftCherryAtDepth (l.branch r) (d + 1) u v fun (z : δ) => (C z).branch r
Greedy-Choice Exchange Lemma ([CLRS09], Lemma 16.2): in any full binary tree S whose
leaves contain a minimum-weight pair (x, y) (∀ z ∈ F_rest, W x ≤ W z ∧ W y ≤ W z), rotating and
swapping (x, y) into a maximum-depth sibling cherry produces a tree S' with the same leaf
multiset and S'.weightedPathLength W ≤ S.weightedPathLength W.
Cherry-Contraction Path-Length Identity ([CLRS09], Lemma 16.3): contracting a sibling
cherry (x, y) into a single leaf z of weight W z = W x + W y shifts weightedPathLength by
W x + W y.
Contracting a sibling cherry (t₁, t₂) in a skeleton CodeTree (CodeTree α) into
.leaf (.branch t₁ t₂) leaves CodeTree.join identical.
Skeleton Optimality of IsHuffmanBuild: for any forest F and any skeleton
S : CodeTree (CodeTree α) with S.leaves.Perm F, a Huffman merge tree T built from F achieves
T.weightedPathLength w ≤ S.join.weightedPathLength w.
Optimality of Huffman Coding over Full Binary Code Trees ([Huf52]; [CLRS09],
Theorem 16.4): every tree T produced by IsHuffmanBuild w (syms.map .leaf) T minimizes
weightedPathLength w over all full binary trees T' whose leaf multiset is a permutation of
syms.
Optimality of Huffman Coding over Prefix-Free Binary Codes ([Huf52];
[CT06], Section 5.8): under nonnegative symbol weights, a Huffman tree T achieves
weighted path length at most the weighted codeword length of any injective
InformationTheory.IsPrefixFree binary code on syms.
Optimality of Huffman Coding over All Uniquely Decodable Binary Codes ([McM56];
[CT06], Theorem 5.8.1 specialized to binary alphabet Bool, $D = 2$, and generalized to
arbitrary ordered additive commutative monoid weights β): on at least two distinct symbols
(2 ≤ syms.length) with nonnegative weights, a Huffman tree T produces a uniquely decodable
codebook (InformationTheory.IsUniquelyDecodable T.codewordSet) whose weighted path length is at
most the weighted codeword length of any injective InformationTheory.IsUniquelyDecodable binary
code on syms.
On a 1-symbol alphabet, the unique full binary tree CodeTree.leaf a has codewordSet = {[]},
which is InformationTheory.IsPrefixFree in Mathlib (so
IsHuffmanBuild.weightedPathLength_le_of_isPrefixFree still applies) but not
InformationTheory.IsUniquelyDecodable.