Skip to main content

Optimal Binary Search using Dynamic Programming

An optimal binary search tree is a binary search tree for which the nodes are arranged on levels such that the tree cost is minimum.

If the probabilities of searching for elements of a set are known from accumulated data from past searches, then Binary Search Tree (BST) should be such that the average number of comparisons in a search should be minimum.

eg. Lets the elements to be searched are A, B, C, D and probabilities of searching these items are 0.1, 0.2, 0.4 and 0.3 respectively.

Lets consider 2 out of 14 possible BST containing these keys.
Figure 1
Figure 2

Average number of comparison is calculated as sum of level*probability(key element) for each element of the tree.

Lets the level of tree start from 1.

Therefore, for figure 1 -
   
  • Average number of comparison = 1*0.1 +2*0.2 +3*0.4 +4*0.3  = 2.9                                             
For figure 2 -
  •    Average number of comparison= 2*0.1 +1*0.2 +2*0.4 +3*0.3 = 2.1

Since, both the examples are not optimal. So, how to find the Optimal Binary Search Tree.

In this example, we can find the optimal BST by generating all 14 BST with these keys. i.e. exhaustive search approach, which is not possible for larger value of keys. Because total number of BSTs with n keys is equal to nth Catalan number. So, the alternate method is to use the Dynamic Programming.

Notes for OBST using Dynamic Programming are attached below.

Page1

page2

page3

page4

page5

page6

page7

page8

page9

page10

page11

Comments

Popular posts from this blog

StackOverFlowError and OutOfMemoryError in java

There are two area inside java virtual machine's memory the heap and the  stack . The  stack  memory is used to store local variables and function call while heap memory is used to store objects in  Java The most common cause of StackOverFlowError is too deep or infinite recursion or many local objects / variables creation inside function call in  Java.  According to the java source documentation,  Java throws  java.lang.StackOverflowError   when a stack overflow occurs because an application recurses too deeply. JVM has a given memory allocation for each stack of each thread, and if an attempt to call a method happens to fill this memory, JVM throws an error. Just like it would do if we try to write at index N of an array of length N.  The point to be noted here is that - These are errors not an exceptions. No memory corruption happens due to the error. Stack can not write into the heap space. A StackOverflowError i...

Job Sequencing with Deadlines

Given a set of n jobs Each job i has an integer deadlines di>=0 and a profit pi>0 All jobs requires only one unit time to complete Only one machine is available for processing jobs For job i the profit pi is earned if the job is completed by its deadline.