- 2078 Bhadra · 10 marks
Weighted Huffman encoding and decoding
Short answer
Letter frequencies give Huffman codes (e.g., a=01, i=00). Encoding: replace letters to get 83-bit stream. Decoding: receiver uses same tree, reads bits sequentially; each leaf reached outputs a symbol, then restart. Prefix-free property ensures unique decoding. Efficiency ≈ 99.6%.
Encode "Jasta lai tastai dhido lai nistai" using weighted Huffman encoder. Also demonstrate how it is decoded.
Assumptions: spaces are ignored, upper and lower case letters are treated as the same symbol, and each letter is a source symbol. The sentence has 28 letters and 10 distinct symbols.
Step 1: frequencies
a=7, i=6, t=4, s=3, d=2, l=2, h=1, j=1, n=1, o=1
Step 2: build the Huffman tree
Repeatedly merge the two smallest weights (N1, N2, ... are the new nodes):
1. h(1) + j(1) = 2 -> N1
2. n(1) + o(1) = 2 -> N2
3. d(2) + l(2) = 4 -> N3
4. N1(2) + N2(2) = 4 -> N4
5. s(3) + t(4) = 7 -> N5
6. N3(4) + N4(4) = 8 -> N6
7. i(6) + a(7) = 13 -> N7
8. N5(7) + N6(8) = 15 -> N8
9. N7(13) + N8(15) = 28 -> N9
The branch to the smaller-weight node is labelled 0 and to the larger 1, and the codes are read from the root downwards.
Step 3: code table
| Symbol | Count | p | Code | Length |
|---|---|---|---|---|
| a | 7 | 7/28 | 01 | 2 |
| i | 6 | 6/28 | 00 | 2 |
| t | 4 | 4/28 | 101 | 3 |
| s | 3 | 3/28 | 100 | 3 |
| d | 2 | 2/28 | 1100 | 4 |
| l | 2 | 2/28 | 1101 | 4 |
| h | 1 | 1/28 | 11100 | 5 |
| j | 1 | 1/28 | 11101 | 5 |
| n | 1 | 1/28 | 11110 | 5 |
| o | 1 | 1/28 | 11111 | 5 |
Step 4: entropy, average length and efficiency
- Entropy H = −Σ p log₂ p = 2.9532 bits/symbol
- Average length L = Σ p·l = 83/28 = 2.9643 bits/symbol
- Efficiency η = H / L = 2.9532 / 2.9643 = 99.63 %
- Total bits with Huffman = 83; with fixed-length codes (4 bits/symbol) = 112, so saving = 29 bits.
(Huffman codes are not unique when weights tie, but every choice gives the same average length and efficiency.)
Answer: η ≈ 99.6 %
Encoding the sentence
Replace each letter by its code (words shown separately; spaces are not coded):
Jasta 111010110010101
lai 11010100
tastai 101011001010100
dhido 11001110000110011111
lai 11010100
nistai 11110001001010100
Total = 83 bits (against 112 bits with 4-bit fixed codes).
Complete stream:
11101011001010111010100101011001010100110011100001100111111101010011110001001010100
Decoding
The receiver has the same code table (or the same frequencies to rebuild the tree). It reads bits from the left and walks down the tree (0 = left branch, 1 = right branch) until a leaf is reached, outputs that letter and restarts at the root. Because no code is a prefix of another, there is no ambiguity.
First bits: 11101 01 100 101 01 ...
- 1 → 11 → 111 → 1110 → 11101: leaf j
- 0 → 01: leaf a
- 1 → 10 → 100: leaf s
- 1 → 10 → 101: leaf t
- 0 → 01: leaf a
So "jasta" is recovered, and the same procedure over the whole stream gives "jastalaitastaidhidolainistai" (the original letters, without spaces).
✓ Answer checked by Claude Sonnet 5.5
Related questions
- Huffman code for a sentence (Phool ko) · Data Communication, chapter 8
- Encode 1101001 with polar NRZ, AMI, Manchester · Data Communication, chapter 5 · asked 3×
- Encode 1001101110 with unipolar, polar, bipolar codes · Data Communication, chapter 5 · asked 3×
- Encode 10010110001 with NRZ-L, NRZ-I, diff. Manchester · Data Communication, chapter 5
- Encode 1010011001 with five line codes · Data Communication, chapter 5
- Line coding: properties; encode 01001100011 · Data Communication, chapter 5
- CDMA encoding and decoding · Wireless Communication, chapter 7 · asked 2×
- Short note: Convolutional encoding and decoding · Wireless Communication, chapter 6
- Huffman encoding of symbols A to F · Data Structure and Algorithm, chapter 6
- Importance of source encoder; Huffman algorithm · Communication Systems, chapter 6