Real World Maths Busters

The Maths Behind Everyday Life

The story, the formula and a worked example — see exactly where this maths shows up in the world around you.

Back to all stories
Technology 3 min read

How zip files shrink text by being lazy about common letters

Give frequent symbols short codes and rare ones long codes — the total shrinks.

Huffman coding, the heart of zip and many formats, assigns shorter codes to characters that appear often and longer codes to rare ones. The weighted average code length drops below the fixed 8 bits per character.

Weight code length by frequency

The average bits per symbol is the sum of each symbol's probability times its code length. Short codes for common symbols pull the average down.

Lˉ=∑piLi\bar{L} = \sum p_i L_i

Average code length

A toy alphabet

One symbol at 50%, others rarer.

Beating 8 bits

  1. 1

    Common symbol

    Appears half the time, gets a 1-bit code.

  2. 2

    Rare symbols

    Get longer codes, but they appear seldom, so the weighted average stays low.

Answer

Frequent-means-short is why text compresses well below 8 bits per character.

The formula behind it

Average Code Length

Lˉ=∑piLi\bar{L} = \sum p_i L_i

The takeaway

Huffman coding shrinks data by matching code length to frequency. The frequency-weighted average beats fixed-width encoding.

Back to all stories