GATE / CS ARCHIVE

SIMILAR QUESTIONS / ALGORITHMS

Build a Huffman tree and evaluate weighted code length

4 questions solved the same way, newest first. Back to Algorithms

How these are solved

Repeatedly merge the two least frequent symbols to obtain an optimal binary prefix tree, then inspect codewords or sum frequency times depth. 2007 code-selection and average-length subparts share a stem and are not repeats;

2 marks · MCQ
Report issue

Greedy Algorithms

Question 26, GATE 2021: Encode abbccddeee with a binary prefix code. Among equal-frequency letters, earlier dictionary letters must have code length no greater than later ones. Find minimum encoded length. A 21; B 23; C 25; D 30.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

The frequencies are 1, 2, 2, 2, 3. Huffman merging costs are 3, 4, 6, 10, totaling 23 bits, the optimal weighted path length. The dictionary tie rule can also be met: assign b=00, c=01, e=10, a=110, d=111. Then lengths of b, c, d are 2, 2, 3, and total length is 3+4+4+6+6=23. Thus B.

Huffman repeatedly merges the two smallest available weights: 1+2=3; 2+2=4; 3+3=6; 4+6=10. Each merge adds its weight once to the encoded length because all leaves below it gain one bit. The displayed code is prefix-free and satisfies the alphabetical tie restriction, so the unrestricted Huffman lower bound is attainable with the extra condition.

Original paper, page 67 · Answer key

2 marks · NAT
Report issue

Greedy Algorithms

Question 50, GATE 2017: A message uses symbols P,Q,R,S,T with probabilities 0.22,0.34,0.17,0.19,0.08 respectively. For a 100-character message encoded with binary Huffman coding, what is the expected encoded length in bits?
Tap to enlarge
Answer & solution

Official answer: 225.0

Our answer: 225

1. Huffman coding repeatedly merges the two smallest current weights. Merge T and R:0.08+0.17=0.25. Next merge S and P:0.19+0.22=0.41. The remaining weights are 0.25,0.34,0.41; merge 0.25+0.34=0.59, then 0.41+0.59=1.

2. The resulting depths are 2 for P,Q,S and 3 for R,T. One valid code assignment is S=00,P=01,T=100,R=101,Q=11; swapping branch labels changes codewords but not lengths.

3. Expected bits per symbol are 2(0.22+0.34+0.19)+3(0.17+0.08)=1.5+0.75=2.25. Equivalently sum the merge weights 0.25+0.41+0.59+1=2.25. For 100 symbols the expectation is 225 bits, excluding codebook/container overhead. It is an expectation, not a guarantee for every possible 100-symbol sequence.

Original paper, page 20 · Answer key

2 marks · MCQ
Report issue

Huffman code for dyadic probabilities

Greedy Algorithms

Question 76, GATE 2007: Letters a,b,c,d,e,f occur with probabilities 1/2,1/4,1/8,1/16,1/32,1/32, respectively. Which list, in a,b,c,d,e,f order, is a Huffman code?
Options:
(A) 0,10,110,1110,11110,11111
(B) 11,10,011,010,001,000
(C) 11,10,01,001,0001,0000
(D) 110,100,010,000,001,111
Question 76, GATE 2007: Letters a,b,c,d,e,f occur with probabilities 1/2,1/4,1/8,1/16,1/32,1/32, respectively. Which list, in a,b,c,d,e,f order, is a Huffman code?
Options:
(A) 0,10,110,1110,11110,11111
(B) 11,10,011,010,001,000
(C) 11,10,01,001,0001,0000
(D) 110,100,010,000,001,111
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

1. Approach. Repeatedly merge the two lowest-probability nodes. Code length is leaf depth in the resulting binary tree; high-probability a should get a one-bit word. Prefix-free alone is not enough to guarantee optimality.

2. Derivation and interpretation. Huffman merging combines e and f (1/32 each) into 1/16, merges with d to 1/8, merges with c to 1/4, merges with b to 1/2, then merges with a. The resulting leaf depths for a,b,c,d,e,f are 1,2,3,4,5,5. Option A supplies a prefix code with these depths: 0,10,110,1110,11110,11111. Bit reversal at internal branches can give other equivalent codes, but A is the listed optimal code.

Original paper, page 19

2 marks · MCQ
Report issue

Average Huffman code length

Greedy Algorithms

Question 77, GATE 2007: Letters a,b,c,d,e,f occur with probabilities 1/2,1/4,1/8,1/16,1/32,1/32, respectively. What is the average length of an optimal binary Huffman code (the linked Q76 code)? Derive the code lengths from these probabilities.
Options:
(A) 3
(B) 2.1875
(C) 2.25
(D) 1.9375
Question 77, GATE 2007: Letters a,b,c,d,e,f occur with probabilities 1/2,1/4,1/8,1/16,1/32,1/32, respectively. What is the average length of an optimal binary Huffman code (the linked Q76 code)? Derive the code lengths from these probabilities.
Options:
(A) 3
(B) 2.1875
(C) 2.25
(D) 1.9375
Question 77, GATE 2007: Letters a,b,c,d,e,f occur with probabilities 1/2,1/4,1/8,1/16,1/32,1/32, respectively. What is the average length of an optimal binary Huffman code (the linked Q76 code)? Derive the code lengths from these probabilities.
Options:
(A) 3
(B) 2.1875
(C) 2.25
(D) 1.9375
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. Approach. Average code length is probability-weighted, not the arithmetic mean of the six word lengths. Huffman merging gives lengths 1,2,3,4,5,5, with the shortest code assigned to the most frequent symbol.

2. Derivation and interpretation. Using the independently derived lengths 1,2,3,4,5,5, the weighted average is (1/2)*1+(1/4)*2+(1/8)*3+(1/16)*4+(1/32)*5+(1/32)*5 = 31/16 = 1.9375 bits per symbol. Answer D.

Original paper, page 19