Meaning and assumptions
A prefix code can be read immediately, symbol by symbol, without separators because no codeword starts another. Kraft’s inequality tests whether a proposed list of lengths admits a binary prefix code. Huffman constructs a prefix code with minimum expected length for specified symbol probabilities.[1]
Binary alphabet, positive integer codeword lengths and a specified finite source alphabet. Probabilities are nonnegative and sum to one. Huffman optimality here concerns symbol-by-symbol prefix codes; dictionary, block and arithmetic coding are different methods.[1]
Why the approach works
A length-l word occupies fraction2−ˡ of all possible continuations in a full binary tree. Disjoint leaves of a prefix tree cannot occupy more than the whole tree, so the Kraft sum is≤1. Conversely the inequality guarantees existence of a suitable prefix code, not that an arbitrary written list is already prefix free. Huffman merges the two least probable symbols, replaces them with their summed probability and repeats to the root. Backtracking assigns0 and1 at each branch. Ties may create different equally good codes; expected length remains the relevant cost.[1]
Two complete worked examples
Merge four symbols step by step
- Given
- A, B, C, D have probabilities0.4,0.3,0.2,0.1.
- Find
- The code needs1.9bits per symbol on average,5% fewer payload bits than a fixed2-bit code.
Merge the two smallest weights first.
Merge the new0.3 node withB, then withA.
Evaluate one possible bit assignment and expected length.
Result: The code needs1.9bits per symbol on average,5% fewer payload bits than a fixed2-bit code.
Check: Kraft sum1/2+1/4+1/8+1/8=1. The actual bitwords are prefix free. The5% comparison excludes tree-transmission and header overhead.
Reject an impossible length list
- Given
- Three binary symbols are assigned lengths1,1,2.
- Find
- No binary prefix code exists with exactly these lengths.
Determine each occupied tree fraction.
Add fractions and compare with one.
Result: No binary prefix code exists with exactly these lengths.
Check: Two length-one words already use0 and1. Any third longer word must start with one of them.
Recall the method later with the formula’s flashcards.
10 flashcards · English materials
Already have an account? Sign in for flashcardsAvailable from Essential. A ready-made formula set, no AI credits required. Article exercises are not all included.
Five exercises with solutions
Calculate · Application
Find the Kraft sum for lengths2,2,2,2 and give a prefix code.
Show hint
State the approach and assumptions before substituting.
Learning goal: Find the Kraft sum for lengths2,2,2,2 and give a prefix code.
Show solution and calculation
K=4·2−2=1.
One choice is00,01,10,11.
Calculate · Application
Lengths1,2,3,3 with probabilities1/2,1/4,1/8,1/8. Find mean length.
Show hint
State the approach and assumptions before substituting.
Learning goal: Lengths1,2,3,3 with probabilities1/2,1/4,1/8,1/8. Find mean length.
Show solution and calculation
l̄=1/2+2/4+3/8+3/8=1.75bits/symbol.
For these dyadic probabilities entropy is also1.75bits/symbol.
Error analysis · Application
Are0,01,11 prefix free even though their Kraft sum equals one?
Show hint
State the approach and assumptions before substituting.
Learning goal: Are0,01,11 prefix free even though their Kraft sum equals one?
Show solution and calculation
No:0 is a prefix of01.
Kraft guarantees some code with lengths1,2,2, such as0,10,11; it does not validate an arbitrary actual list.
Interpret · Application
Why does Huffman first combine the two smallest weights?
Show hint
State the approach and assumptions before substituting.
Learning goal: Why does Huffman first combine the two smallest weights?
Show solution and calculation
The least probable symbols can be chosen as deepest siblings in an optimal tree.
Merging them reduces the problem by one symbol while preserving an optimal continuation.
Transfer · Challenge
1000symbols use1.9rather than2bits on average. An extra header needs200bits. Is the file smaller?
Show hint
State the approach and assumptions before substituting.
Learning goal: 1000symbols use1.9rather than2bits on average. An extra header needs200bits. Is the file smaller?
Show solution and calculation
Payload saves100bits, but the header costs200.
Total2100instead of2000bits:100bits larger.
Average coding gain does not guarantee smaller short files.
Check your understanding with the formula’s reference test.
8 test questions · English materials
Already have an account? Sign in for the testAvailable from Essential. A ready-made formula set, no AI credits required. Article exercises are not all included.
Related next steps
Related foundations: Shannon entropy, Hamming distance between bit strings.
Sources for this explanation
Quanta · Updated November 6, 2026
Editorial responsibility: Quanta Study editorial team
Review scope
This explanation, 2 worked examples and 5 exercises are available in German and English. Structure, source references and mathematical notation undergo automated checks. Calculations, model assumptions and both language versions receive a separate AI review.
An independent, named human subject review has not yet been documented. Our exercise numbers are worked examples, rather than measurements published by the cited sources.
Editorial responsibility, sources and methodsChange history
- Locally authored DE/EN article with two original examples, five exercises, model limits and a native diagram.
Cite this reference page
Quanta. Prefix codes, Kraft inequality and Huffman coding. Updated November 6, 2026. https://quanta-study.de/en/formula-sheet/praefixcodes-kraft-huffman
When citing a specific scientific statement, also cite the specialist source linked to that statement.