Romanian Informatics Olympiad - Modified Huffman Encoding

Source: (Romanian Informatics Olympiad ONI'03, extended team selection)

File:Huffman tree 2.svg

( ^ Representative diagram of Huffman Encoding. No relevance in the problem)

Problem:
A telegraph machine can transmit only lines and dots; it takes 2 seconds to transmit a line, but only 1 second to transmit a dot. We generally want to transmit texts containing letters of the English alphabet, and digits (so we have N<=36 symbols in total). Therefore, a prefix-free encoding using lines and dots is needed. Given the frequencies of the N symbols in a large text, find the minimum time it takes to transmit the text using a suitable encoding. The solution should run in O(N^4) time, and use O(N^3) space.




Solution:

Highlight the part between the * symbols for the answer.
* Official solution (as posted by JDGM from the Olympiad authors): this is Huffman coding with unequal letter costs (dot = 1s, line = 2s), which - unlike the equal-cost case - has no known greedy solution, but does have a polynomial DP.

Think of the code tree as a modified Huffman tree where a "line" child sits 2 levels below its parent. First observation (greedy exchange argument): in an optimal tree, reading the leaves in level order, the symbol frequencies must be non-increasing. So sort the symbols by frequency; only the tree's SHAPE is left to determine.

Build the tree top-down with DP. The state: (number of internal nodes one level above the frontier, number of internal nodes two levels above, number of leaves placed on upper levels). Each expansion choice (open an internal node, or close a leaf and assign the next-most-frequent symbol to it) costs frequency x depth, and the depth is implicit in the state. With N <= 36 symbols the state space is O(N^3) and transitions O(N), giving the required O(N^4) time / O(N^3) space.

Solution by JDGM from the comments (the problem author's official write-up).
*

Comments

  1. This one has been untouched almost three weeks, so here's the solution in the author's words:

    "This is a special case of Huffman coding with unequal letter costs (a polynomial solution is not known for the general case). We need to construct a modified Huffman tree, in which the right child of a node is considered at depth 2 below the node itself. If we take the level traversal of the leaves in this tree, the symbols must appear in decreasing order of frequency (by a greedy exchange argument). So we must only determine the topology of the tree. This can be done by conceptually building the tree from root to leaves using a dynamic programming algorithm; the state is given by the number of internal nodes one and two levels above, and the total number of leaves on upper levels. Note that we can construct the algorithm so that it doesn't need to include the level in the state (this is needed to get the required complexity). Constant factors are significantly improved if we implement this using a memorized recursive call (it turns out only about 10% of the array needs to be computed)."

    RIP Mihai.

    ReplyDelete

Post a Comment

Popular posts from this blog

Polya's Urn Problem: Expected Balls in the Smaller Urn

Lion and Man in a Circular Cage: Can the Lion Catch the Tamer?

Expected Tosses for Consecutive Heads: 2 Heads vs 3 Heads Puzzle