Study Vault Search notes, aliases and full text
Graph
TopicDBISAI-written · not yet proofread

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#

  1. Prefix-Free Code — the property that makes a code decodable without delimiters; Kraft’s inequality says which code lengths are possible
  2. Entropy — lower bound on the expected code length of any prefix-free code
  3. Entropy-Optimal Code — code whose expected length equals the entropy; exists iff all pi=2−Lip_i = 2^{-L_i}
  4. Lagrange Multipliers — proof tool: minimising ∑ipiLi\sum_i p_i L_i under the Kraft constraint gives Li=log⁡2(1/pi)L_i = \log_2(1/p_i)
  5. Unary Code — simplest integer code, optimal for Pr⁡(X=x)=2−x\Pr(X = x) = 2^{-x}, building block of Golomb and Elias codes
  6. Golomb Code — unary quotient + binary remainder with modulus MM, for geometrically distributed gaps
  7. Elias-Gamma Code — unary length prefix + binary, no parameter, 2⌊log⁡2x⌋+12\lfloor \log_2 x \rfloor + 1 bits
  8. Elias-Delta Code — Elias-Gamma for the length, shorter than Gamma for large xx
  9. Gap Encoding — turns the sorted IDs of an inverted list into small numbers that the integer codes compress well
  10. 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 ∑i2−Li≤1\sum_i 2^{-L_i} \le 1 characterises the code lengths a prefix-free code can have. Among these, the expected length Lˉ=∑ipiLi\bar L = \sum_i p_i L_i is bounded below by the entropy H(X)=−∑ipilog⁡2piH(X) = -\sum_i p_i \log_2 p_i, and Shannon’s source coding theorem gives a prefix-free code with Lˉ≤H(X)+1\bar L \le H(X) + 1. The bound Lˉ=H(X)\bar L = H(X) is reached exactly when Li=log⁡2(1/pi)L_i = \log_2(1/p_i) for all ii, 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 pi=2−Lip_i = 2^{-L_i}, check ∑ipi=1\sum_i p_i = 1 and Lˉ=H(X)\bar L = H(X).
  • Prove the optimum: minimise ∑ipiLi\sum_i p_i L_i subject to ∑i2−Li=1\sum_i 2^{-L_i} = 1 with Lagrange Multipliers, which yields pi=2−Lip_i = 2^{-L_i}.

Integer codes as implicit distributions. Each integer code fixes a length L(x)L(x), so by the rule above it is entropy-optimal for Pr⁡(X=x)=2−L(x)\Pr(X = x) = 2^{-L(x)}. Choosing a code means choosing which numbers you expect to be small:

CodeLength L(x)L(x)Optimal forParameter
Unaryxx2−x2^{-x} (very small numbers)–
Golomb, M=2kM = 2^k⌊x/M⌋+1+k\lfloor x/M \rfloor + 1 + kgeometric distributions; MM chosen from the typical gap sizeMM
Elias-Gamma2⌊log⁡2x⌋+12\lfloor \log_2 x \rfloor + 12−(2⌊log⁡2x⌋+1)2^{-(2\lfloor \log_2 x \rfloor + 1)} (polynomial tail)–
Elias-Delta⌊log⁡2x⌋+2⌊log⁡2(⌊log⁡2x⌋+1)⌋+1\lfloor \log_2 x \rfloor + 2\lfloor \log_2(\lfloor \log_2 x \rfloor + 1) \rfloor + 1heavier 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 MM 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: ∑i2−Li≤1\sum_i 2^{-L_i} \le 1 (Prefix-Free Code)
  • Entropy: H(X)=−∑ipilog⁡2piH(X) = -\sum_i p_i \log_2 p_i (Entropy)
  • Optimality: Lˉ=H(X)  ⟺  pi=2−Li\bar L = H(X) \iff p_i = 2^{-L_i} for all ii (Entropy-Optimal Code)
  • Elias-Gamma length: 2⌊log⁡2x⌋+12\lfloor \log_2 x \rfloor + 1 (Elias-Gamma Code)

Exercises & Exam Questions#

SuccessSolution

Encoding: Use 3-bit binary: 1=000,2=001,…,8=1111 = 000, 2 = 001, \ldots, 8 = 111.

All code lengths are Li=3L_i = 3, so the expected code length is Lˉ=3\bar{L} = 3.

The entropy of the uniform distribution over 8 symbols is:

H(X)=−8⋅18⋅log⁡2 ⁣(18)=log⁡28=3H(X) = -8 \cdot \frac{1}{8} \cdot \log_2\!\left(\frac{1}{8}\right) = \log_2 8 = 3

Since Lˉ=H(X)=3\bar{L} = H(X) = 3, the encoding is entropy-optimal.

SuccessSolution

For entropy-optimality, we need Li=log⁡2(1/pi)L_i = \log_2(1/p_i), i.e., pi=2−Lip_i = 2^{-L_i}.

With La=Lb=3L_a = L_b = 3 and Lc=Ld=Le=2L_c = L_d = L_e = 2:

pa=pb=1/8,pc=pd=pe=1/4p_a = p_b = 1/8, \quad p_c = p_d = p_e = 1/4

Verification: ∑pi=2/8+3/4=1/4+3/4=1\sum p_i = 2/8 + 3/4 = 1/4 + 3/4 = 1. The entropy is:

H=2⋅18⋅3+3⋅14⋅2=34+32=94H = 2 \cdot \frac{1}{8} \cdot 3 + 3 \cdot \frac{1}{4} \cdot 2 = \frac{3}{4} + \frac{3}{2} = \frac{9}{4}

Expected code length: Lˉ=2⋅18⋅3+3⋅14⋅2=94=H(X)\bar{L} = 2 \cdot \frac{1}{8} \cdot 3 + 3 \cdot \frac{1}{4} \cdot 2 = \frac{9}{4} = H(X). Entropy-optimal.

SuccessSolution
  1. Convert hex to binary: 1F60E = 0 0001 1111 0110 0000 1110 (17 significant bits)
  2. 17 bits requires a 4-byte UTF-8 sequence: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx (21 payload bits)
  3. Pad to 21 bits: 000 011111 011000 001110
  4. Fill the template:
    • 11110 + 000 = 11110000
    • 10 + 011111 = 10011111
    • 10 + 011000 = 10011000
    • 10 + 001110 = 10001110
  5. Convert to hex: F0 9F 98 8E

Exam occurrences#

SemesterTaskPointsSubtopics
DBIS Exam WS1314 (not published)Task 120ptsGolomb encoding, gap encoding, entropy-optimal code
DBIS Exam WS1516 (not published)Task 220ptsunary encoding, decode function, entropy-optimality proof
DBIS Exam WS1617 (not published)Task 220ptsGolomb+log2 prefix, encode function, code length formula
DBIS Exam WS1718 (not published)Task 220ptsunary encoding function, expected bits, entropy-optimality
DBIS Exam WS1819 (not published)Task 220ptsprefix-free encoding, Shannon’s theorem, UTF-8 Greek
DBIS Exam WS1920 (not published)Task 225ptsentropy-optimal encoding, code length proof, UTF-8 emoji
DBIS Exam WS2122 (not published)Task 225ptsZipf proof, Elias-Delta, entropy-optimal, UTF-8
DBIS Exam WS2223 (not published)Task 120ptsexponential 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.

Search

Type In
↑ ↓ move ↵ open ⇥ next section Ctrl↵ results page Prefix # to search text only