Lecture7: Huffman Code Adaptive Huffman Code Exeee.guc.edu.eg/Courses/Communications/COMM901...
Transcript of Lecture7: Huffman Code Adaptive Huffman Code Exeee.guc.edu.eg/Courses/Communications/COMM901...
SOURCE CODING PROF. A.M.ALLAM
Adaptive Huffman Code Lecture7: Huffman Code
0 1
1
0
0 1
Ex:
49 50
Ex:
43 44
45 46
47 48
49 50
Then increment node
number 48 to weight 2
instead of 1
Then increment node
number 50 to weight 3
instead of 2
Then increment node
number 51 to weight 5
instead of 4
SOURCE CODING PROF. A.M.ALLAM
Adaptive Huffman Code Lecture7: Huffman Code
Encoding
Initially, the tree at both the encoder and
decoder consists of a single node; the NYT node
The codeword for the very first symbol that
appears is a previously agreed upon fixed code
Send the code for the NYT node, followed by the
previously agreed upon fixed code for the symbol
The code for the NYT node is obtained by
traversing the Huffman tree from the root to the
NYT node
If a symbol to be encoded has a corresponding
node in the tree, then the code for the symbol is
generated by traversing the tree from the root to
the external node corresponding to the symbol
SOURCE CODING PROF. A.M.ALLAM
Ex: Encoding the message [a a r d v a r k], where our alphabet consists of the 26 lowercase
letters of the English alphabet
The total number of nodes in this tree will be =2m-1=2×26−1
= 51, so we start numbering backwards from 51 with the
number of the root node being 51
We begin with only the NYT node with weight 0, nothing
transmitted so far
Input: aardvark
Symbol Code
NYT 0
a
r
d
v
K
Output:
Adaptive Huffman Code Lecture7: Huffman Code
LECTURES 11/4/2017 3
Starting the tree
code
SOURCE CODING PROF. A.M.ALLAM
The first symbol to be encoded is the letter ‘a’
As ‘a’ is the first letter of the alphabets order, k =1. As 1 is less than 20, ‘a’ is encoded as the 5 bit
binary representation of k−1=0, which is 00000
We send the code for the NYT node, by traversing the Huffman tree from the root to the NYT node
(0) followed by the previously agreed upon fixed code for the symbol “a” (00000)
r d v a r k a aInput: Output: 00000
Adaptive Huffman Code Lecture7: Huffman Code
11/4/2017
Encoding the first symbol
Transmission
The NYT node gives birth to an external node corresponding to
the element ‘a’ and new NYT node
The weight of the terminal node should be higher than the
NYT node and since, ‘a’ has occurred once, its external node
has a weight of one, hence, the weight of the NYT node is 1
The parent node also has a weight of 1, as its weight is the sum
of the weights of its offspring
Also we assign the number 49 to the NYT node and 50 to the
terminal node corresponding to the letter ‘a’
The node corresponding to ‘a’ has the highest number (if we
do not consider its parent), so we do not need to swap nodes
Updating
Symbol Code
NYT 0
a 1
r
d
v
k
0 1
1
0
0 1
SOURCE CODING PROF. A.M.ALLAM
After a symbol has been encoded, the external node
corresponding to the symbol is examined to see if it has
the largest node number in its block. If the external
node does not have the largest node number, it is
exchanged with the node that has the largest node
number in the block, as long as the node with the
higher number is not the parent of the of the node
being updated, so no exchange is done, then increment
the weight of the external node and its parent
The second letter to be transmitted is also ‘a’
It is not a new symbol. Its code is obtained by traversing the tree from the root node to the external node
corresponding to it. This traversal consists of a single right branch, therefore, the Huffman code for the symbol
‘a’ is 1
Symbol Code
NYT 0
a 1
r
d
v
k
Output: 000001
Adaptive Huffman Code Lecture7: Huffman Code
11/4/2017 5
Encoding the next symbol
Transmission
Updating
r d v a r k aa Input:
We send the code for the NYT node, by traversing the Huffman tree from the root to the NYT node (0) followed by
the code for the symbol “a” (000001)
0 1
0 1
SOURCE CODING PROF. A.M.ALLAM
Adaptive Huffman Code Lecture7: Huffman Code
11/4/2017 6
Transmission
Encoding the next symbol
We send the code for the NYT node , which is 0 (we obtain it by traversing the tree from the root
to the NYT node), followed by the previously arranged binary representation for ‘r’ . The letter ‘r’
is the 18th letter of the alphabet; therefore, the binary representation of ‘r’ is 10001
The third symbol to be encoded is the letter ‘r’
As ‘r’ is the 18th letter of the alphabets order, , the binary representation of ‘r’ is 10001
d v a r k r aa Input: Output: 000001010001
0 1
SOURCE CODING PROF. A.M.ALLAM
11/4/2017 7
Adaptive Huffman Code Lecture7: Huffman Code
Updating
The NYT node gives birth to an external node corresponding to the element ‘r’ and new NYT
node. The weight of the terminal node should be higher than the NYT node and since, ‘r’ has
occurred once, its external node has a weight of one, hence, the weight of the NYT node is 0. The
parent node also has a weight of 1, as its weight is the sum of the weights of its offspring. Also we
assign the number 47 to the NYT node and 48 to the terminal node corresponding to the letter ‘r’
The node corresponding to ‘r’ has the highest number (if we do not consider its parent), so we do
not need to swap nodes . The parent node is not the root node, hence we go to the grandparent,
if that node has the highest number in its block , then increment its weight , else swap within the
block and increment its weight . Continue till the root node
Symbol Code
NYT 00
a 1
r 01
d
v
k
0 1
1
1
0 1
3
SOURCE CODING PROF. A.M.ALLAM
11/4/2017 8
Adaptive Huffman Code Lecture7: Huffman Code
We send the code for the NYT node , which is 00 (we obtain it by traversing the tree from the
root to the NYT node), followed by the previously arranged binary representation for ‘d’
Transmission
Encoding the next symbol
It is the 4th symbol in the alphabet. As this is less than 20, the 5 bit binary representation of
4−1 = 3 is 00011
The next symbol to be transmitted is ‘d’
v a r k r d aa Input: Output: 0000010100010000011
SOURCE CODING PROF. A.M.ALLAM
The NYT node gives birth to an external node
corresponding to the element ‘d’ and new
NYT node. The weight of the external node
should be higher than the NYT node and
since, ‘d’ has occurred once, its external node
has a weight of one, hence, the weight of the
NYT node is zero. The parent node also has a
weight of one, as its weight is the sum of the
weights of its offspring. Also we assign the
number 45 to the NYT node and 46 to the
terminal node corresponding to the letter ‘d’
Adaptive Huffman Code Lecture7: Huffman Code
9
Updating
2
4
The node corresponding to ‘d’ has the highest
number (if we do not consider its parent), so we
do not need to swap nodes . The parent node is
not the root node, hence we go to the
grandparent, if that node has the highest number
, then increment its weight , else swap within the
block and increment its weight . Continue till the
root node
SOURCE CODING PROF. A.M.ALLAM
Symbol Code
NYT 000
a 1
r 01
d 001
v
k
Adaptive Huffman Code Lecture7: Huffman Code
11/4/2017 10
SOURCE CODING PROF. A.M.ALLAM
The next symbol to be transmitted is ‘v’
As this is the first appearance of this symbol,. It is the 22nd symbol in the alphabet. As this is
greater than 20, the 4 bit binary representation of 22−10−1 = 11 is 1011
Adaptive Huffman Code Lecture7: Huffman Code
11
Transmission
Encoding the next symbol
a r k r d v aa Input:
We send the code for the NYT node , which is
000 (we obtain it by traversing the tree from
the root to the NYT node), followed by the
previously arranged binary representation for
‘v’ which is 1011
Output: 00000101000100000110001011
SOURCE CODING PROF. A.M.ALLAM
Adaptive Huffman Code Lecture7: Huffman Code
12
Updating
The NYT node gives birth to an external
node corresponding to the element ‘v’ and
new NYT node. The weight of the terminal
node should be higher than the NYT node
and since, ‘v’ has occurred once, its
external node has a weight of one, hence,
the weight of the NYT node is 0. The
internal node also has a weight of 1, as its
weight is the sum of the weights of its
offspring. Also we assign the number 43 to
the NYT node and 44 to the terminal node
corresponding to the letter ‘v’. The node
corresponding to ‘v’ has the highest
number (if we do not consider its parent),
so we do not need to swap nodes
SOURCE CODING PROF. A.M.ALLAM
13
-We examine the grandparent node of v (node 47) to see if it has the largest number in its block. As
it does not, we swap it with node 48, which has the largest number in its block. We then increment
node 48 and move to its parent, which is node 49
Adaptive Huffman Code Lecture7: Huffman Code
1 2
In the block containing node 49, the largest number belongs to node 50. Therefore, we swap
nodes 49 and 50 and then increment node 50. We then move to the parent node of node 50, which
is node 51. As this is the root node, all we do is increment node 51
SOURCE CODING PROF. A.M.ALLAM
11/4/2017 14 LECTURES
Adaptive Huffman Code Lecture7: Huffman Code
Symbol Code
NYT 1100
a 0
r 10
d 111
v 1101
k
50 49
47 48
46 45
44 43
4
2 3
5
SOURCE CODING PROF. A.M.ALLAM
11/4/2017 15
Adaptive Huffman Code
-The next letter to be encoded is ‘a’ . It has an external node , we simply traverse the tree from the
root node to the external node corresponding to ‘a’ which consists of a single left branch.
Therefore, the Huffman code for the symbol ‘a’ is 0, send it . No swap
Symbol Code
NYT 1100
a 0
r 10
d 111
v 1101
k
48 47
50 49
Lecture7: Huffman Code
r k r d v a aa Input:
Output: 0000010100010000011000101111000
SOURCE CODING PROF. A.M.ALLAM
11/4/2017 16
Adaptive Huffman Code
Symbol Code
NYT 1100
a 0
r 10
d 001
v 1101
k
48 47
50 49
Lecture7: Huffman Code
-The next letter to be encoded is ‘r’ . It has an external node , we simply traverse the tree from the
root node to the external node corresponding to ‘r’ which consists of a signle right branch followed
by a single left branch. Therefore, the Huffman code for the symbol ‘r’ is 10, send it . No swap
k r d v a r aa Input:
Output: 0000010100010000011000101111000110010
SOURCE CODING PROF. A.M.ALLAM
17
-The next symbol to be transmitted is ‘k’. As this is the first appearance of this symbol, we send
the code for the NYT node , which is 1100 (we obtain it by traversing the tree from the root to
the NYT node), followed by the previously arranged binary representation for ‘k’ . It is the 11th
symbol in the alphabet. the 5 bit binary representation of 11−1 = 10 is 01010
Adaptive Huffman Code
Symbol Code
NYT 11000
a 0
r 10
d 111
v 1101
k
Lecture7: Huffman Code
r d v a r k aa Input:
1
48 47
50 49
Output: 0000010100010000011000101111000110010110001010
SOURCE CODING PROF. A.M.ALLAM
11/4/2017 18
Adaptive Huffman Code
50 49
Lecture7: Huffman Code
r d v a r k aa Input:
Symbol Code
NYT 11100
a 0
r 10
d 110
v 1111
k 11101
Output: 0000010100010000011000101111000110010110001010