Saturday, November 18, 2023

Optimal binary search trees

Optimal binary search trees (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.

Suppose “n” keys k1, k2, …, k n are stored at the internal nodes of a binary search tree. It is assumed that the keys are given in sorted order, so that k1< k2 < … < kn. For each key ki , we have a probability pi that a search will be for kiSome searches may be for values not in K, and so we also have n + 1 “dummy keys” d0, d1, d2, . . . ,dn representing values not in K. For each dummy key di , we have a probability qi that a search will correspond to di
Figure shows binary search tree for a set of n = 5 keys. Each key ki is an internal node, and each dummy key di is a leaf. Every search is either successful (finding some key ki ) or unsuccessful (finding some dummy key di), and so we have
Because binary search tree has probabilities of searches for each key and each dummy key, the expected cost of a search can be determined.

For a given set of probabilities, a binary search tree whose expected search cost is smallest can be constructed. And such a tree is called an optimal binary search tree.

The number of binary trees with n nodes is Ω(4n / n3/2), and to examine an exponential number of binary search trees in an exhaustive search.

Solving this problem with dynamic programming:

Step 1: The structure of an optimal binary search tree

If an optimal binary search tree T has a sub tree Tl containing keys ki . . . . .kj , then this sub tree Tl must be optimal as well for the sub problem with keys ki . . . . ,kj and dummy keys di-1, . . . ,dj
The optimal substructure is used to show that an optimal solution can be constructed to the problem from optimal solutions to sub problems.

Step 2: A recursive solution

Pick subproblem domain as finding an optimal binary search tree containing the keys ki , . . . ,kj, where i ≥ 1, j ≤ n, and j = i - 1.
Let e[ i, j ] as the expected cost of searching an optimal binary search tree containing the keys ki , . . . ,kj .
The easy case occurs when j = i - 1. Then we have just the dummy key di-1. The expected search cost is e[i, i – 1] = qi-1.
When j ≥ i , there is a need to select a root kr from ki , . . . ,kj and then make an optimal binary search tree with keys ki , . . . ,kr-1 as its left sub tree and an optimal binary search tree with keys kr+1, . . . ,kj as its right sub tree.
For a sub
tree with keys ki , . . . ,kj , let us denote this sum of probabilities as

Thus, if kr is the root of an optimal subtree containing keys ki , . . . ,kj, then
Choose the root that gives the lowest expected search cost, giving final recursive formulation:

Step 3: Computing the expected search cost of an optimal binary search tree

The pseudo code that follows takes as inputs the probabilities p1, . . . ,pn and q0, . . . ,qn and the size n, and it returns the tables e and root.

The OPTIMAL-BST procedure takes θ (n3) time

Example:









0 comments:

Post a Comment

Data Structures with C++



NET/SET/CS PG



Operating Systems



Computer Networks



JAVA



Design and Analysis of Algorithms



Programming in C++

Top