Lecture - 20 - Dynamic Programming - OBST
Lecture - 20 - Dynamic Programming - OBST
2
Balanced Binary Tree
• A balanced binary tree, also referred to as a height-balanced binary tree, is
defined as a binary tree in which the height of the left and right subtree of
any node differ by not more than 1.
3
Balanced Binary Search Tree
• If we have keys: 15, 25, 35, then see how many BST can be
formed, which grows exponentially, brute force is hopeless.
• In the third case, the total number of comparisons is less as
compared to others because the height of the tree is less. So,
we can say that it’s a balanced binary search tree.
4
Avg. No. of Comparisons
Optimal Binary Search Tree (OBST)
• Now assume there is an associated cost to search a value in the tree, now we
have to find out which balanced binary search tree is optimal
Keys 15 25 35
Associated Cost (AC) 3 2 5
1x3=3
2x2=4
3x5=15
Keys 15 25 35
Cost 2x3 3x2 1x5
Total Cost = 17
7
Optimal Binary Search Trees
• Key Table
• Keys (in order) + frequency
A B C D E F G H I J K L M N O P …
23 10 8 12 30 5 14 18 20 2 4 11 7 22 22 10 …
• Key Problem
• Which key should be placed at the root?
• If we can determine this, we can ask the same question for the left and right subtrees.
8
Optimal Binary Search Tree
• Divide and conquer?
• Choose a key for the root n choices
• Repeat the process for the sub-trees O(2n)
9
Optimal Binary Search Tree
• Start with the small problems
A B C D E F G H I J K L M N O P …
23 10 8 12 30 5 14 18 20 2 4 11 7 22 22 10 …
• Two possible D C
Min
arrangements
Cost 8x1 + 12x2 = 32 8x2 + 12x1 = 28
10
Optimal Binary Search Tree - Cost Matrix
• Initialise
Cjj
• Diagonal
• C[j,j]
• Costs of
one-element ‘trees’ Zer
o
• Below diagonal
• C[j,k] x
• Costs of best tree
j to k
Cost of best tree C-G
11
Optimal Binary Search Tree - Cost matrix
12
Optimal Binary Search Tree - Root matrix
13
Optimal Binary Search Tree - 3-element trees
A B C D E F G H I J K L M N O P …
23 10 8 12 30 5 14 18 20 2 4 11 7 22 22 10 …
• 3-element trees
Root = Root = Root =
B C D
B We already D
know this C
D is B
best for C,D B D
C and stored
C
its cost
Best
B,C
• Find best, store cost in C[B,D]
• Store root in best[B,D]
15
Optimal Binary Search Tree - 3-element trees
Costs Roots
16
Optimal Binary Search Trees - 4-trees
• Now the 4-element trees
eg A-D
17
Optimal Binary Search Trees
• Final cost will be in C[0,n-1]
Final
cost
18
Optimal Binary Search Trees
• Construct the search tree
• Root will be in
best[0,n-1]
• If r0 = best[0,n-1],
• Left subtree root is
best[0,r0-1],
Right subtree root is
best[r0+1,n-1]
Root = ‘E’
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 …
A B C D E F G H I J K L M N O P …
23 10 8 12 30 5 14 18 20 2 4 11 7 22 22 10 … 19
Optimal Binary Search Trees
• Construct the search tree
B H
A D G I
C F J
20
Optimal Binary Search Trees - Analysis
21
Optimal Binary Search Trees - Analysis
ak
P 1 ...P k-1 P k+1 ...P n
Q 0 ...Q k-1 Q k ...Q n
a1...ak-1 ak+1...an
8-
C(1,k-1) C(k+1,n) 22
Optimal Binary Search Trees - Analysis
k 1
C(i, j) min Pk Qi-1 Pm Q m Ci, k 1
i k j
m i
j
Q k Pm Q m Ck 1, j
m k 1
j
min Ci, k 1 Ck 1, j Qi-1 Pm Q m
i k j
m i
ak
P 1 ...P k-1 P k+1 ...P n
Q 0 ...Q k-1 Q k ...Q n
a1...ak-1 ak+1...an
8-
C(1,k-1) C(k+1,n) 23
Optimal Binary Search Trees - Analysis
• k -element trees require k operations
• One for each candidate root
• There are k of them O(k2) n
• There are n levels k 2
= O(n3)
k =1
24