The Optimal Binary Search Tree (OBST) problem involves constructing a binary search tree (BST) in such a way that the weighted search cost of accessing nodes is minimised. Given a set of node values and their corresponding access frequencies (or probabilities), the goal is to compute the arrangement of BST that ensures the minimum possible weighted search cost. The weight is calculated by multiplying the frequency by the depth of the node in the tree.
Input:
Node values:[10,20]Access frequencies:[50,100]Output:
Optimal BST cost:300Explanation:
Possible trees:Tree 1:20/10Cost =100*1+50*2=300Tree 2:10\20Cost =50*1+100*2=450Optimal cost is achieved with Tree 1.
Input:
Node values:[10,20,30]Access frequencies:[100,150,200]Output:
Optimal BST cost:950Explanation:
Using dynamic programming, the optimal BST cost is calculated as 950 by proper arrangement of the nodes.
To minimise the search cost, nodes with higher frequencies should ideally be closer to the root. The problem is solved using dynamic programming because it involves overlapping subproblems (cost computations for subarrays) and a directed acyclic structure. The DP table keeps track of the minimum cost for each subtree arrangement.