TOPICS
Search

Huffman Coding


Huffman coding is a lossless data compression algorithm that constructs a binary prefix-free code minimizing the frequency-weighted sum of codeword lengths (Huffman 1952). Frequently occurring symbols can receive shorter codewords.

Huffman coding works on a list of weights {w_i} by building an extended binary tree with minimum weighted external path length and proceeds by finding the two smallest ws, w_1 and w_2, viewed as external nodes, and replacing them with an internal node of weight w_1+w_2. The procedure is them repeated stepwise until the root node is reached. An individual external node can then be encoded by a binary string of 0s (for left branches) and 1s (for right branches).

HuffmanCoding

The procedure is summarized below for the weights 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, and 41 given by the first 13 primes, and the resulting tree is shown above (Knuth 1997, pp. 402-403). As is clear from the diagram, the paths to the larger weights are shorter than those to the smaller weights. In this example, the number 13 would be encoded as 1010.

2357111317192329313741
557111317192329313741
107111317192329313741
17111317192329313741
172417192329313741
2434192329313741
24344229313741
344253313741
4253653741
42536578
956578
95143
238

In the unequal-letter-cost version, the cost of a codeword is the sum of the costs of its bits, and the objective is to minimize the frequency-weighted sum of these costs. Chandran and Gajjala (2026) proved that deciding whether a binary prefix-free code has total cost at most a given threshold is an NP-complete problem even when the letter costs are M and M+1 for a positive integer M. The frequencies, threshold, and M are part of the input and encoded in binary. For each fixed pair of positive integer letter costs, an exact polynomial-time algorithm is available (Golin and Rote 1998). Chandran and Gajjala (2026) credit Codex with helping develop the reduction.

The following Wolfram Language code can be used to construct the list of internal nodes and table of iterations:

  HuffmanStep[l0_List] := Module[
    {
      l = l0,
      s2 = Take[Select[Sort[l0], Positive], 2]
    },
    l[[Take[Flatten[Position[l, #]& /@ s2], 2]]] = 0;
    l[[Last[Position[l, 0]]]] = Plus @@ s2;
    {l, s2}
  ]
  HuffmanList[l_List] := Module[{},
    Plus @@@ Last /@ NestWhileList[
      HuffmanStep[First[#]]&,
        HuffmanStep[l], Length[Union[First[#]]] > 2&]
  ]
  HuffmanTable[l_List] :=
    NestWhileList[First[HuffmanStep[#]]&, l,
      Length[Union[#]] > 2&]

See also

Codeword, External Path Length, Prefix-Free Code

Explore with Wolfram|Alpha

References

Chandran, L. S. and Gajjala, R. "Binary Huffman Coding with Unequal Letter Costs Is NP-Complete." 3 Nov 2026. https://arxiv.org/abs/2611.04444.Golin, M. J. and Rote, G. "A Dynamic Programming Algorithm for Constructing Optimal Prefix-Free Codes with Unequal Letter Costs." IEEE Trans. Inform. Th. 44, 1770-1781, 1998. https://doi.org/10.1109/18.705558.Huffman, D. A. "A Method for the Construction of Minimum-Redundancy Codes." Proc. Inst. Radio Eng. 40, 1098-1101, 1952.Knuth, D. E. The Art of Computer Programming, Vol. 1: Fundamental Algorithms, 3rd ed. Reading, MA: Addison-Wesley, pp. 402-406, 1997.Press, W. H.; Flannery, B. P.; Teukolsky, S. A.; and Vetterling, W. T. "Huffman Coding and Compression of Data." Ch. 20.4 in Numerical Recipes in FORTRAN: The Art of Scientific Computing, 2nd ed. Cambridge, England: Cambridge University Press, pp. 896-901, 1992.Schwarz, E. S. "An Optimum Encoding with Minimum Longest Code and Total Number of Digits." Information and Control 7, 37-44, 1964.

Referenced on Wolfram|Alpha

Huffman Coding

Cite this as:

Weisstein, Eric W. "Huffman Coding." From MathWorld--A Wolfram Resource. https://mathworld.wolfram.com/HuffmanCoding.html

Subject classifications