LectureNotes01 PDF
LectureNotes01 PDF
Tara Javidi
These lecture notes were originally developed by late Prof. J. K. Wolf.
UC San Diego
Spring 2014
1 / 16
Source Coding
• Source Coding
• Basic Definitions
Larger Alphabet
Huffman Codes
Class Work
2 / 16
Source Coding: A Simple Example
Source Coding
Back to our simple example of a source:
• Source Coding
• Basic Definitions
1 1 1 1
Larger Alphabet P[A] = , P[B] = , P[C] = , P[D] =
Huffman Codes
2 4 8 8
Class Work
Assumptions
1. One must be able to uniquely recover the source sequence from
the binary sequence
2. One knows the start of the binary sequence at the receiver
3. One would like to minimize the average number of binary digits
per source letter
3 / 16
Source Coding: A Simple Example
Source Coding
Back to our simple example of a source:
• Source Coding
• Basic Definitions
1 1 1 1
Larger Alphabet P[A] = , P[B] = , P[C] = , P[D] =
Huffman Codes
2 4 8 8
Class Work
1. A → 00
B → 01 ABAC → 00010010 → ABAC
C → 10
D → 11 L=2
Assumptions
1. One must be able to uniquely recover the source sequence from
the binary sequence
2. One knows the start of the binary sequence at the receiver
3. One would like to minimize the average number of binary digits
per source letter
3 / 16
Source Coding: A Simple Example
Source Coding
Back to our simple example of a source:
• Source Coding
• Basic Definitions
1 1 1 1
Larger Alphabet P[A] = , P[B] = , P[C] = , P[D] =
Huffman Codes
2 4 8 8
Class Work
1. A→0 AABD → 00110 → CBBA
B→1 (→ CBD)
C → 10 (→ AABD)
D → 11 L = 45
This code is useless. Why?
Assumptions
1. One must be able to uniquely recover the source sequence from
the binary sequence
2. One knows the start of the binary sequence at the receiver
3. One would like to minimize the average number of binary digits
per source letter
3 / 16
Source Coding: A Simple Example
Source Coding
Back to our simple example of a source:
• Source Coding
• Basic Definitions
1 1 1 1
Larger Alphabet P[A] = , P[B] = , P[C] = , P[D] =
Huffman Codes
2 4 8 8
Class Work
1. A→0 ABACD → 0100110111 → ABACD
B → 10
C → 110
7
D → 111 L= 4
Minimum length code satisfying Assumptions 1 and 2!
Assumptions
1. One must be able to uniquely recover the source sequence from
the binary sequence
2. One knows the start of the binary sequence at the receiver
3. One would like to minimize the average number of binary digits
per source letter
3 / 16
Source Coding: A Simple Example
Source Coding
Back to our simple example of a source:
• Source Coding
• Basic Definitions
1 1 1 1
Larger Alphabet P[A] = , P[B] = , P[C] = , P[D] =
Huffman Codes
2 4 8 8
Class Work
1. A→0 ABACD → 0100110111 → ABACD
B → 10
C → 110
7
D → 111 L= 4
Minimum length code satisfying Assumptions 1 and 2!
Assumptions
1. One must be able to uniquely recover the source sequence from
the binary sequence
2. One knows the start of the binary sequence at the receiver
3. One would like to minimize the average number of binary digits
per source letter
3 / 16
Source Coding: Basic Definitions
Source Coding
Codeword (aka Block Code)
• Source Coding
• Basic Definitions Each source symbol is represented by some sequence of coded
Larger Alphabet symbols called a code word
Huffman Codes Non-Singular Code
Class Work
Code words are distinct
Uniquely Decodable (U.D.) Code
Every distinct concatenation of m code words is distinct for every
finite m
Instantaneous Code
A U.D. Code where we can decode each code word without seeing
subsequent code words
4 / 16
Source Coding: Basic Definitions
Source Coding
Example
• Source Coding
• Basic Definitions
Back to the simple case of 4-letter DMS:
Larger Alphabet
Huffman Codes
Class Work
Source Symbols Code 1 Code 2 Code 3 Code 4
A 0 00 0 0
B 1 01 10 01
C 00 10 110 011
D 01 11 111 111
4 / 16
Source Coding
Larger Alphabet
• Example 1
Huffman Codes
Class Work
5 / 16
Example 1: 3 letter DMS
Source Coding
Source Symbols Probability U.D. Code
Larger Alphabet
• Example 1 A .5 0
L1 = 1.5 (Bits / Symbol)
Huffman Codes B .35 10
Class Work C .85 11
6 / 16
Example 1: 3 letter DMS
Source Coding
Source Symbols Probability U.D. Code
Larger Alphabet
• Example 1 A .5 0
L1 = 1.5 (Bits / Symbol)
Huffman Codes B .35 10
Class Work C .85 11
Source Coding
In other words,
Larger Alphabet
• Example 1 1. It is more efficient to build a code for 2 source symbols!
Huffman Codes 2. Is it possible to decrease the length more and more by
Class Work
increasing the alphabet size?
To see the answer to the above question, it is useful if we can say
precisely characterize the best code. The codes given above are
Huffman Codes. The procedure for making Huffman Codes will be
described next.
6 / 16
Source Coding
Larger Alphabet
Huffman Codes
• Binary Huffman Code
• Example I
• Example II
• Optimality
• Shannon-Fano Codes
Class Work
Minimizing average length
7 / 16
Binary Huffman Codes
Source Coding
Binary Huffman Codes
Larger Alphabet
1. Order probabilities - Highest to Lowest
Huffman Codes
• Binary Huffman Code
2. Add two lowest probabilities
• Example I 3. Reorder probabilities
• Example II
• Optimality 4. Break ties in any way you want
• Shannon-Fano Codes
Class Work
8 / 16
Binary Huffman Codes
Source Coding
Binary Huffman Codes
Larger Alphabet
1. Order probabilities - Highest to Lowest
Huffman Codes
• Binary Huffman Code
2. Add two lowest probabilities
• Example I 3. Reorder probabilities
• Example II
• Optimality 4. Break ties in any way you want
• Shannon-Fano Codes
8 / 16
Binary Huffman Codes
Source Coding
Binary Huffman Codes
Larger Alphabet
1. Order probabilities - Highest to Lowest
Huffman Codes
• Binary Huffman Code
2. Add two lowest probabilities
• Example I 3. Reorder probabilities
• Example II
• Optimality 4. Break ties in any way you want
• Shannon-Fano Codes
5. Assign 0 to top branch and 1 to bottom branch (or vice versa)
Class Work
6. Continue until we have only one probability equal to 1
7. L = Sum of probabilities of combined nodes (i.e., the circled
ones)
8 / 16
Binary Huffman Codes
Source Coding
Binary Huffman Codes
Larger Alphabet
1. Order probabilities - Highest to Lowest
Huffman Codes
• Binary Huffman Code
2. Add two lowest probabilities
• Example I 3. Reorder probabilities
• Example II
• Optimality 4. Break ties in any way you want
• Shannon-Fano Codes
5. Assign 0 to top branch and 1 to bottom branch (or vice versa)
Class Work
6. Continue until we have only one probability equal to 1
7. L = Sum of probabilities of combined nodes (i.e., the circled
ones)
8 / 16
Huffman Coding: Example
Source Coding
Example Continued
Larger Alphabet
order
Huffman Codes 1. {.1, .2, .15, .3, .25} → {.3, .25, .2, .15, .1}
• Binary Huffman Code
• Example I
• Example II
• Optimality
• Shannon-Fano Codes
Class Work
9 / 16
Huffman Coding: Example
Source Coding
Example Continued
Larger Alphabet
order
Huffman Codes 1. {.1, .2, .15, .3, .25} → {.3, .25, .2, .15, .1}
• Binary Huffman Code
• Example I
• Example II
• Optimality
• Shannon-Fano Codes
Class Work
9 / 16
Huffman Coding: Example
Source Coding
Example Continued
Larger Alphabet
order
Huffman Codes 1. {.1, .2, .15, .3, .25} → {.3, .25, .2, .15, .1}
• Binary Huffman Code
• Example I Or
• Example II
• Optimality
• Shannon-Fano Codes
Class Work
9 / 16
Huffman Coding: Tie Breaks
Source Coding
In the last example, the two ways of breaking the tie led to two
Larger Alphabet
different codes with the same set of code lengths. This is not always
Huffman Codes
• Binary Huffman Code
the case — Sometimes we get different codes with different code
• Example I lengths.
• Example II
• Optimality
• Shannon-Fano Codes
EXAMPLE:
Class Work
10 / 16
Huffman Coding: Optimal Average Length
Source Coding
• Binary Huffman Code will have the shortest average length as
Larger Alphabet
compared with any U.D. Code for set of probabilities (No U.D. will
Huffman Codes
• Binary Huffman Code
have a shorter average length).
• Example I
• Example II ◦ The proof that a Binary Huffman Code is optimal — that is,
• Optimality
• Shannon-Fano Codes has the shortest average code word length as compared with
Class Work any U.D. code for that the same set of probabilities — is
omitted.
◦ However, we would like to mention that the proof is based on
the fact that in the process of constructing a Huffman Code
for that set of probabilities other codes are formed for other
sets of probabilities, all of which are optimal.
11 / 16
Shannon-Fano Codes
Source Coding
S HANNON - FANO C ODES is another binary coding technique to
Larger Alphabet
construct U.D. codes (not necessarily optimum!)
Huffman Codes
• Binary Huffman Code 1. Order probabilities in decreasing order.
• Example I 2. Partition into 2 sets that one as close to equally probable as
• Example II
• Optimality possible. Label top set with a ”‘0”’ and bottom set with a ”‘1”’.
• Shannon-Fano Codes
3. Continue using step 2 over and over
Class Work
12 / 16
Shannon-Fano Codes
Source Coding
S HANNON - FANO C ODES is another binary coding technique to
Larger Alphabet
construct U.D. codes (not necessarily optimum!)
Huffman Codes
• Binary Huffman Code 1. Order probabilities in decreasing order.
• Example I 2. Partition into 2 sets that one as close to equally probable as
• Example II
• Optimality possible. Label top set with a ”‘0”’ and bottom set with a ”‘1”’.
• Shannon-Fano Codes
3. Continue using step 2 over and over
Class Work
.4 ⇒ 0
.3 ⇒ 1 0
.5 ⇒ 0
.1 ⇒ 1 1 0 0
.2 ⇒ 1 0
.05 ⇒ 1 1 0 1
.15 ⇒ 1 1 0
.05 ⇒ 1 1 1 0
.15 ⇒ 1 1 1
.05 ⇒ 1 1 1 1 0
.05 ⇒ 1 1 0 1 1
L =? L = 2.3
Larger Alphabet
Huffman Codes
Class Work
• More Examples
• More Examples
• More Examples
More Examples
13 / 16
Binary Huffman Codes
Source Coding
Construct binary Huffman and Shannon-Fano codes where:
Larger Alphabet
EXAMPLE 1: (p1 , p2 , p3 , p4 ) = ( 12 , 14 , 18 , 81 )
Huffman Codes
Class Work
• More Examples
• More Examples
• More Examples
14 / 16
Large Alphabet Size
Source Coding
Example 3: Consider a binary Source {A, B}
Larger Alphabet
(p1 , p2 ) = (.9, .1). Now construct a series of Huffman Codes and
Huffman Codes
series of Shannon-Fano Codes, by encoding N source symbols at a
Class Work
• More Examples time for N = 1, 2, 3, 4.
• More Examples
• More Examples
15 / 16
Shannon-Fano codes are suboptimal !
Source Coding
Example 3: Construct a Shannon-Fano code:
Larger Alphabet 2 × .25 0 0
Huffman Codes 2 × .20 0 1
Class Work .6 3 × .15 1 0 0
• More Examples
• More Examples
.7 3 × .10 1 0 1
• More Examples .75 4 × .05 1 1 0 0
.8 5 × .05 1 1 0 1 0
.85 5 × .05 1 1 0 1 1
.9 4 × .05 1 1 1 0
5 × .04 1 1 1 1 0
6 × .03 1 1 1 1 1 0
6 × .03 1 1 1 1 1 1
L = 3.11
Compare this with a binary Huffman Code.
16 / 16