Sub topic 8
Entropy: The Lower Bound on Average Code Length
This chapter assumes you can read Python, know what a logarithm is, and have seen build_codes from the chapter on building the Huffman tree. The question here is how good a code can possibly be. Huffman gave "abracadabra" 23 bits. Could any code have done better?
Average code length
Suppose symbol i appears with probability pᵢ and gets a code ℓᵢ bits long. The expected code length is the average number of bits spent per symbol:
L = Σ pᵢ · ℓᵢ
For "abracadabra", Huffman assigns a 1 bit and each of b, r, c, d 3 bits. That gives L = (5·1 + 6·3) / 11 = 23/11 ≈ 2.091 bits per symbol. A fixed 3-bit code would cost exactly 3.
Shannon entropy
Entropy is the average information content of the source, measured in bits:
H = −Σ pᵢ · log₂ pᵢ
A symbol with probability p carries log₂(1/p) bits of information. Rare symbols carry more information and common ones carry less. Entropy is the probability-weighted average of these values.
Shannon's source coding theorem says that every uniquely decodable code satisfies L ≥ H, whether or not it is prefix-free. For a Huffman code there is also an upper bound:
H ≤ L < H + 1
So Huffman never wastes a full bit per symbol compared with the theoretical best.
Measuring the gap in code
The script below is self-contained. It repeats build_codes so you can run it directly.
import heapq
import math
from collections import Counter
from itertools import count
def build_codes(text):
freq = Counter(text)
tiebreak = count()
heap = [(f, next(tiebreak), sym) for sym, f in freq.items()]
heapq.heapify(heap)
if len(heap) == 1:
return {heap[0][2]: "0"}
while len(heap) > 1:
f1, _, left = heapq.heappop(heap)
f2, _, right = heapq.heappop(heap)
heapq.heappush(heap, (f1 + f2, next(tiebreak), (left, right)))
codes = {}
def walk(node, prefix):
if isinstance(node, tuple):
walk(node[0], prefix + "0")
walk(node[1], prefix + "1")
else:
codes[node] = prefix
walk(heap[0][2], "")
return codes
def entropy_bits(text):
n = len(text)
return -sum((f / n) * math.log2(f / n) for f in Counter(text).values())
def average_code_length(text, codes):
return sum(len(codes[ch]) for ch in text) / len(text)
text = "abracadabra"
codes = build_codes(text)
print(round(entropy_bits(text), 3), round(average_code_length(text, codes), 3))
# 2.04 2.091The gap is about 0.05 bits per symbol. No symbol-by-symbol code can reach 2.04 here, because code lengths must be whole numbers and log₂(11/5) ≈ 1.14 is not.
When the bound is met exactly
L = H holds exactly only when every probability is a power of ½, such as ½, ¼, or ⅛. In that case each ideal length log₂(1/p) is already an integer, and Huffman matches it.
Exercise
A source emits a, b, c with probabilities 0.5, 0.25, 0.25. Compute its entropy and the Huffman average code length. Why are they equal?
Show answer
H = 0.5·1 + 0.25·2 + 0.25·2 = 1.5 bits. Huffman assigns lengths 1, 2, 2, so L = 1.5 bits. They are equal because every probability is a power of ½, so each ideal length log₂(1/p) is a whole number.
Exercise
A binary source emits x with probability 0.99 and y with probability 0.01. What are H and the Huffman average length L, and what does the gap suggest?
Show answer
H ≈ 0.99·0.0145 + 0.01·6.644 ≈ 0.081 bits. Huffman must give each symbol at least 1 bit, so L = 1. That is about 12 times the entropy. It shows how whole-bit code lengths fail on very skewed sources, which the limitations chapter covers.