Documentation

CslibFrontier.Algorithms.Lean.LempelZiv.LZ78Bits

The LZ78 bit code and its length #

encodeBits e x writes the LZ78 (pointer, symbol) pairs of x as bits ([ZL78]; [CT06], Ch. 13). The i-th pair (counting from 1) has a pointer in {0, …, i - 1}, written in ⌈log₂ i⌉ bits, and a symbol, written in ⌈log₂ k⌉ bits through a fixed numbering e : α ≃ Fin k of the alphabet; numbers are written least significant bit first (Cslib.BitString.lowBits). The widths depend only on the position, so the decoder needs no separators. For k ≥ 2 every pair has at least one bit and decodeBits recovers the input.

The length of the code of an input of length n with c phrases is exactly ∑_{i=1}^{c} (⌈log₂ i⌉ + ⌈log₂ k⌉) ≤ c (⌈log₂ c⌉ + ⌈log₂ k⌉). With the distinct-parsing bound c log_k c ≤ n + 2c this gives ⌈log₂ k⌉ (n + 4c), and with c ≤ n / ((1 - ε_n) log_k n) the worst-case length ⌈log₂ k⌉ n (1 + 4 / ((1 - ε_n) log_k n)) = ⌈log₂ k⌉ n (1 + o(1)): LZ78 never expands its input by more than a vanishing fraction over the fixed-length code of ⌈log₂ k⌉ n bits.

Main definitions #

Main results #

References #

def Cslib.Algorithms.Lean.LempelZiv.LZ78.pairsToBits {α : Type u_1} {k : ℕ} (e : α ≃ Fin k) :
ℕ → List (ℕ × α) → List Bool

Write (pointer, symbol) pairs as bits, the pair at position i (counting from 0) with a pointer of ⌈log₂ (i + 1)⌉ bits and a symbol of ⌈log₂ k⌉ bits.

def Cslib.Algorithms.Lean.LempelZiv.LZ78.bitsToPairs {α : Type u_1} {k : ℕ} (e : α ≃ Fin k) :
ℕ → ℕ → List Bool → Option (List (ℕ × α))

Read (pointer, symbol) pairs written by pairsToBits, starting at position i, with fuel steps. Fails on a truncated pair or a symbol number ≥ k.

LZ78 bit encoder.

def Cslib.Algorithms.Lean.LempelZiv.LZ78.decodeBits {α : Type u_1} {k : ℕ} (e : α ≃ Fin k) (bs : List Bool) :

LZ78 bit decoder.

theorem Cslib.Algorithms.Lean.LempelZiv.LZ78.length_pairsToBits {α : Type u_1} {k : ℕ} (e : α ≃ Fin k) (i : ℕ) (ps : List (ℕ × α)) :
(pairsToBits e i ps).length = ∑ j ∈ Finset.range ps.length, (Nat.clog 2 (i + j + 1) + Nat.clog 2 k)

The exact length of pairsToBits.

theorem Cslib.Algorithms.Lean.LempelZiv.LZ78.decodeBits_encodeBits {α : Type u_1} {k : ℕ} [DecidableEq α] (e : α ≃ Fin k) (hk : 2 ≤ k) (x : List α) :

The LZ78 bit code is lossless for an alphabet of size k ≥ 2.

theorem Cslib.Algorithms.Lean.LempelZiv.LZ78.length_encodeBits {α : Type u_1} {k : ℕ} [DecidableEq α] (e : α ≃ Fin k) (x : List α) :
(encodeBits e x).length = ∑ i ∈ Finset.range (parse x).length, (Nat.clog 2 (i + 1) + Nat.clog 2 k)

The exact length of the LZ78 bit code: ∑_{i=1}^{c} (⌈log₂ i⌉ + ⌈log₂ k⌉), where c is the number of phrases.

The LZ78 bit code of an input with c phrases has at most c (⌈log₂ c⌉ + ⌈log₂ k⌉) bits.

theorem Cslib.Algorithms.Lean.LempelZiv.LZ78.length_encodeBits_le_mul_add {α : Type u_1} {k : ℕ} [DecidableEq α] (e : α ≃ Fin k) (hk : 2 ≤ k) (x : List α) :

Finite length bound. For an alphabet of size k ≥ 2, the LZ78 bit code of an input of length n with c phrases has at most ⌈log₂ k⌉ (n + 4c) bits.

theorem Cslib.Algorithms.Lean.LempelZiv.LZ78.length_encodeBits_le_mul_one_add {α : Type u_1} {k : ℕ} [DecidableEq α] (e : α ≃ Fin k) (hk : 2 ≤ k) (x : List α) (hn : 2 ≤ x.length) (hε : DistinctParsing.epsilon k x.length < 1) :
↑(encodeBits e x).length ≤ ↑(Nat.clog 2 k) * ↑x.length * (1 + 4 / ((1 - DistinctParsing.epsilon k x.length) * Real.logb ↑k ↑x.length))

Worst-case length n (1 + o(1)). For an alphabet of size k ≥ 2 and an input of length n ≥ 2 with ε_n < 1, the LZ78 bit code has at most ⌈log₂ k⌉ n (1 + 4 / ((1 - ε_n) log_k n)) bits; the overhead factor tends to 0 (tendsto_four_div_one_sub_epsilon_mul_logb).

The overhead factor 4 / ((1 - ε_n) log_k n) of the worst-case LZ78 length tends to 0.

theorem Cslib.Algorithms.Lean.LempelZiv.LZ78.exists_length_encodeBits_le {α : Type u_1} {k : ℕ} [DecidableEq α] (e : α ≃ Fin k) (hk : 2 ≤ k) :
∃ (δ : ℕ → ℝ), Filter.Tendsto δ Filter.atTop (nhds 0) ∧ ∀ (x : List α), ↑(encodeBits e x).length ≤ ↑(Nat.clog 2 k) * ↑x.length * (1 + δ x.length)

The LZ78 code length is ⌈log₂ k⌉ n (1 + o(1)) in the worst case: there is a sequence δ n → 0 such that every input of length n over an alphabet of size k ≥ 2 has a bit code of at most ⌈log₂ k⌉ n (1 + δ n) bits.