GATE Computer Science · Programming, Data Structures, and Algorithms · hard

A message uses five characters A, B, C, D, and E with frequencies 5, 9, 12, 13, and 16 respectively. Using optimal binary Huffman coding, what is the total number of bits (weighted external path length) required to encode the 55 characters?

Published · Last updated · checked before publication — how we verify questions

Options

  • A110 bits assuming each character is encoded with a fixed 2-bit codeword
  • B124 bits with lengths of 3 bits for A, B and 2 bits for C, D, ECorrect answer
  • C165 bits using a fixed-length 3-bit ASCII subset code across 55 characters
  • D138 bits obtained by merging the highest frequencies first instead of lowest

Answer: B. 124 bits with lengths of 3 bits for A, B and 2 bits for C, D, E

Frequencies: {5, 9, 12, 13, 16}. Step 1: merge 5 + 9 = 14 -> set {12, 13, 14, 16}. Step 2: merge 12 + 13 = 25 -> set {14, 16, 25}. Step 3: merge 14 + 16 = 30 -> set {25, 30}. Step 4: merge 25 + 30 = 55. Total bits = sum of internal node weights = 14 + 25 + 30 + 55 = 124 bits (code lengths: A=3, B=3, C=2, D=2, E=2 -> 5*3 + 9*3 + 12*2 + 13*2 + 16*2 = 15 + 27 + 24 + 26 + 32 = 124 bits).

Why the other options are wrong

  • A. 110 bits assuming each character is encoded with a fixed 2-bit codeword — Five distinct characters cannot all have 2-bit prefix-free codewords because 2^2 = 4 < 5 (Kraft inequality).
  • C. 165 bits using a fixed-length 3-bit ASCII subset code across 55 characters — 165 bits (55 * 3) is the uncompressed fixed-length cost, not the optimal Huffman cost.
  • D. 138 bits obtained by merging the highest frequencies first instead of lowest — Huffman's greedy choice always merges the two lowest-frequency trees first.

All Programming, Data Structures, and Algorithms questions and the topic overview →

GATE is a mark of the Indian Institute of Science and the Indian Institutes of Technology. PrepIQ Hub is not affiliated with, endorsed by, or sponsored by the GATE Committee.