Huffman's Greed
Time limit1sMemory limit128 MB
We build the optimal binary search tree for weighted key and gap frequencies, minimizing weighted comparison counts.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Tree, Intervals, Prefix sum
- Solved
- No attempts yet
Problem
We first fix some terminology on trees. A tree is defined inductively: it has a root which is either an external node (a leaf) or an internal node that has a sequence of trees as its children. An internal node is called the parent of the roots of its child trees. The level of a node is defined inductively: the root has level , and every other node has level one greater than its parent.
In a binary tree every internal node has exactly two children: its left sub-tree and its right sub-tree. In a labelled binary tree every internal node also carries a string, its label. A binary search tree is a labelled binary tree in which every internal node satisfies: all labels in the left sub-tree of are less than the label of , which in turn is less than all labels in the right sub-tree of . Comparisons use lexicographic (alphabetic) order on strings.
An inorder traversal visits a leaf directly, and for an internal node it first traverses the left sub-tree, then visits the node, then traverses the right sub-tree. Hence an inorder traversal of a binary search tree lists the labels in lexicographic order. Binary search trees of different shapes may still yield the same inorder sequence.
To look up a string , we compare with the label of the current root: we stop if ; if we continue in the left sub-tree; if we continue in the right sub-tree. Reaching a leaf means is not stored in the tree.
The number of comparisons depends on and on the shape of the tree, so we want to build a tree that stores a given set of strings while allowing access that is as fast as possible. Since we do not know in advance which strings will be searched for, we assume a probability distribution.
Let be the number of strings to store, and let be these strings in lexicographic order. Let and be non-negative real numbers with
Their meaning is:
- = probability that the search string equals .
- = probability that lies strictly (lexicographically) between and .
By convention is the probability that , and the probability that .
We want a binary search tree over the labels that minimises the expected number of comparisons, namely
where is the leaf reached when searching for a string that lies strictly between and (using the border convention above).
For example, when there are exactly two possible binary search tree shapes; the answer is the one whose expected cost is smaller.
Input
The input contains several test cases. Each test case starts with an integer with . Then follow non-negative integers, the frequencies. Let be the sum of all these frequencies; you may assume . The probabilities and are obtained, in this order, by dividing the frequencies by : the first frequencies give , and the next give . The last test case is followed by a single .
Output
For each test case, output on its own line the integer , where is the minimum expected number of comparisons achievable by any binary search tree for the given frequencies.