Title: CS2223 Recitation 6 Finding the optimal Binary Search Tree for Keys with Given Probabilities
1CS2223 Recitation 6Finding the optimal Binary
Search Tree for Keys with Given Probabilities
2Problem Definition
5
2
12
4
Any key to the left of the root node is smaller
than the node Any key to the right of the root
node is bigger than the node
3Problem Definition
- Binary Search Tree with Given Access
Probabilities.
5(p0.3)
2(p0.1)
12(p0.01)
4(p0.59)
Balanced BST is optimal only in case that all the
access probabilities are equal 1/n
4Principle of Optimality
- Given Pi for each key, Fn(p) is the expected cost
(comparison) of finding a node in the BST, we
have
For induction
P1,n
5Principle of Optimality
- Global optimal implies partial optimal
- Why is the 1?
- It is the sum of all the probabilities in the BST.
6A Running Example
Greedy on probabilities
E2.37
E2.17
E2.02
7Finding the Optimal BST
Ci,jPi,jmin(Ci,r-1Cr1,j), iltrltj
What we want is C1,n. We need to calculate
Pi,j first
.24 .46 .69 .99 1
0 .22 .45 .75 .76
0 0 .23 .53 .54
0 0 0 .3 .31
0 0 0 0 .01
Filling order 0. lower triangle 0 1. Diagonal
Pi 2. Pi,j, on increasing i
Pi,j
8Finding the Optimal BST
Ci,jPi,jmin(Ci,r-1Cr1,j), iltrltj
We then calculate Ci,j
.24 .68 1.16 1.99 2
0 .22 .67 1.27 1.3
0 0 .23 .76 .78
0 0 0 .3 .32
0 0 0 0 .01
Filling order 0. lower triangle 0 1. Diagonal
Pi,i 2. Ci,j
Ci,j
9Finding the Optimal BST
Ci,jPi,jmin(Ci,r-1Cr1,j), iltrltj
.24 .68
0 .22 .67
0 0 .23
0 0 0 .3 .32
0 0 0 0 .01
.24 .46 .69 .99 1
0 .22 .45 .75 .76
0 0 .23 .53 .54
0 0 0 .3 .31
0 0 0 0 .01
Pi,j
Ci,j
1
r1, C2,2 ?.22 r2, C1,1 ? .24
r1
C1,2P1,2min
2
3
r2, C3,3 ?.23 r3, C2,2 ? .22
r3
C2,3P2,3min
2
4
r4, C5,5 ?.01 r5, C4,4 ? .3
r4
C4,5P4,5min
5
10Finding the Optimal BST
Ci,jPi,jmin(Ci,r-1Cr1,j), iltrltj
.24 .68 1.16
0 .22 .67 1.27
0 0 .23 .76
0 0 0 .3 .32
0 0 0 0 .01
.24 .46 .69 .99 1
0 .22 .45 .75 .76
0 0 .23 .53 .54
0 0 0 .3 .31
0 0 0 0 .01
Pi,j
Ci,j
2
r1, C2,3 ?.67 r2, C1,1C3,3 ? .47 r3,
C1,2 ?.68
r2
C1,3P1,3min
3
1
r2, C3,4 ?.76 r3, C2,2C4,4 ? .52 r4,
C2,3 ?.67
r3
C2,4P2,4min
3
4
2
11Finding the Optimal BST
Ci,jPi,jmin(Ci,r-1Cr1,j), iltrltj
.24 .68 1.16 1.99 2
0 .22 .67 1.27 1.3
0 0 .23 .76 .78
0 0 0 .3 .32
0 0 0 0 .01
.24 .46 .69 .99 1
0 .22 .45 .75 .76
0 0 .23 .53 .54
0 0 0 .3 .31
0 0 0 0 .01
Pi,j
Ci,j
r1, C2,5 ?1.3 r2, C1,1C3,5 ? 1.02 r3,
C1,2C4,5 ?1 r4, C1,3C5,5 ? 1.17 r5,
C1,4 ?1.99
r3
C1,5P1,5min
3
4,5
1,2
Calculation order by diagonals
12Backtrack the Optimal BST
- Recording every r during filling.
r1
r3
.24 .68 1.16 1.99 2
0 .22 .67 1.27 1.3
0 0 .23 .76 .78
0 0 0 .3 .32
0 0 0 0 .01
Ci,j
r4
3
1
2
4
5