Encodings and Compression
Prefix-free codes, entropy, Shannon's source coding theorem, and integer encoding schemes (unary, Golomb, Elias-Gamma, Elias-Delta, gap encoding) and UTF-8.
Concepts#
- Prefix-Free Code — the property that makes a code decodable without delimiters; Kraft’s inequality says which code lengths are possible
- Entropy — lower bound on the expected code length of any prefix-free code
- Entropy-Optimal Code — code whose expected length equals the entropy; exists iff all
- Lagrange Multipliers — proof tool: minimising under the Kraft constraint gives
- Unary Code — simplest integer code, optimal for , building block of Golomb and Elias codes
- Golomb Code — unary quotient + binary remainder with modulus , for geometrically distributed gaps
- Elias-Gamma Code — unary length prefix + binary, no parameter, bits
- Elias-Delta Code — Elias-Gamma for the length, shorter than Gamma for large
- Gap Encoding — turns the sorted IDs of an inverted list into small numbers that the integer codes compress well
- UTF-8 — prefix-free variable-length byte code for Unicode code points
How it fits together#
From prefix-freeness to optimality. A code is only usable without separators if it is prefix-free. Kraft’s inequality characterises the code lengths a prefix-free code can have. Among these, the expected length is bounded below by the entropy , and Shannon’s source coding theorem gives a prefix-free code with . The bound is reached exactly when for all , i.e. when every probability is a negative power of two (Entropy-Optimal Code). This gives the two standard exam proofs:
- Given a code, find the distribution for which it is optimal: set , check and .
- Prove the optimum: minimise subject to with Lagrange Multipliers, which yields .
Integer codes as implicit distributions. Each integer code fixes a length , so by the rule above it is entropy-optimal for . Choosing a code means choosing which numbers you expect to be small:
| Code | Length | Optimal for | Parameter |
|---|---|---|---|
| Unary | (very small numbers) | – | |
| Golomb, | geometric distributions; chosen from the typical gap size | ||
| Elias-Gamma | (polynomial tail) | – | |
| Elias-Delta | heavier tail than Gamma | – |
All four build on the same idea: a self-delimiting (unary or Gamma) part that says how long the rest is, followed by plain binary digits.
Where the numbers come from. The sorted document IDs of an inverted list are large, but their differences are small, especially in the long lists of frequent words (Zipf’s law: a few words account for most occurrences). Gap Encoding turns the list into these differences, which are then written with one of the integer codes. Golomb suits gaps of roughly known size (choose from the list density), Elias codes need no parameter.
UTF-8 as a real-world prefix code. UTF-8 encodes Unicode code points with 1–4 bytes. The leading bits of the first byte (0, 110, 1110, 11110) play the role of the unary length prefix, and continuation bytes start with 10, so the code is prefix-free and self-synchronising. Exam tasks convert code points to bytes and count valid byte sequences (shortest-form rule).
Key Formulas#
- Kraft: (Prefix-Free Code)
- Entropy: (Entropy)
- Optimality: for all (Entropy-Optimal Code)
- Elias-Gamma length: (Elias-Gamma Code)
Exercises & Exam Questions#
SuccessSolution
Encoding: Use 3-bit binary: .
All code lengths are , so the expected code length is .
The entropy of the uniform distribution over 8 symbols is:
Since , the encoding is entropy-optimal.
SuccessSolution
For entropy-optimality, we need , i.e., .
With and :
Verification: . The entropy is:
Expected code length: . Entropy-optimal.
SuccessSolution
- Convert hex to binary:
1F60E=0 0001 1111 0110 0000 1110(17 significant bits) - 17 bits requires a 4-byte UTF-8 sequence:
11110xxx 10xxxxxx 10xxxxxx 10xxxxxx(21 payload bits) - Pad to 21 bits:
000 011111 011000 001110 - Fill the template:
11110+000=1111000010+011111=1001111110+011000=1001100010+001110=10001110
- Convert to hex:
F0 9F 98 8E
Exam occurrences#
| Semester | Task | Points | Subtopics |
|---|---|---|---|
| DBIS Exam WS1314 (not published) | Task 1 | 20pts | Golomb encoding, gap encoding, entropy-optimal code |
| DBIS Exam WS1516 (not published) | Task 2 | 20pts | unary encoding, decode function, entropy-optimality proof |
| DBIS Exam WS1617 (not published) | Task 2 | 20pts | Golomb+log2 prefix, encode function, code length formula |
| DBIS Exam WS1718 (not published) | Task 2 | 20pts | unary encoding function, expected bits, entropy-optimality |
| DBIS Exam WS1819 (not published) | Task 2 | 20pts | prefix-free encoding, Shannon’s theorem, UTF-8 Greek |
| DBIS Exam WS1920 (not published) | Task 2 | 25pts | entropy-optimal encoding, code length proof, UTF-8 emoji |
| DBIS Exam WS2122 (not published) | Task 2 | 25pts | Zipf proof, Elias-Delta, entropy-optimal, UTF-8 |
| DBIS Exam WS2223 (not published) | Task 1 | 20pts | exponential search, Elias-Gamma, Lagrangian |
Recurring patterns:
- Every exam includes an encoding task (usually Task 2, worth 20–25 points)
- Entropy-optimality proofs appear in nearly every exam – either proving a given code is optimal or finding the distribution that makes it optimal
- UTF-8 encoding/decoding appears alongside encodings starting from WS 15/16
- Coding tasks (implement encode/decode functions) appear in most exams, typically ~5pts and limited to 10–15 lines
Lectures#
- DBIS_VL01 > Tokenization — encoding errors in raw text (wrongly decoded UTF-8)
- DBIS_VL10 > Unicode — code points, UTF-8 in detail, URL encoding
- Prefix-free codes, entropy and the integer codes were not lectured in WS 25/26; they come from the past exams of the predecessor course Information Retrieval.
Related Topics#
- Inverted Index and Ranking — gap-encoded inverted lists are what the integer codes compress
- Web Applications — UTF-8 and URL encoding in HTTP and HTML