Huffman coding
What Is Huffman coding?
Huffman coding is a lossless data compression technique that assigns variable-length binary codewords to symbols, giving shorter codewords to symbols that appear more frequently in a data stream and longer codewords to rarer ones. The result is a prefix-free code, meaning no valid codeword forms the beginning of another, which guarantees unambiguous decoding without delimiters. The method was introduced by David A. Huffman in his 1952 paper "A Method for the Construction of Minimum-Redundancy Codes," published in the Proceedings of the IRE, and remains one of the most widely deployed compression primitives in computing.
Huffman coding belongs to the family of entropy coding methods, grounded in Claude Shannon's 1948 information theory. The algorithm produces a code whose average codeword length L satisfies H(S) ≤ L < H(S) + 1, where H(S) is the entropy of the symbol source in bits per symbol. This bound makes it optimal among all prefix-free codes for a given symbol probability distribution, as documented in Huffman coding overviews on ScienceDirect.
The Huffman Algorithm
Constructing a Huffman code begins with a table of symbols and their associated probabilities or frequencies. The algorithm repeatedly selects the two nodes with the lowest probabilities, merges them into a parent node whose probability equals their sum, and inserts the parent back into the priority queue. This process continues until a single root node remains, forming a binary tree. Traversing from root to each leaf, assigning 0 to left branches and 1 to right branches (or vice versa), generates the codeword for the corresponding symbol.
The tree construction can be implemented efficiently using a min-heap, yielding an O(n log n) algorithm for a vocabulary of n symbols. A key property of the resulting tree is that the two least-probable symbols always occupy the deepest level and differ only in their final bit. This structure guarantees that the assignment is both prefix-free and minimum-redundancy: no other binary prefix code for the same probability distribution has a smaller expected codeword length.
Canonical and Adaptive Variants
Standard Huffman coding requires the decoder to receive or reconstruct the code tree before decompressing, adding overhead for stored or transmitted codebooks. Canonical Huffman coding addresses this by imposing additional constraints, specifically that codewords of the same length are assigned in alphabetical order and that longer codes are always numerically greater than shorter ones. A canonical code can be transmitted by sending only the codeword lengths for each symbol, significantly reducing the codebook size. DEFLATE, the compression algorithm underlying ZIP files and the zlib library, uses canonical Huffman codes as its second-stage coder.
Adaptive Huffman coding, independently developed by Newton Faller and Robert Gallager in the 1970s, constructs and updates the code tree dynamically as symbols are processed. Because the transmitter and receiver maintain synchronized trees, no separate codebook transmission is needed, making the method suitable for streaming applications. IEEE work on adaptive Huffman coding with memory optimization continues to refine the approach for resource-constrained embedded systems where codebook storage is limited. The DEFLATE algorithm, specified in IETF RFC 1951, defines the canonical Huffman encoding scheme used in ZIP and PNG and remains the most widely deployed realization of Huffman compression.
Applications
Huffman coding has applications in a range of fields, including:
- Image compression: JPEG uses Huffman coding to encode the quantized discrete cosine transform (DCT) coefficients
- Video compression: MPEG and H.26x standards incorporate Huffman or related context-adaptive entropy coding stages
- Audio compression: MP3 uses Huffman coding to compress quantized spectral coefficients
- Lossless file compression: DEFLATE-based formats (ZIP, PNG, gzip) combine LZ77 dictionary coding with Huffman entropy coding
- Multimedia streaming and archival systems requiring efficient symbol-level entropy reduction