TOPICS
Search

Huffman Coding


A lossless data compression algorithm which uses a small number of bits to encode common characters. Huffman coding approximates the probability for each character as a power of 1/2 to avoid complications associated with using a nonintegral number of bits to encode characters using their actual probabilities.

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

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&]

Explore with Wolfram|Alpha

References

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 Web Resource. https://mathworld.wolfram.com/HuffmanCoding.html

Subject classifications