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:
- Linear-time
O(n)construction from pre-sorted weights (fromSortedCode,fromSortedProgram): givenn = ws.lengthnondecreasing symbol weightsws : List ℕin standard Word-RAM input layout (State.init ws), runsn - 1iterations 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 ofn - 1iterations, and halts within57 * n + 24instructions outputting the exact per-symbol Huffman codeword-length list viaExecutesListwheneverFits w (6 * n + 2)andFits w ws.sum. O(n log n)construction from arbitrary unsorted weights (huffmanCode,huffmanProgram): sorts an index permutation[0, …, n - 1]by nondecreasing input weight usingCslib.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 within52 * n * Nat.clog 2 n + 82 * n + 47instructions wheneverFits w (10 * n + 3)andFits w ws.sum.
Main definitions #
Cslib.Algorithms.WordRAM.Huffman.symWeight: symbol weight lookupi ↦ ws[i]onFin ws.length.Cslib.Algorithms.WordRAM.Huffman.Reg.out: output register (Fin 23) holding the returned length-prefixed codeword-length array address.Cslib.Algorithms.WordRAM.Huffman.fromSortedCode,Cslib.Algorithms.WordRAM.Huffman.fromSortedProgram:O(n)Word-RAM Huffman construction from pre-sorted weights.Cslib.Algorithms.WordRAM.Huffman.huffmanCode,Cslib.Algorithms.WordRAM.Huffman.huffmanProgram:O(n log n)Word-RAM Huffman construction from arbitrary unsorted weights.
Main results #
Cslib.Algorithms.WordRAM.Huffman.fromSortedCode_exec,Cslib.Algorithms.WordRAM.Huffman.fromSortedProgram_spec:57 * n + 23structured /57 * n + 24compiled Word-RAM instruction bounds, input-slice preservation,ExecutesListcodeword-length output,IsHuffmanBuildcertificate (covering full-tree andIsPrefixFreeoptimality for alln ≥ 1, with the degeneraten = 1case producinglens = [0]andCodeTree.leaf 0), and uniquely decodable feasibility (IsUniquelyDecodable T.codewordSet) and optimality for2 ≤ non pre-sorted weights at word widths fitting6 * n + 2andws.sum.Cslib.Algorithms.WordRAM.Huffman.huffmanCode_exec,Cslib.Algorithms.WordRAM.Huffman.huffmanProgram_spec:52 * n * Nat.clog 2 n + 82 * n + 46structured /+ 47compiled Word-RAM instruction bounds, input-slice preservation,ExecutesListcodeword-length output,IsHuffmanBuildcertificate (with the samen = 1,lens = [0]degeneracy), and uniquely decodable feasibility and optimality for2 ≤ non arbitrary weights at word widths fitting10 * n + 3andws.sum.
References #
- [vL76] Jan van Leeuwen, On the Construction of Huffman Trees, ICALP 1976, pp. 382–410.
- [Huf52] David A. Huffman, A Method for the Construction of Minimum-Redundancy Codes, 1952.
- [CLRS09] Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein, Introduction to Algorithms (3rd ed.), Section 16.3, 2009.
Private Arithmetic and Memory Helpers #
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.