Two-Queue Huffman Construction #
When input symbols are already sorted in nondecreasing order of nonnegative weight, van Leeuwen
([vL76]) observed that Huffman's algorithm does not need a general priority queue:
maintaining a list q₁ of unmerged leaves alongside a single FIFO queue q₂ of merged internal
nodes keeps q₂ automatically sorted, because consecutive merged pair sums w(t₁) + w(t₂) are
monotone nondecreasing under nonnegative weights.
This module provides the pure mathematical construction and optimality theorems; machine-level
Word-RAM running-time bounds (O(n) from pre-sorted weights and O(n log n) from unsorted weights)
are proved in CslibFrontier.Algorithms.WordRAM.Huffman:
- Two-list functional FIFO queue (
FifoQueue γ) ([CLRS09], Exercise 10.1-6):peek?inspectsfront.head?directly under the canonical invariantfront = [] → back = [], andenqueue/dequeue?satisfy explicit potential-method list-reversal bounds (enqueue_revCost_bound,dequeue?_revCost_bound). UnlikeStd.Queue(Init.Data.Queue, which does not maintaindList = [] → eList = [], so inspecting the head without committing a dequeue would repeatedly reverseeList) andCslib.DataStructure.Queue(an abstractCslib.DataStructuretwo-stack module withoutpeek?),FifoQueueinspects the head directly whenpopMincompares the heads ofq₁andq₂and popsq₁while leavingq₂unchanged. - Cached-weight trees (
WeightedTree α β): each queue entry cacheswt = tree.weight w, so each merge computesa.wt + b.wtwithout re-traversing subtrees. - Van Leeuwen two-queue construction (
huffmanFromSorted): fuel-free step equationtwoQueueLoop_step, queue-list specificationpopMin_spec_toList, and correctness on sorted nonnegative inputs (huffmanFromSorted_spec). - End-to-end unsorted Huffman construction (
huffman): sortssymswithList.mergeSortand runshuffmanFromSorted, producing a tree certified byIsHuffmanBuild(hence optimal over allCodeTrees and all injectiveIsPrefixFreebinary codes, and—when2 ≤ syms.length, avoiding the 1-symbolCodeTree.leaf adegeneracy whosecodewordSet = {[]}isIsPrefixFreein Mathlib but notIsUniquelyDecodable—producing a uniquely decodable codebook optimal over all injectiveIsUniquelyDecodablebinary codes).
Main definitions #
Cslib.Algorithms.Lean.Huffman.FifoQueue: two-list functional FIFO queue.Cslib.Algorithms.Lean.Huffman.WeightedTree:CodeTree αpaired with its cached weightβ.Cslib.Algorithms.Lean.Huffman.popMin,twoQueueLoop: single minimum-extraction step and main merge loop of van Leeuwen's two-queue construction.Cslib.Algorithms.Lean.Huffman.huffmanFromSorted: two-queue Huffman construction from sorted symbols.Cslib.Algorithms.Lean.Huffman.huffman: Huffman construction from arbitrary symbols viaList.mergeSort.
Main results #
Cslib.Algorithms.Lean.Huffman.FifoQueue.enqueue_revCost_bound,Cslib.Algorithms.Lean.Huffman.FifoQueue.dequeue?_revCost_bound: potential-method list-reversal bounds forFifoQueue.Cslib.Algorithms.Lean.Huffman.popMin_spec_toList,twoQueueLoop_step: representation-free step specifications forpopMinandtwoQueueLoop.Cslib.Algorithms.Lean.Huffman.huffmanFromSorted_spec: correctness (IsHuffmanBuildand leaf permutation) on any nonempty sorted symbol list with nonnegative weights.Cslib.Algorithms.Lean.Huffman.huffman_spec,huffman_optimal,huffman_optimal_isPrefixFree,huffman_optimal_isUniquelyDecodable: end-to-end correctness and optimality ofhuffman(with2 ≤ syms.lengthandIsUniquelyDecodable T.codewordSetin the uniquely decodable headline).
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.), Exercise 10.1-6 and Section 16.3, 2009.
Two-list functional FIFO queue #
Empty FIFO queue.
Abstract FIFO sequence represented by q.
Number of elements stored in q.
Enqueue x at the back of q.
Peek at the front element of q by inspecting front.head?.
Potential bound for enqueue with potential Φ(q) = q.back.length:
reversal steps plus ΔΦ is at most 1.
Cached-weight trees and van Leeuwen's two-queue loop #
Pop a minimum-weight entry from leaf list q₁ and internal-node queue q₂ (preferring q₁ on
ties, as in [vL76]). Matching directly on q₂.front ensures FifoQueue.checkf is
evaluated under strict evaluation only in branches that actually dequeue from q₂.
Representation-free specification of popMin in terms of q₂.toList whenever q₂.WF holds.
Main merge loop of van Leeuwen's two-queue Huffman algorithm ([vL76]).
Fuel-free unfolding equation for twoQueueLoop.
Huffman construction from an arbitrary symbol list syms, sorting first via List.mergeSort
and then running huffmanFromSorted.
IsHuffmanBuild depends on the starting forest F only up to List.Perm.
Correctness of Two-Queue Huffman Construction ([vL76]): on any nonempty symbol
list syms sorted in nondecreasing order of w with nonnegative weights,
huffmanFromSorted w syms returns some T satisfying IsHuffmanBuild w (syms.map .leaf) T and
T.leaves.Perm syms.
End-to-End Correctness of huffman: on any nonempty symbol list syms with nonnegative
weights, huffman w syms returns some T satisfying IsHuffmanBuild w (syms.map .leaf) T and
T.leaves.Perm syms.
Optimality of huffman over Full Binary Code Trees: the tree returned by huffman w syms
minimizes weightedPathLength w among all full binary CodeTree αs with leaf multiset syms.
Optimality of huffman over All Prefix-Free Binary Codes: on distinct symbols syms with
nonnegative weights, huffman w syms achieves weighted path length at most that of any injective
InformationTheory.IsPrefixFree binary code.
Optimality of huffman over All Uniquely Decodable Binary Codes: on at least two distinct
symbols (2 ≤ syms.length) with nonnegative weights, huffman w syms returns a tree T whose
codebook is InformationTheory.IsUniquelyDecodable and achieves weighted path length at most that
of any injective InformationTheory.IsUniquelyDecodable binary code.
On a 1-symbol input, huffman w [a] = some (.leaf a) has codewordSet = {[]}, which is
InformationTheory.IsPrefixFree in Mathlib (covered by huffman_optimal_isPrefixFree) but not
InformationTheory.IsUniquelyDecodable.