Optimal Binary Search Trees-Dynamic Programming
Explore how to apply dynamic programming for building optimal binary search trees. Understand frequency-based cost calculations, memoization, and evaluation orders to minimize total search time in a sorted key set.
Dynamic programming algorithm for optimal binary search trees
The final problem we considered in the previous chapter was the optimal binary search tree problem. The input is a sorted array of search keys and an array of frequency counts, where is the number of times we will search for . Our task is to construct a binary search tree for that set such that the total cost of all the searches is as small as possible.
Fix the frequency array , and let denote the total search time in the optimal search tree for the subarray . We derived the following recurrence for the function :
You can probably guess what we’re going to do with this recurrence eventually, but let’s get rid of that cumbersome summation first. For any pair of indices , let denote the total frequency count for all the keys in the interval :
This function satisfies the following simple recurrence:
...