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

Wolfram Web Resources

Mathematica »

The #1 tool for creating Demonstrations and anything technical.

Wolfram|Alpha »

Explore anything with the first computational knowledge engine.

Wolfram Demonstrations Project »

Explore thousands of free applications across science, mathematics, engineering, technology, business, art, finance, social sciences, and more.

Computerbasedmath.org »

Join the initiative for modernizing math education.

Online Integral Calculator »

Solve integrals with Wolfram|Alpha.

Step-by-step Solutions »

Walk through homework problems step-by-step from beginning to end. Hints help you try the next step on your own.

Wolfram Problem Generator »

Unlimited random practice problems and answers with built-in Step-by-step solutions. Practice online or make a printable study sheet.

Wolfram Education Portal »

Collection of teaching and learning tools built by Wolfram education experts: dynamic textbook, lesson plans, widgets, interactive Demonstrations, and more.

Wolfram Language »

Knowledge-based programming for everyone.