Consider the string . Each letter in the string must be assigned a binary code satisfying the following properties:
- For any two letters, the code assigned to one letter must not be a prefix of the code assigned to the other letter.
- For any two letters of the same frequency, the letter which occurs earlier in the dictionary order is assigned a code whose length is at most the length of the code assigned to the other letter.
Among the set of all binary code assignments which satisfy the above two properties, what is the minimum length of the encoded string?
View worked solution
Answer: B
First, let us determine the frequencies of each character in the string abbccddeee:
By constructing a standard Huffman tree, the optimal set of code lengths (depths) for these frequencies is found to be . The absolute minimum length of the encoded string under any prefix-free code is:
Now, we must satisfy Property 2: for characters with the same frequency (), their code lengths must satisfy since they are alphabetically ordered as .
We can assign the optimal set of lengths to the characters as follows:
This assignment satisfies both properties:
- It is a valid prefix-free code (since the set of depths is ).
- For same-frequency characters , we have (i.e., ).
The total encoded length remains:
Since 23 is the absolute lower bound for any prefix code with these frequencies, the minimum length of the encoded string is 23.
Free practice · Your answers on this page are not saved.
