Optimal Binary Search Trees–Dynamic Programming
Understand the different techniques used to solve the optimal binary search trees problem efficiently.
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 :
We 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:
We can compute all possible values of in time using—you guessed it!—dynamic programming! The usual mechanical steps give us the following dynamic programming algorithm:
Create a free account to access the full course.
By signing up, you agree to Educative's Terms of Service and Privacy Policy