Dynamic Huffman Coding

19
Dynamic Huffman Dynamic Huffman Coding Coding Computer Networks Computer Networks Assignment Assignment

description

Dynamic Huffman Coding. Computer Networks Assignment. T. Stage 1 (First occurrence of t ) r / \ 0 t (1) Order: 0, t (1) * r represents the root - PowerPoint PPT Presentation

Transcript of Dynamic Huffman Coding

Page 1: Dynamic Huffman Coding

Dynamic Huffman Dynamic Huffman CodingCoding

Computer Networks Computer Networks AssignmentAssignment

Page 2: Dynamic Huffman Coding

2

TT Stage 1 (First occurrence of Stage 1 (First occurrence of t t ) ) rr / \/ \ 0 0 tt(1)(1)

Order: 0,Order: 0,tt(1)(1) ** r represents the root r represents the root** 0 represents the null node 0 represents the null node** t(1) denotes the occurrence of t(1) denotes the occurrence of TT with a frequency with a frequency

of 1 of 1

Page 3: Dynamic Huffman Coding

3

TETE

Stage 2 (First occurrence of Stage 2 (First occurrence of ee)) rr / \/ \ 1 1 tt(1)(1) / \/ \ 0 0 ee(1)(1)

Order: 0,Order: 0,ee(1),1,(1),1,tt(1)(1)

Page 4: Dynamic Huffman Coding

4

TENTEN Stage 3 (First occurrence of Stage 3 (First occurrence of nn ) ) rr / \/ \ 2 2 tt(1)(1) / \/ \ 1 1 ee(1)(1) / \ / \ 0 0 nn(1)(1)

Order: 0,Order: 0,nn(1),1,(1),1,ee(1),2,(1),2,tt(1) : (1) : MisfitMisfit

Page 5: Dynamic Huffman Coding

5

Reorder: TENReorder: TEN

rr / \/ \ tt(1) 2 (1) 2 / \/ \ 1 1 ee(1)(1) / \/ \ 0 0 nn(1)(1)

Order: 0,Order: 0,nn(1),1,(1),1,ee(1),(1),tt(1),2(1),2

Page 6: Dynamic Huffman Coding

6

TENNTENN Stage 4 ( Repetition of Stage 4 ( Repetition of nn ) ) rr / \/ \ tt(1) 3(1) 3 / \/ \ 2 2 ee(1)(1) / \/ \ 0 0 nn(2)(2)

Order: 0,Order: 0,nn(2),2,(2),2,ee(1),(1),tt(1),3 : (1),3 : MisfitMisfit

Page 7: Dynamic Huffman Coding

7

Reorder: TENNReorder: TENN

rr / \/ \ nn(2) 2(2) 2 / \/ \ 1 1 ee(1)(1) / \/ \ 0 0 tt(1)(1) Order: 0,Order: 0,tt(1),1,(1),1,ee(1),(1),nn(2),2 (2),2 tt(1),(1),nn(2) are swapped(2) are swapped

Page 8: Dynamic Huffman Coding

8

TENNETENNE Stage 5 (Repetition of Stage 5 (Repetition of ee ) ) rr / \/ \ nn(2) 3(2) 3 / \/ \ 1 1 ee(2)(2) / \/ \ 0 0 tt(1)(1)

Order: 0,Order: 0,tt(1),1,(1),1,ee(2),(2),nn(2),3 (2),3

Page 9: Dynamic Huffman Coding

9

TENNESTENNES Stage 6 (First occurrence of s)Stage 6 (First occurrence of s) rr / \/ \ nn(2) 4(2) 4 / \/ \ 2 2 ee(2)(2) / \/ \ 1 1 tt(1)(1) / \/ \ 0 0 ss(1)(1)

Order: 0,Order: 0,ss(1),1,(1),1,tt(1),2,(1),2,ee(2),(2),nn(2),4(2),4

Page 10: Dynamic Huffman Coding

10

TENNESSTENNESS Stage 7 (Repetition of Stage 7 (Repetition of ss)) rr / \/ \ nn(2) 5(2) 5 / \/ \ 3 3 ee(2)(2) / \/ \ 2 2 tt(1)(1) / \/ \ 0 0 ss(2)(2)

Order: 0,Order: 0,ss(2),2,(2),2,tt(1),3,(1),3,ee(2),(2),nn(2),5 : (2),5 : MisfitMisfit

Page 11: Dynamic Huffman Coding

11

Reorder: TENNESSReorder: TENNESS

rr / \/ \ n(2) 5n(2) 5 / \/ \ 3 e(2)3 e(2) / \/ \ 1 s (2)1 s (2) / \/ \ 0 t(1) 0 t(1)

Order : 0,t(1),1,s(2),3,e(2),n(2),5Order : 0,t(1),1,s(2),3,e(2),n(2),5 s(2) and t(1) are swapped s(2) and t(1) are swapped

Page 12: Dynamic Huffman Coding

12

TENNESSETENNESSE

Stage 8 (Second repetition of Stage 8 (Second repetition of ee ) ) rr / \/ \ nn(2) 6(2) 6 / \/ \ 3 3 ee(3)(3) / \/ \ 1 1 ss(2)(2) / \/ \ 0 0 tt(1)(1)

Order : 0,Order : 0,tt(1),1,(1),1,ss(2),3,(2),3,ee(3),(3),nn(2),6 : (2),6 : MisfitMisfit

Page 13: Dynamic Huffman Coding

13

Reorder: TENNESSEReorder: TENNESSE

rr / \/ \ ee(3) 5(3) 5 / \/ \ 3 3 nn(2)(2) / \/ \ 1 1 ss(2)(2) / \/ \ 0 0 tt(1)(1) Order : 1,t(1),1,s(2),3,n(2),e(3),5Order : 1,t(1),1,s(2),3,n(2),e(3),5 N(2) and e(3) are swappedN(2) and e(3) are swapped

Page 14: Dynamic Huffman Coding

14

TENNESSEETENNESSEE

Stage 9 (Second repetition of Stage 9 (Second repetition of ee ) ) rr 00/ \/ \11

ee(4) 5(4) 5 00/ \/ \11

3 3 nn(2)(2) 00/ \/ \11

1 1 ss(2)(2) 00/ \/ \11

0 0 tt(1)(1) Order : 1,Order : 1,tt(1),1,(1),1,ss(2),3,(2),3,nn(2),(2),ee(4),5(4),5

Page 15: Dynamic Huffman Coding

15

ENCODINGENCODING

The letters can be encoded as follows:The letters can be encoded as follows:

e : 0e : 0 n : 11n : 11 s : 101s : 101 t : 1001t : 1001

Page 16: Dynamic Huffman Coding

16

Average Code LengthAverage Code Length

Average code length = Average code length = i=0,n i=0,n (length*frequency)/ (length*frequency)/ i=0,ni=0,n

frequencyfrequency

= { 1(4) + 2(2) + 3(2) + 1(4) } / = { 1(4) + 2(2) + 3(2) + 1(4) } / (4+2+2+1)(4+2+2+1)

= 18 / 9 = 2= 18 / 9 = 2

Page 17: Dynamic Huffman Coding

17

ENTROPYENTROPY

Entropy = -Entropy = -i=1i=1,,nn (p (pii loglog2 2 ppii))

= - ( 0.44 * log= - ( 0.44 * log220.44 + 0.22 * log0.44 + 0.22 * log220.220.22

+ 0.22 * log+ 0.22 * log220.22 + 0.11 * 0.22 + 0.11 * loglog220.11 )0.11 )

= - (0.44 * log0.44 + 2(0.22 * log0.22 + 0.11 * = - (0.44 * log0.44 + 2(0.22 * log0.22 + 0.11 * log0.11)log0.11)

/ log2/ log2

= 1.8367= 1.8367

Page 18: Dynamic Huffman Coding

18

Ordinary Huffman CodingOrdinary Huffman Coding

TENNESSETENNESSE 99 00/ \/ \11

5 5 ee(4)(4) 00/ \/ \11

ss(2) 3(2) 3 00/ \/ \11

tt(1) (1) nn(2) (2)

ENCODINGENCODING E : 1E : 1 S : 00S : 00 T : 010T : 010 N : 011N : 011

Average code length = (1*4 Average code length = (1*4 + 2*2 + 2*3 + 3*1) / 9 = + 2*2 + 2*3 + 3*1) / 9 = 1.891.89

Page 19: Dynamic Huffman Coding

19

SUMMARYSUMMARY

The average code length of ordinary Huffman The average code length of ordinary Huffman coding seems to be better than the Dynamic version,in this coding seems to be better than the Dynamic version,in this exercise.exercise.

But, actually the performance of dynamic coding is But, actually the performance of dynamic coding is better. The problem with static coding is that the tree has better. The problem with static coding is that the tree has to be constructed in the transmitter and sent to the to be constructed in the transmitter and sent to the receiver. The tree may change because the frequency receiver. The tree may change because the frequency distribution of the English letters may change in plain text distribution of the English letters may change in plain text technical paper, piece of code etc. technical paper, piece of code etc.

Since the tree in dynamic coding is constructed on the Since the tree in dynamic coding is constructed on the receiver as well, it need not be sent. Considering this, receiver as well, it need not be sent. Considering this, Dynamic coding is better.Dynamic coding is better.

Also, the average code length will improve if the Also, the average code length will improve if the transmitted text is bigger.transmitted text is bigger.