Documentation

CslibFrontier.Algorithms.Lean.Huffman.Kraft

Binary Kraft Converse, Prefix-Free Trie Compaction, and McMillan Reduction #

Mathlib's Mathlib.InformationTheory.Coding.Kraft and KraftMcMillan prove the forward $D$-ary Kraft and Kraft–McMillan inequalities ∑ w ∈ S, (1 / D) ^ w.length ≤ 1 for finite prefix-free and uniquely decodable codes. This module supplies the finite binary ($D = 2$, alphabet Bool) constructive converse and tree reductions needed for Huffman optimality ([Kra49]; [McM56]; [CT06], Theorems 5.2.1 and 5.5.1, specialized from $D$-ary to binary codes and from real probability weights to arbitrary ordered additive commutative monoids β):

  1. Prefix-free codebook compaction (treeOfCodebook): any nonempty prefix-free binary codebook contracts (by eliminating unary internal nodes) to a full binary CodeTree α with the same leaf symbols and at most the same weighted external path length under nonnegative weights.
  2. Constructive binary Kraft converse (kraftAssign, exists_isPrefixFree_iff_kraft_le_one): sorting symbols in nondecreasing order of target length ℓ and assigning consecutive dyadic subintervals of [0, 2 ^ L) constructs an explicit injective prefix-free binary code achieving exact lengths ℓ whenever ∑ a ∈ s, (1 / 2 : ℝ) ^ ℓ a ≤ 1, completing the if-and-only-if characterization.
  3. McMillan tree reduction (exists_codeTree_of_isUniquelyDecodable): combining Mathlib's IsUniquelyDecodable.finsetSum_one_div_card_pow_length_le_one with the binary Kraft converse and treeOfCodebook, every finite injective uniquely decodable binary code on at least two symbols (2 ≤ syms.length) admits a full binary CodeTree α whose codebook is itself InformationTheory.IsUniquelyDecodable and whose weighted path length is at most that of the original code over any ordered additive commutative monoid β. (For a single symbol, the unique full binary CodeTree.leaf a has codewordSet = {[]}, which is IsPrefixFree in Mathlib but not IsUniquelyDecodable.)

Main definitions #

Main results #

References #

Fixed-width binary representation and dyadic prefix intervals #

Most-significant-bit-first k-bit binary representation of q % 2 ^ k.

Natural number represented by a most-significant-bit-first list of bits.

Constructive binary Kraft converse #

def Cslib.Algorithms.Lean.Huffman.kraftAssign {α : Type u_1} (L : ℕ) (ℓ : α → ℕ) :
ℕ → List α → List (α × List Bool)

Assign consecutive dyadic codewords of lengths ℓ inside [0, 2 ^ L) to a list of symbols sorted in nondecreasing order of ℓ.

@[simp]
theorem Cslib.Algorithms.Lean.Huffman.map_fst_kraftAssign {α : Type u_1} (L : ℕ) (ℓ : α → ℕ) (offset : ℕ) (xs : List α) :
List.map Prod.fst (kraftAssign L ℓ offset xs) = xs
theorem Cslib.Algorithms.Lean.Huffman.length_snd_of_mem_kraftAssign {α : Type u_1} {L : ℕ} {ℓ : α → ℕ} {offset : ℕ} {xs : List α} {p : α × List Bool} (hp : p ∈ kraftAssign L ℓ offset xs) :
p.2.length = ℓ p.1
theorem Cslib.Algorithms.Lean.Huffman.exists_isPrefixFree_of_kraft_nat {α : Type u_1} (s : Finset α) (ℓ : α → ℕ) (L : ℕ) (hL : ∀ a ∈ s, ℓ a ≤ L) (hkraft : ∑ a ∈ s, 2 ^ (L - ℓ a) ≤ 2 ^ L) :
∃ (code : α → List Bool), Set.InjOn code ↑s ∧ InformationTheory.IsPrefixFree (code '' ↑s) ∧ ∀ a ∈ s, (code a).length = ℓ a

Integer-Scaled Binary Kraft Converse: if s : Finset α and ℓ : α → ℕ satisfy ∀ a ∈ s, ℓ a ≤ L and ∑ a ∈ s, 2 ^ (L - ℓ a) ≤ 2 ^ L, then there exists an injective prefix-free binary code on s whose codewords have exact lengths ℓ.

theorem Cslib.Algorithms.Lean.Huffman.exists_isPrefixFree_iff_kraft_le_one {α : Type u_1} (s : Finset α) (ℓ : α → ℕ) :
(∃ (code : α → List Bool), Set.InjOn code ↑s ∧ InformationTheory.IsPrefixFree (code '' ↑s) ∧ ∀ a ∈ s, (code a).length = ℓ a) ↔ ∑ a ∈ s, (1 / 2) ^ ℓ a ≤ 1

Binary Kraft Theorem ([Kra49]; [CT06], Theorem 5.2.1 specialized to binary alphabet Bool, $D = 2$, on finite codebooks s : Finset α): a finite symbol set s admits an injective prefix-free binary code with codeword lengths ℓ if and only if ∑ a ∈ s, (1 / 2 : ℝ) ^ ℓ a ≤ 1.

Prefix-free codebook compaction to CodeTree and McMillan reduction #

@[irreducible]

Compact a binary codebook pairs : List (α × List Bool) into a full binary CodeTree α by recursively branching on first bits and contracting unary internal nodes.

theorem Cslib.Algorithms.Lean.Huffman.treeOfCodebook_spec {α : Type u_1} {β : Type u_2} [AddCommMonoid β] [PartialOrder β] [IsOrderedAddMonoid β] {pairs : List (α × List Bool)} (hne : pairs ≠ []) (hinc : List.Pairwise (fun (c₁ c₂ : List Bool) => ¬c₁ <+: c₂ ∧ ¬c₂ <+: c₁) (List.map Prod.snd pairs)) {w : α → β} (hw : ∀ p ∈ pairs, 0 ≤ w p.1) :
∃ (T : CodeTree α), treeOfCodebook pairs = some T ∧ T.leaves.Perm (List.map Prod.fst pairs) ∧ CodeTree.weightedPathLength w T ≤ (List.map (fun (p : α × List Bool) => p.2.length • w p.1) pairs).sum

On any nonempty prefix-incomparable codebook pairs, treeOfCodebook pairs succeeds and produces a full binary CodeTree α with the same leaf multiset and at most the same weighted path length under nonnegative weights.

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

Prefix-Free Code Tree Reduction: every nonempty finite injective prefix-free binary code on syms admits a full binary CodeTree α with the same leaf symbols and at most the same weighted path length under nonnegative weights.

theorem Cslib.Algorithms.Lean.Huffman.exists_codeTree_of_isUniquelyDecodable {α : Type u_1} {β : Type u_2} [AddCommMonoid β] [PartialOrder β] [IsOrderedAddMonoid β] {syms : List α} (h2 : 2 ≤ syms.length) (hnodup : syms.Nodup) {code : α → List Bool} (hinj : Set.InjOn code {a : α | a ∈ syms}) (hud : InformationTheory.IsUniquelyDecodable (code '' {a : α | a ∈ syms})) {w : α → β} (hw : ∀ a ∈ syms, 0 ≤ w a) :

McMillan Code Tree Reduction (derived from McMillan's forward inequality [McM56] / [CT06], Theorem 5.5.1 in Mathlib together with the finite binary Kraft converse of Theorem 5.2.1 and exists_codeTree_of_isPrefixFree): every finite injective uniquely decodable binary ($D = 2$) code on at least two distinct symbols (2 ≤ syms.length) admits a full binary CodeTree α with the same leaf symbols, a uniquely decodable codebook (InformationTheory.IsUniquelyDecodable T.codewordSet), and at most the same weighted path length under nonnegative weights over any ordered additive commutative monoid β.

The hypothesis 2 ≤ syms.length is necessary for the CodeTree representation: on a 1-symbol alphabet, the only full binary tree CodeTree.leaf a assigns the empty codeword [] (codewordSet = {[]}), which satisfies InformationTheory.IsPrefixFree in Mathlib but violates InformationTheory.IsUniquelyDecodable.epsilon_notMem, whereas every uniquely decodable 1-symbol code has length at least 1.