Documentation

CslibFrontier.Algorithms.Lean.Huffman.Basic

Binary Code Trees and Prefix-Free Bit-Stream Coding #

This module defines full binary code trees (CodeTree α), their weighted external path length (CodeTree.weightedPathLength), their prefix-free binary codebook (CodeTree.codewords), and total bit-stream encoding and decoding (CodeTree.encode, CodeTree.decode), connecting the tree model to Mathlib's InformationTheory.IsPrefixFree and InformationTheory.IsUniquelyDecodable ([Huf52]; [CLRS09], Section 16.3; [CT06], Chapter 5).

Main definitions #

Main results #

References #

A full binary code tree with symbols of type α at the leaves.

Instances For
    @[instance_reducible]
    def Cslib.Algorithms.Lean.Huffman.instDecidableEqCodeTree.decEq {α✝ : Type u_1} [DecidableEq α✝] (x✝ x✝¹ : CodeTree α✝) :
    Decidable (x✝ = x✝¹)
    @[instance_reducible]
    instance Cslib.Algorithms.Lean.Huffman.instReprCodeTree {α✝ : Type u_1} [Repr α✝] :
    Repr (CodeTree α✝)

    Whether a CodeTree consists of a single leaf.

    Left-to-right list of leaf symbols in T.

    Pairs (a, d) of each leaf symbol a together with its root-to-leaf depth d.

    Maximum root-to-leaf depth of T.

    def Cslib.Algorithms.Lean.Huffman.CodeTree.map {α : Type u_1} {β : Type u_2} (f : α → β) :
    CodeTree α → CodeTree β

    Functorial map on leaf labels.

    Graft a CodeTree of subtrees into a single CodeTree.

    @[simp]
    theorem Cslib.Algorithms.Lean.Huffman.CodeTree.leaves_map {α : Type u_1} {β : Type u_2} (f : α → β) (T : CodeTree α) :

    Subtree weight and weighted external path length #

    def Cslib.Algorithms.Lean.Huffman.CodeTree.weight {α : Type u_1} {β : Type u_2} [Add β] (w : α → β) :
    CodeTree α → β

    Total weight of all leaves in T with respect to symbol weight w.

    def Cslib.Algorithms.Lean.Huffman.CodeTree.weightedPathLength {α : Type u_1} {β : Type u_2} [Add β] [Zero β] (w : α → β) :
    CodeTree α → β

    Weighted external path length of T, defined recursively via internal child-weight sums ([CLRS09], Exercise 16.3-4): 0 at a leaf, and l.weightedPathLength w + r.weightedPathLength w + (l.weight w + r.weight w) at an internal branch.

    @[simp]
    theorem Cslib.Algorithms.Lean.Huffman.CodeTree.weight_leaf {α : Type u_1} {β : Type u_2} [Add β] (w : α → β) (a : α) :
    weight w (leaf a) = w a
    @[simp]
    theorem Cslib.Algorithms.Lean.Huffman.CodeTree.weight_branch {α : Type u_1} {β : Type u_2} [Add β] (w : α → β) (l r : CodeTree α) :
    weight w (l.branch r) = weight w l + weight w r
    theorem Cslib.Algorithms.Lean.Huffman.CodeTree.weight_map {α : Type u_1} {β : Type u_2} {γ : Type u_3} [Add β] {w₁ : α → β} {w₂ : γ → β} {f : α → γ} (T : CodeTree α) (hw : ∀ (a : α), w₂ (f a) = w₁ a) :
    weight w₂ (map f T) = weight w₁ T
    @[simp]
    theorem Cslib.Algorithms.Lean.Huffman.CodeTree.weightedPathLength_leaf {α : Type u_1} {β : Type u_2} [Add β] [Zero β] (w : α → β) (a : α) :
    @[simp]
    theorem Cslib.Algorithms.Lean.Huffman.CodeTree.weightedPathLength_branch {α : Type u_1} {β : Type u_2} [Add β] [Zero β] (w : α → β) (l r : CodeTree α) :
    theorem Cslib.Algorithms.Lean.Huffman.CodeTree.weight_eq_sum_leaves {α : Type u_1} {β : Type u_2} [AddCommMonoid β] (w : α → β) (T : CodeTree α) :
    theorem Cslib.Algorithms.Lean.Huffman.CodeTree.weight_eq_of_leaves_perm {α : Type u_1} {β : Type u_2} [AddCommMonoid β] (w : α → β) {T₁ T₂ : CodeTree α} (h : T₁.leaves.Perm T₂.leaves) :
    weight w T₁ = weight w T₂
    theorem Cslib.Algorithms.Lean.Huffman.CodeTree.weight_nonneg {α : Type u_1} {β : Type u_2} [AddCommMonoid β] [PartialOrder β] [IsOrderedAddMonoid β] {w : α → β} {T : CodeTree α} (hw : ∀ a ∈ T.leaves, 0 ≤ w a) :
    0 ≤ weight w T
    theorem Cslib.Algorithms.Lean.Huffman.CodeTree.weightedPathLength_eq_sum_leafDepths {α : Type u_1} {β : Type u_2} [AddCommMonoid β] (w : α → β) (T : CodeTree α) :
    weightedPathLength w T = (List.map (fun (p : α × ℕ) => p.2 • w p.1) T.leafDepths).sum

    External-Path-Length Identity ([CLRS09], Equation (16.4) and Exercise 16.3-4): the recursive internal-weight sum weightedPathLength w equals ∑ (a, d) ∈ T.leafDepths, d • w a.

    theorem Cslib.Algorithms.Lean.Huffman.CodeTree.weight_join {α : Type u_1} {β : Type u_2} [Add β] (w : α → β) (S : CodeTree (CodeTree α)) :
    weight w S.join = weight (fun (t : CodeTree α) => weight w t) S
    theorem Cslib.Algorithms.Lean.Huffman.CodeTree.weightedPathLength_join {α : Type u_1} {β : Type u_2} [AddCommMonoid β] (w : α → β) (S : CodeTree (CodeTree α)) :

    Grafting subtrees onto a skeleton S : CodeTree (CodeTree α) decomposes weighted external path length into the skeleton's weighted path length (under subtree weights) plus the sum of the subtrees' own weighted path lengths.

    Prefix-free codebook and bit-stream encoding/decoding #

    Canonical binary codebook assigning false to left branches and true to right branches.

    Set of binary codewords produced by T.

    @[simp]

    Distinct leaf entries in T.codewords have prefix-incomparable codewords.

    A CodeTree codebook is InformationTheory.IsUniquelyDecodable if and only if the tree has at least two leaves: every branch l r assigns nonempty prefix-free codewords, whereas a single leaf leaf a assigns the empty codeword [], violating InformationTheory.IsUniquelyDecodable.epsilon_notMem.

    Lookup the binary codeword of symbol a in T (first match in left-to-right order).

    Encode a message msg : List α by concatenating symbol codewords.

    Decode a single symbol by walking from cur to a leaf, returning the remaining bit stream.

    theorem Cslib.Algorithms.Lean.Huffman.CodeTree.decodeOne_branch_length_lt {α : Type u_1} {l r : CodeTree α} {bits rem : List Bool} {a : α} (h : (l.branch r).decodeOne bits = some (a, rem)) :
    rem.length < bits.length
    @[irreducible]

    Decode a bit stream into a list of symbols using a non-leaf code tree T.

    theorem Cslib.Algorithms.Lean.Huffman.CodeTree.encodeSym_map_of_injective {α : Type u_1} {β : Type u_2} [DecidableEq α] [DecidableEq β] {f : α → β} (hf : Function.Injective f) (T : CodeTree α) (a : α) :
    (map f T).encodeSym (f a) = T.encodeSym a
    theorem Cslib.Algorithms.Lean.Huffman.CodeTree.decodeOne_append_of_mem_codewords {α : Type u_1} {T : CodeTree α} {a : α} {c : List Bool} (hmem : (a, c) ∈ T.codewords) (rest : List Bool) :
    T.decodeOne (c ++ rest) = some (a, rest)
    theorem Cslib.Algorithms.Lean.Huffman.CodeTree.exists_encode_of_subset_leaves {α : Type u_1} [DecidableEq α] {T : CodeTree α} {msg : List α} (hmsg : ∀ a ∈ msg, a ∈ T.leaves) :
    ∃ (bits : List Bool), T.encode msg = some bits
    theorem Cslib.Algorithms.Lean.Huffman.CodeTree.decode_encode {α : Type u_1} [DecidableEq α] (l r : CodeTree α) {msg : List α} {bits : List Bool} (henc : (l.branch r).encode msg = some bits) :
    (l.branch r).decode bits = some msg

    Lossless Bit-Stream Round-Trip: for any non-leaf CodeTree (branch l r) and any message whose encoded bit stream is bits, decode recovers msg exactly.

    theorem Cslib.Algorithms.Lean.Huffman.CodeTree.length_encode_eq_weightedPathLength_count {α : Type u_1} [DecidableEq α] {T : CodeTree α} (hnodup : T.leaves.Nodup) {msg : List α} {bits : List Bool} (henc : T.encode msg = some bits) :
    bits.length = weightedPathLength (fun (a : α) => List.count a msg) T

    When T.leaves.Nodup, the bit length of T.encode msg equals the weighted external path length of T under the empirical symbol frequency function fun a => msg.count a.