ADA
ADA
G.G.D.S.D. COLLEGE
2024-2026
02
ACKNOWLEDGEMENT
03
CONTENTS
1 Introduction 4-5
2 Example 6
3 Algorithm 7-8
6 References 13
MINOR PROJECT MS-64 // SHRUTI ARORA
04
INTRODUCTION
An Optimal Binary Search Tree (OBST), also known as a Weighted Binary Search Tree, is a binary
search tree that minimizes the expected search cost.
It is a classic problem in computer science that belongs to the domain of Dynamic Programming
and deals with efficient searching.
Problem Definition:
Given:
A set of n distinct keys K = {k1, k2, ..., kn} where the keys are sorted.
An array of probabilities P = {p1, p2, ..., pn}, where pi represents the probability of
searching for key ki.
Optionally, we may also be given a set of dummy keys, which correspond to unsuccessful
searches between the actual keys.
Objective: The goal is to construct a binary search tree that minimizes the expected search
cost, where the cost of searching a key is proportional to its depth in the tree.
05
2. Recursive Structure:
The recursive relation captures the optimal cost for a given range of keys by trying all
possible roots within the range. Each subproblem is solved optimally, and the results are
used to build larger solutions.
The base case occurs when i > j, which represents an empty subtree, and its cost is zero.
3. Compute Optimal Solutions for Subproblems:
For each possible subtree, compute the cost for all ranges of keys i to j and store the
results in a table.
Start by solving smaller subproblems (with only one or two keys), then move on to larger
subproblems until the entire range 1 to n is solved.
4. Reconstruct the Optimal Tree:
The dynamic programming table not only gives the minimal search cost but also helps in
reconstructing the structure of the OBST by keeping track of the roots chosen for each
subtree.
Complexity:
Time Complexity: The time complexity of the dynamic programming approach for OBST is
O(n^3) because we compute the cost for all possible subtrees of the keys (a total of O(n^2)
subproblems), and for each subproblem, we evaluate all possible roots (O(n)).
Space Complexity: The space complexity is O(n^2) since we store the computed cost of each
subtree in a table.
MINOR PROJECT MS-64 // SHRUTI ARORA
06
EXAMPLE
Example: Let n=4 and (a1, a2, a3, a4) = (do, if, int, while). In sorted order as this is a BST.
i=
0 1 2 3 4
distance
w02 = P2 + Q2 + w01= 3+1+8 =12 w13 = P3 + Q3 + w12= 1+1+7 =12 w24 = P4 + Q4 + w23
c02= min c13= min = 1+1+3 =5
{k=1, c00+c12+w02 = 19 {k=2, c11+c23+w13 = 12 c24= min
2 k=2, c01+c22+w02 = 20} k=3, c12+c33+w13 = 16} {k=3, c22+c34+w24 = 8
r02=1 r13=2 k=3, c23+c44+w24 = 8}
both are same so take k=3
r24=3
The rank r04 is the root of the calculated optimal tree. It has
node 1 (r02) as the left child and node 3 (r24) as the right child.
w04 = P4 + Q4 + w03= 1+1+14 =16
Node 3 has right child node 4 (r34) has shown below.
c04= min
{k=1, c00+c14+w04 = 35 if
4 k=2, c01+c24+w04 = 32
k=3, c02+c34+w04 = 38
k=4, c03+c44+wo4= 41}
do int
r03=2
while
MINOR PROJECT MS-64 // SHRUTI ARORA
07
ALGORITHM
Algorithm OBST(p, q, n)
// Given n distinct identifiers a1 < a2 < ... < an and probabilities
// p[i], 1 <= i <= n, and q[i], 0 <= i <= n, this algorithm computes
// the cost c[i, j] of optimal binary search trees tij for identifiers
// ai, ..., aj. It also computes r[i, j], the root of tij.
// w[i, j] is the weight of tij.
{
// Step 1: Initialize matrices w, c, and r for empty and single-node subtrees
for i := 0 to n do
{
w[i, i] := q[i];
r[i, i] := 0;
c[i, i] := 0.0;
if i < n then
{
// Initialize single-node subtrees
w[i, i+1] := q[i] + q[i+1] + p[i+1];
r[i, i+1] := i + 1;
c[i, i+1] := w[i, i+1];
}
}
08
ALGORITHM
// Step 3: Use Knuth's optimization to find the optimal root in the range [r[i, j-1], r[i+1, j]]
k := Find(c, r, i, j); // Find the optimal root using restricted range
c[i, j] := w[i, j] + c[i, k-1] + c[k, j];
r[i, j] := k;
}
Algorithm Find(c, r, i, j)
// Finds the root k in the range [r[i, j-1], r[i+1, j]] that minimizes the cost c[i, j]
{
min := ∞;
l := r[i, j-1]; // Start from the lower bound of the range
for m := r[i, j-1] to r[i+1, j] do
if (c[i, m-1] + c[m, j] < min) then
{
min := c[i, m-1] + c[m, j];
l := m;
}
return l; // Return the root that minimizes the cost
}
MINOR PROJECT MS-64 // SHRUTI ARORA
09
CODE
#include <stdio.h>
#include <limits.h> // For INT_MAX
void optimal_bst(float p[], float q[], int n, float c[MAX][MAX], float w[MAX][MAX],
int r[MAX][MAX]) {
// Step 1: Initialize matrices w, c, and r for empty and single-node subtrees
for (int i = 0; i <= n; i++) {
w[i][i] = q[i];
c[i][i] = 0.0;
r[i][i] = 0;
if (i < n) {
// Initialize single-node subtrees
w[i][i + 1] = q[i] + q[i + 1] + p[i + 1];
c[i][i + 1] = w[i][i + 1];
r[i][i + 1] = i + 1;
}
}
// Step 3: Use Knuth's optimization to find the optimal root in the range
[r[i, j-1], r[i+1, j]]
int k = find(c, r, i, j); // Find the optimal root using restricted range
c[i][j] = w[i][j] + c[i][k - 1] + c[k][j];
r[i][j] = k;
}
}
MINOR PROJECT MS-64 // SHRUTI ARORA
10
CODE
return optimal_root;
}
MINOR PROJECT MS-64 // SHRUTI ARORA
11
CODE
int main() {
int n = 4; // Number of keys
float p[] = {0, 0.15, 0.10, 0.05, 0.10}; // Probabilities for successful searches
(p[1] to p[n])
float q[] = {0.05, 0.10, 0.05, 0.05, 0.05}; // Probabilities for unsuccessful
searches (q[0] to q[n])
return 0;
}
MINOR PROJECT MS-64 // SHRUTI ARORA
12
SNIPPETS
MINOR PROJECT MS-64 // SHRUTI ARORA
13
SNIPPETS
MINOR PROJECT MS-64 // SHRUTI ARORA
14
REFERENCES
www.geeksforgeeks.org
www.javapoint.com
Algorithm Design and Analysis Simplified - Dr. Himani Mittal
Fundamentals of Computer Algorithms - Sartaj Sahani
ChatGpt
www.google.com