# B.6 Huffman Encoding

Huffman encoding looks at the symbol table of a whole data set. The compression is achieved by finding the "weights" of each symbol in the data set. Some symbols occur more frequently than others, so Huffman encoding suggests that the frequent symbols need not be encoded using as many bits as the less-frequent symbols. There are variations on Huffman-style encoding, but the original (and frequent) variation involves looking for the most common symbol and encoding it using just one bit, say 1. If you encounter a 0, you know you're on the way to encoding a longer variable length symbol.

Let's imagine we apply Huffman encoding to our local phone-book example (assume we have already whitespace-compressed the report). We might get:

```Encoding   Symbol
1           7
010         2
011         3
00000       4
00001       5
00010       6
00011       8
00100       9
00101       0
00111       1
```

Our initial symbol set of digits could already be straightforwardly encoded (with no-compression) as 4-bit sequences (nibbles). The Huffman encoding given will use up to 5-bits for the worst-case symbols, which is obviously worse than the nibble encoding. However, our best case will use only 1 bit, and we know that our best case is also the most frequent case, by having scanned the data set. So we might encode a particular phone number like:

```772 7628 --> 1 1 010 1 00010 010 00011
```

The nibble encoding would take 28-bits to represent a phone number; in this particular case, our encoding takes 19-bits. I introduced spaces into the example above for clarity; you can see that they are not necessary to unpack the encoding, since the encoding table will determine whether we have reached the end of an encoded symbol (but you have to keep track of your place in the bits).

Huffman encoding is still fairly cheap to decode, cycle-wise. But it requires a table lookup, so it cannot be quite as cheap as RLE, however. The encoding side of Huffman is fairly expensive, though; the whole data set has to be scanned and a frequency table built up. In some cases a "shortcut" is appropriate with Huffman coding. Standard Huffman coding applies to a particular data set being encoded, with the set-specific symbol table prepended to the output data stream. However, if the whole type of data encoded?not just the single data set?has the same regularities, we can opt for a global Huffman table. If we have such a global Huffman table, we can hard-code the lookups into our executables, which makes both compression and decompression quite a bit cheaper (except for the initial global sampling and hard-coding). For example, if we know our data set would be English-language prose, letter-frequency tables are well known and quite consistent across data sets.  Chapter 1. Python Basics  Chapter 2. Basic String Operations  Chapter 3. Regular Expressions  Chapter 4. Parsers and State Machines  Chapter 5. Internet Tools and Techniques  Appendix A. A Selective and Impressionistic Short Review of Python  Appendix B. A Data Compression Primer  B.1 Introduction  B.2 Lossless and Lossy Compression  B.3 A Data Set Example  B.4 Whitespace Compression  B.5 Run-Length Encoding  B.6 Huffman Encoding  B.7 Lempel Ziv-Compression  B.8 Solving the Right Problem  B.9 A Custom Text Compressor  B.10 References  Appendix C. Understanding Unicode  Appendix D. A State Machine for Adding Markup to Text  Appendix E. Glossary