ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
CanonicalHuffman.h File Reference

The canonical code of Huffman (chapter 12, Statistical Coding, of Ferragina's book): the lengths \( L( \sigma ) \) of the codes of Huffman [see Huffman.h, whose huffman() gives them], and a new code with the same lengths, hence as short, chosen so that neither the encoder nor the decoder needs the tree. More...

#include "ADS/Contract.h"
#include "ADS/compression/PackedBits.h"
#include "ADS/string/Huffman.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <map>
#include <span>
#include <string>
#include <type_traits>
#include <vector>
#include "ADS/compression/impl/CanonicalHuffman.tpp"

Go to the source code of this file.

Classes

class  ADS::CanonicalHuffman< C >
 the canonical prefix code of the given lengths of the symbols of type C More...

Namespaces

namespace  ADS

Functions

template<class C, class F>
std::map< C, int > ADS::huffmanLengths (const std::map< C, F > &freq)
 the lengths of the codes of Huffman [see Huffman.h] of the symbols with the frequencies freq
template<class C, class F>
std::map< C, int > ADS::limitedLengths (const std::map< C, F > &freq, int L)
 the lengths of a code of Huffman of no length above \( L \), by halving the frequencies (rounding up) until the code of Huffman of the halved ones has no length above \( L \)

Detailed description

The canonical code of Huffman (chapter 12, Statistical Coding, of Ferragina's book): the lengths \( L( \sigma ) \) of the codes of Huffman [see Huffman.h, whose huffman() gives them], and a new code with the same lengths, hence as short, chosen so that neither the encoder nor the decoder needs the tree.

With \( \mathit{num}[ l ] \) the number of the symbols of length \( l \) and \( \max \) the longest length, the first code of length \( l \) is \( \mathit{fc}[ \max ] = 0 \) and \( \mathit{fc}[ l ] = \lceil ( \mathit{fc}[ l + 1 ] + \mathit{num}[ l + 1 ] ) / 2 \rceil \), the parent of the first unused node of the level below, and the symbols of length \( l \), in their order, get the consecutive codes \( \mathit{fc}[ l ] , \mathit{fc}[ l ] + 1 , \ldots \) on \( l \) bits: the tree is skewed to the left, the longest codes being all zeros at the start. The same chapter divides without rounding up, which is the same on the complete codes that the algorithm of Huffman gives (the sum is even there); rounding up also keeps the decoder right on a code that is not complete, e.g., one cut by the limit on the lengths. The decoder reads one bit at a time, \( v \) being the value of the \( l \) bits read, and stops at the first \( l \) with \( v \ge \mathit{fc}[ l ] \), since the codes of length \( l \) are the values from \( \mathit{fc}[ l ] \) on and every prefix of a longer code is smaller: the symbol is \( \mathit{symb}[ l ][ v - \mathit{fc}[ l ] ] \). Hence the preamble is the lengths alone, at most \( \max^2 \) bits for \( \mathit{fc} \) and \( ( |\Sigma| + \max ) \log_2( |\Sigma| + 1 ) \) for \( \mathit{symb} \), instead of the pointers of the tree, and the decoding of a code of \( l \) bits makes \( l \) steps on two arrays that fit in the cache, instead of \( l \) steps along the pointers of the tree, each a possible miss. The decoders of the practice go further, as the literature explains: a table indexed by the next \( k \) bits gives the length of most codes in one access (Moffat and Turpin) [see HuffmanCoder.h], and the canonical code is that of the Huffword of Witten, Moffat and Bell, whose symbols are the words of a text.

The average length of the code of Huffman is within one bit of the entropy, \( H_0 \le \bar\ell < H_0 + 1 \), since its lengths are optimal and those of Shannon, \( \lceil \log_2( 1 / P[ \sigma ] ) \rceil \), have an average below \( H_0 + 1 \) [see Entropy.h], and its codes can be long: a frequency that grows as the numbers of Fibonacci gives a code of 33 bits after about \( 10^7 \) symbols (chapter 12, Statistical Coding, of Ferragina's book). limitedLengths() bounds them to a given \( L \) by the iterative scaling of the same chapter: the frequencies are halved until the code of Huffman has no length above \( L \), which in the limit gives a code of fixed length \( \lceil \log_2 |\Sigma| \rceil \); the package-merge of Larmore and Hirschberg finds the optimal code of lengths at most \( L \) in \( O( |\Sigma| L ) \) time, at the price of a less simple algorithm [see limitedHuffmanLengths() of HuffmanCoder.h]. The construction takes \( O( |\Sigma| \log |\Sigma| ) \) time, the encoding of a symbol \( O( 1 ) \) and its decoding \( O( L( \sigma ) ) \).

The contracts [see Contract.h]: the lengths are positive, at most 63, and satisfy the inequality of Kraft, \( \sum_\sigma 2^{-L( \sigma )} \le 1 \); the frequencies of limitedLengths() are positive and \( 2^L \ge |\Sigma| \); a symbol encoded has a code, and the bits decoded are codes; the decoding of an encoding gives the text back, an audit. The operations are defined in CanonicalHuffman.tpp.

See also
P. Ferragina, Pearls of Algorithm Engineering, Cambridge University Press, 2023, chapter 12, Statistical Coding, doi:10.1017/9781009128933
D. A. Huffman, A method for the construction of minimum-redundancy codes, Proceedings of the IRE 40(9), 1098-1101, 1952, doi:10.1109/JRPROC.1952.273898
E. S. Schwartz, B. Kallick, Generating a canonical prefix encoding, Communications of the ACM 7(3), 166-169, 1964, doi:10.1145/363958.363991
A. Moffat, A. Turpin, On the implementation of minimum redundancy prefix codes, IEEE Transactions on Communications 45(10), 1200-1207, 1997, doi:10.1109/26.634683
L. L. Larmore, D. S. Hirschberg, A fast algorithm for optimal length-limited Huffman codes, Journal of the ACM 37(3), 464-473, 1990, doi:10.1145/79147.79150
I. H. Witten, A. Moffat, T. C. Bell, Managing Gigabytes, 2nd ed., Morgan Kaufmann, 1999
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa