Documentation

CslibFrontier.Algorithms.Lean.Huffman.Optimality

Optimality of Huffman Coding #

This module formalizes the relational specification of Huffman tree construction (IsHuffmanBuild) and proves its optimality over:

  1. 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 requiring DecidableEq α, LinearOrder β, or nonnegativity of symbol weights.
  2. All finite injective InformationTheory.IsPrefixFree binary codes (IsHuffmanBuild.weightedPathLength_le_of_isPrefixFree) under nonnegative weights, via unary trie compaction (exists_codeTree_of_isPrefixFree).
  3. All finite injective InformationTheory.IsUniquelyDecodable binary codes on at least two symbols (IsHuffmanBuild.weightedPathLength_le_of_isUniquelyDecodable, requiring 2 ≤ syms.length and proving InformationTheory.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 alphabet Bool, $D = 2$, and generalized from real probability weights to any ordered additive commutative monoid β). On a 1-symbol alphabet, CodeTree.leaf a has codewordSet = {[]}, which satisfies Mathlib's IsPrefixFree but not IsUniquelyDecodable.

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 #

Main results #

References #

Relational specification of Huffman tree construction #

structure Cslib.Algorithms.Lean.Huffman.IsMinPair {α : Type u_1} {β : Type u_2} [Add β] [LE β] (w : α → β) (F : List (CodeTree α)) (t₁ t₂ : CodeTree α) (F_rest : List (CodeTree α)) :

(t₁, t₂) is a minimum-weight pair extracted from forest F leaving F_rest.

inductive Cslib.Algorithms.Lean.Huffman.IsHuffmanBuild {α : Type u_1} {β : Type u_2} [Add β] [LE β] (w : α → β) :
List (CodeTree α) → CodeTree α → Prop

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).

theorem Cslib.Algorithms.Lean.Huffman.IsHuffmanBuild.leaves_perm_flatten {α : Type u_1} {β : Type u_2} [Add β] [LE β] {w : α → β} {F : List (CodeTree α)} {T : CodeTree α} (h : IsHuffmanBuild w F T) :
theorem Cslib.Algorithms.Lean.Huffman.IsHuffmanBuild.leaves_perm {α : Type u_1} {β : Type u_2} [Add β] [LE β] {w : α → β} {syms : List α} {T : CodeTree α} (h : IsHuffmanBuild w (List.map CodeTree.leaf syms) T) :
T.leaves.Perm syms
theorem Cslib.Algorithms.Lean.Huffman.IsHuffmanBuild.map {α₁ : Type u_1} {α₂ : Type u_2} {β : Type u_3} [Add β] [LE β] {w₁ : α₁ → β} {w₂ : α₂ → β} {f : α₁ → α₂} (hw : ∀ (a : α₁), w₂ (f a) = w₁ a) {F : List (CodeTree α₁)} {T : CodeTree α₁} (h : IsHuffmanBuild w₁ F T) :

Monoid rearrangement and depth-weighted list swaps #

theorem Cslib.Algorithms.Lean.Huffman.rearrangement_nsmul_le {β : Type u_1} [AddCommMonoid β] [PartialOrder β] [IsOrderedAddMonoid β] {d₁ d₂ : ℕ} (hd : d₁ ≤ d₂) {w₁ w₂ : β} (hw : w₁ ≤ w₂) :
d₂ • w₁ + d₁ • w₂ ≤ d₁ • w₁ + d₂ • w₂

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.

inductive Cslib.Algorithms.Lean.Huffman.LeftCherryAtDepth {δ : Type u_1} :
CodeTree δ → ℕ → δ → δ → (δ → CodeTree δ) → Prop

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.

theorem Cslib.Algorithms.Lean.Huffman.LeftCherryAtDepth.leaves_and_depths {δ : Type u_1} {T : CodeTree δ} {D : ℕ} {u v : δ} {C : δ → CodeTree δ} (h : LeftCherryAtDepth T D u v C) :
∃ (tail : List δ) (ds : List ℕ), T.leaves = u :: v :: tail ∧ (∀ (z : δ), (C z).leaves = z :: tail) ∧ List.map Prod.snd T.leafDepths = D :: D :: ds ∧ tail.length = ds.length
theorem Cslib.Algorithms.Lean.Huffman.exists_cherry_weightedPathLength_le {γ : Type u_1} {β : Type u_2} [AddCommMonoid β] [PartialOrder β] [IsOrderedAddMonoid β] (W : γ → β) {x y : γ} {F_rest : List γ} (S : CodeTree γ) (hperm : S.leaves.Perm (x :: y :: F_rest)) (hmin : ∀ z ∈ F_rest, W x ≤ W z ∧ W y ≤ W z) :
∃ (S' : CodeTree γ) (C' : γ → CodeTree γ) (tail' : List γ), LeftCherryAtDepth S' S.maxDepth x y C' ∧ S'.leaves = x :: y :: tail' ∧ tail'.Perm F_rest ∧ (∀ (z : γ), (C' z).leaves = z :: tail') ∧ CodeTree.weightedPathLength W S' ≤ CodeTree.weightedPathLength W S

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.

theorem Cslib.Algorithms.Lean.Huffman.LeftCherryAtDepth.weight_contract_eq {γ : Type u_1} {β : Type u_2} [AddCommMonoid β] {S : CodeTree γ} {D : ℕ} {x y : γ} {C : γ → CodeTree γ} (h : LeftCherryAtDepth S D x y C) (W : γ → β) {z : γ} (hz : W z = W x + W y) :
theorem Cslib.Algorithms.Lean.Huffman.LeftCherryAtDepth.weightedPathLength_eq_contract_add {γ : Type u_1} {β : Type u_2} [AddCommMonoid β] {S : CodeTree γ} {D : ℕ} {x y : γ} {C : γ → CodeTree γ} (h : LeftCherryAtDepth S D x y C) (W : γ → β) {z : γ} (hz : W z = W x + W y) :

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.

theorem Cslib.Algorithms.Lean.Huffman.LeftCherryAtDepth.join_contract_eq {α : Type u_1} {S : CodeTree (CodeTree α)} {D : ℕ} {t₁ t₂ : CodeTree α} {C : CodeTree α → CodeTree (CodeTree α)} (h : LeftCherryAtDepth S D t₁ t₂ C) :
(C (t₁.branch t₂)).join = S.join

Contracting a sibling cherry (t₁, t₂) in a skeleton CodeTree (CodeTree α) into .leaf (.branch t₁ t₂) leaves CodeTree.join identical.

Main optimality theorems ([CLRS09], Theorem 16.4; [CT06], Theorem 5.8.1) #

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.

theorem Cslib.Algorithms.Lean.Huffman.IsHuffmanBuild.weightedPathLength_le_of_isPrefixFree {α : Type u_1} {β : Type u_2} [AddCommMonoid β] [PartialOrder β] [IsOrderedAddMonoid β] {syms : List α} (hne : syms ≠ []) (hnodup : syms.Nodup) {w : α → β} (hw : ∀ a ∈ syms, 0 ≤ w a) {T : CodeTree α} (hbuild : IsHuffmanBuild w (List.map CodeTree.leaf syms) T) {code : α → List Bool} (hinj : Set.InjOn code {a : α | a ∈ syms}) (hpf : InformationTheory.IsPrefixFree (code '' {a : α | a ∈ syms})) :
CodeTree.weightedPathLength w T ≤ (List.map (fun (a : α) => (code a).length • w a) syms).sum

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.

theorem Cslib.Algorithms.Lean.Huffman.IsHuffmanBuild.weightedPathLength_le_of_isUniquelyDecodable {α : Type u_1} {β : Type u_2} [AddCommMonoid β] [PartialOrder β] [IsOrderedAddMonoid β] {syms : List α} (h2 : 2 ≤ syms.length) (hnodup : syms.Nodup) {w : α → β} (hw : ∀ a ∈ syms, 0 ≤ w a) {T : CodeTree α} (hbuild : IsHuffmanBuild w (List.map CodeTree.leaf syms) T) {code : α → List Bool} (hinj : Set.InjOn code {a : α | a ∈ syms}) (hud : InformationTheory.IsUniquelyDecodable (code '' {a : α | a ∈ 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.