Multimedia & Machine Learning Algorithms · Multimedia Algorithms
Lossless Compression — Entropy, Shannon, Huffman & LZW
Explanation
Intuition — Why Compress?
In an English text file, 'e' appears in 13% of characters while 'z' appears in only 0.07% — why represent both with 8 bits? Assigning shorter codes to frequent symbols and longer codes to rare ones saves space. This is the essence of lossless compression: a representation that allows perfect reconstruction without losing a single bit.
Shannon Entropy
Suppose a source emits symbols from an alphabet with probabilities .
Definition (Shannon Entropy):
measures the average uncertainty per symbol, or equivalently the minimum average bits per symbol needed to encode the source.
Properties:
- , with equality only when one symbol is certain ().
- is maximized when all symbols are equiprobable: .
- For binary: bit.
Shannon's Source Coding Theorem
Let be the average code length per symbol of a lossless code. Then:
- Lower bound (for any uniquely decodable code):
- Upper bound (for Huffman / optimal prefix codes only):
Meaning: you cannot compress below entropy (for any code). An arbitrary code can be far longer than — only Huffman coding guarantees .
Huffman Coding
Idea: Build a binary tree bottom-up — always merge the two symbols/subtrees with the lowest probability.
Algorithm:
1. Sort symbols by probability ascending.
2. Create a leaf node for each symbol.
3. Repeat until one node remains: remove two lowest-probability nodes, create a parent with combined probability.
4. Assign 0/1 to each branch — each symbol's code is the path from root to its leaf.
Key property: The result is a prefix code: no codeword is a prefix of another — unambiguous decoding without separators.
Optimality: Huffman produces the binary prefix code with minimum average length.
LZW (Lempel-Ziv-Welch)
Unlike Huffman (which codes symbol by symbol), LZW codes sequences and builds a dynamic dictionary.
Principle:
- Start with an initial dictionary of all single symbols.
- At each step, extend the current string w by the next character c:
- If is already in the dictionary continue (w w+c).
- Otherwise output code(w), add to dictionary, reset to w c.
Critical advantage: Encoder and decoder build the same dictionary independently — no need to transmit the dictionary! LZW underlies GIF and TIFF formats.
Arithmetic Coding
Advanced idea: Encodes the entire message as a single number in . Each symbol subdivides the current interval into sub-intervals proportional to symbol probabilities, marking the chosen symbol's sub-interval.
Advantage: Can approach without the ceiling that limits single-symbol Huffman. Used in JPEG 2000 and H.264's CABAC entropy coder.
Interactive Visualization
Loading visualization
Worked Examples
Practice
Exercise 1 of 6
1
What does Huffman coding guarantee compared to other prefix-code methods?
★Quiz
Test your knowledge from this lesson
A mix of questions from the lesson — each attempt varies slightly
Answer at least 4 out of 5 questions correctly to pass (70%)