Documentation

CslibFrontier.Algorithms.WordRAM.Huffman

Huffman Tree and Codeword-Length Construction on the Word RAM #

This module implements van Leeuwen's two-queue Huffman construction ([vL76]) and unsorted Huffman coding ([Huf52]; [CLRS09], Section 16.3) as deterministic structured and compiled programs over Cslib.WordRAM.Basis.standard, charging every array load, store, address calculation, weight addition, comparison, loop test, and final halt:

  1. Linear-time O(n) construction from pre-sorted weights (fromSortedCode, fromSortedProgram): given n = ws.length nondecreasing symbol weights ws : List ℕ in standard Word-RAM input layout (State.init ws), runs n - 1 iterations of van Leeuwen's two-queue merge pass directly over the input leaf slice and an internal-node FIFO array, records explicit left/right child arrays in memory, propagates root-to-leaf depths in a single reverse pass of n - 1 iterations, and halts within 57 * n + 24 instructions outputting the exact per-symbol Huffman codeword-length list via ExecutesList whenever Fits w (6 * n + 2) and Fits w ws.sum.
  2. O(n log n) construction from arbitrary unsorted weights (huffmanCode, huffmanProgram): sorts an index permutation [0, …, n - 1] by nondecreasing input weight using Cslib.Algorithms.WordRAM.MergeSort.mergeSortPerm, gathers the sorted weight array, invokes the shared two-queue subroutine, and scatters the resulting leaf depths back to the original symbol indices within 52 * n * Nat.clog 2 n + 82 * n + 47 instructions whenever Fits w (10 * n + 3) and Fits w ws.sum.

Main definitions #

Main results #

References #

Weight of symbol i : Fin ws.length in weight list ws.

Private Arithmetic and Memory Helpers #

Register File (Fin 23) #

@[reducible, inline]

Output register holding the base address of the length-prefixed codeword-length list.

Two-Queue Huffman Core (O(n) on pre-sorted weights) #

End-to-End O(n) Word-RAM Huffman on Pre-Sorted Weights #

Structured O(n) Word-RAM Huffman builder from standard input State.init ws.

Compiled O(n) Word-RAM Huffman program returning the codeword-length list via ExecutesList.

Structured ExecWithin specification of fromSortedCode: halts within 57 * n + 23 instructions, preserves the input weight slice Block.HoldsList 1 ws y.mem, and writes the Huffman codeword-length list at y.regs Reg.out. For ws ≠ [] this yields a tree T certified by IsHuffmanBuild (on the degenerate n = 1 input, lens = [0] and T = CodeTree.leaf 0, whose codebook {[]} is IsPrefixFree in Mathlib but not IsUniquelyDecodable); for 2 ≤ ws.length, T.codewordSet is InformationTheory.IsUniquelyDecodable and optimal over all injective InformationTheory.IsUniquelyDecodable binary codes.

Linear-Time O(n) Word-RAM Huffman Construction from Pre-Sorted Weights ([vL76]): on any list ws : List ℕ of n = ws.length nondecreasing symbol weights in State.init ws at word width w satisfying Fits w (6 * n + 2) and Fits w ws.sum, fromSortedProgram halts within 57 * n + 24 instructions outputting a codeword-length list lens (lens.length = n) via ExecutesList such that (when ws ≠ []) there is a tree T produced by huffmanFromSorted certified by IsHuffmanBuild whose codeword for each symbol i : Fin n has length lens.getD i 0 (optimal over all full binary code trees and injective IsPrefixFree binary codes; on the degenerate n = 1 input, lens = [0] and T = CodeTree.leaf 0, whose codebook {[]} is IsPrefixFree in Mathlib but not IsUniquelyDecodable). Whenever 2 ≤ n, T.codewordSet is InformationTheory.IsUniquelyDecodable and minimizes weighted codeword length over all injective InformationTheory.IsUniquelyDecodable binary codes.

End-to-End O(n log n) Word-RAM Huffman on Unsorted Weights #

Structured O(n log n) Word-RAM Huffman builder from arbitrary unsorted input State.init ws.

Structured ExecWithin specification of huffmanCode: halts within 52 * n * Nat.clog 2 n + 82 * n + 46 instructions, preserves the input weight slice Block.HoldsList 1 ws yF.mem, and writes the Huffman codeword-length list at yF.regs Reg.out. For ws ≠ [] this yields a tree T certified by IsHuffmanBuild (on the degenerate n = 1 input, finalLens = [0] and T = CodeTree.leaf 0, whose codebook {[]} is IsPrefixFree in Mathlib but not IsUniquelyDecodable); for 2 ≤ ws.length, T.codewordSet is InformationTheory.IsUniquelyDecodable and optimal over all injective InformationTheory.IsUniquelyDecodable binary codes.

Compiled O(n log n) Word-RAM Huffman program returning the codeword-length list via ExecutesList.

O(n log n) Word-RAM Huffman Construction from Unsorted Weights ([Huf52]; [vL76]; [CLRS09], Section 16.3): on any list ws : List ℕ of n = ws.length nonnegative symbol weights in State.init ws at word width w satisfying Fits w (10 * n + 3) and Fits w ws.sum, huffmanProgram halts within 52 * n * Nat.clog 2 n + 82 * n + 47 instructions outputting a codeword-length list lens (lens.length = n) via ExecutesList certified by IsHuffmanBuild (optimal over all full binary code trees and injective IsPrefixFree binary codes; on the degenerate n = 1 input, lens = [0] and T = CodeTree.leaf 0, whose codebook {[]} is IsPrefixFree in Mathlib but not IsUniquelyDecodable). Whenever 2 ≤ n, T.codewordSet is InformationTheory.IsUniquelyDecodable and minimizes weighted codeword length over all injective InformationTheory.IsUniquelyDecodable binary codes.