Prefix-Free Code
Also Prefix Code
A code is prefix-free if no codeword is a prefix of another codeword, so a concatenation of codewords can be decoded uniquely from left to right without delimiters.
Formula#
Kraft’s inequality. For symbols with codeword lengths :
Properties#
- Unique decodability without delimiters. Read bits from left to right until they form a codeword, output its symbol, and continue. Because no codeword is a prefix of another, the first match is the only possible one, so the decoding is unique.
- Lengths constrain each other. Kraft’s inequality is the only restriction: short codewords “use up” of the available budget . Codes with waste nothing; this is the constraint under which the past exams minimize the expected code length (Entropy-Optimal Code > Properties).
- Typical proof that a code is prefix-free: show that the end of each codeword can be recognized from the codeword itself, e.g. the first
1in the Unary Code, or the number of leading zeroes in the Elias-Gamma Code, which fixes the total length. - UTF-8 is prefix-free on the byte level: the leading bits of the first byte (
0,110,1110,11110) determine the number of bytes.
Example#
- , , , is prefix-free, and . The bit string
01000101100111decodes uniquely as0|10|0|0|10|110|0|111. - , , , , is prefix-free: .
- is not prefix-free ( is a prefix of ), and
010is ambiguous:0|10or01|0.
Sources#
- DBIS Exam WS1516 (not published) Task 2.1 (prove that the unary code is prefix-free), DBIS Exam WS1819 (not published) Task 2.1, DBIS Exam WS1920 (not published) Tasks 2.1–2.2, DBIS Exam WS2122 (not published) Task 2.3
- Not lectured in WS 25/26 (the course was “Information Retrieval” when these exams were set).