liveBook · Manning

Huffman encoding

As explained in Grokking Algorithms, Second Edition

Huffman coding is a lossless compression algorithm that represents more frequent symbols with shorter variable-length codes.

How can a text file take less space without losing its letters? A file containing “tilt” takes 4 bytes when each character uses one byte. Huffman encoding uses a binary tree to give symbols variable-length codes: frequent symbols get shorter codes. It is a technique for lossless text compression. section 7.6 section 7.0

Try shorter codes for “tilt”

With two-bit codes—t = 00, i = 01, l = 10—“tilt” needs 8 bits. Give the repeated t a shorter code instead—t = 1, i = 00, l = 01—and the same letters need 6 bits: 1 00 01 1. That compares code lengths, not the size of a finished compressed file. section 7.6

Short codes must still be readable without confusion. For example, a = 0, b = 1, c = 00 is ambiguous: 00 could mean “aa” or “c.” A binary tree provides a way to organize codes so a sequence can be decoded. The focus here is how the tree is used, rather than the steps for constructing a Huffman tree. section 7.6

How Grokking Algorithms, Second Edition covers it

How deep it goes
Works through character sizes and contrasting codes, but does not describe the algorithm for constructing a Huffman tree.
Examples
Compares codes for “tilt” and shows how an ambiguous assignment can give one bit sequence two readings.
What it assumes
Introduces bits, bytes, and character encodings before comparing binary codes.
What you can do after
Evaluate/understand how shorter codes can reduce bit counts and why decodable codes matter.
How it connects
Connects Huffman coding to binary trees, character encoding, bits, bytes, and text compression.

Also explained in

Compare how 4 books explain Huffman encoding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Algorithms, Second Edition. The section links point to the parts of the book it draws on.