Optimal binary search tree
In computer science the so-called optimal binary search tree problem consists in finding the binary search tree which guarantees the minimum possible search time for each of its nodes given an access-frequency or access-probability vector.
Suppose we have two nodes of value 10 and 20. These two nodes can form the following binary search trees
now suppose we also have an array of access frequencies per each node. This tells us that the node 10 will be accessed 50 times while the node 20 will be accessed 100 times.
If we were to access the nodes in a sequential fashion we would just put the node 20 (with the highest access frequency) at the front and the other node at the bottom (less accessed). Anyway with binary trees we would ideally like to have the node 20, which is the most-accessed one, as root in order to reduce searching.
In the case above we have two possible BSTs. Weighing more deeply nested nodes more, the first will have cost
\[200 \cdot 1 + 50 \cdot 2 = 300\]while the second BST will have cost
\[50 \cdot 1 + 200 \cdot 2 = 450\]This leads us to state that the optimal binary search tree is the first one with the most-accessed node at the root.
A recursive solution
If the input nodes are sorted (condicio sine qua non) a recursive algorithm can be set up to solve the problem. Let us consider again the two BSTs above
In the first case we chose 20 as root and 10 as subnode. There could have been other subnodes as well. The key to solve the problem is to consider the way we assigned the weights:
\[200 \cdot 1 + 50 \cdot 2 = 300\]we weighed 1 the root node’s frequency and 2 the other node’s frequency. This is equivalent to doing
\[\sum{\text{all frequencies of this tree}} + \sum{\text{all frequencies of the subtree}}\]and thus a recursive relation such as
would give us the cost of a specific subtree rooted at root. What we need to do is iterate over all the possible root nodes (i.e. try each node as root) for every possible subtree.
A careful reader might notice that we haven’t dealt with the correctness of a BST in any way, i.e. we didn’t care of having lesser nodes in the left subtree and bigger nodes in the right subtree. The pseudocode above only works if the nodes are sorted since grabbing each node and considering it as root and recursing on the left and right parts of the input nodes array only yields valid BSTs if the nodes are sorted (and so must be the frequencies to have a consistent access). A snippet to sort frequencies and nodes according to the nodes’ ordering follows
Dynamic programming solution
The pseudocode proposed above has \( O(2^N) \)exponential complexity since once a root has been established, it tries to build recursively all possible subtrees and the process starts again by testing each node as root.
Since recursively testing for all possible subtrees has the overlapping subproblems property (i.e. we’re computing the same problem over and over) and since for every subset of items (e.g. only 2 nodes) an optimal minimum cost solution can be found, the problem has the optimal substructure property as well and thus it is a good candidate to be solved via dynamic programming.
The following code puts together the requirement for the keys array (i.e. the array of nodes) to be sorted, along with the frequencies arranged abiding by their order, and a dynamic programming iteration via increasing subsets.
This algorithm has complexity \( O(N^3) \) and features optimization spots, for instance frequencies sums could be cached instead of being recalculated each time.