Professional Documents
Culture Documents
E.G. Binary Search Trees For 3, 7, 9, 12
E.G. Binary Search Trees For 3, 7, 9, 12
3 7 7 12
3 12 3 9 9
7
9 9 12 3
7
12
(a) (b) (c) (d)
8 -1
Optimal binary search trees
n identifiers : a1 <a2 <a3 <…< an
Pi, 1in : the probability that ai is searched.
Qi, 0in : the probability that x is searched
where ai < x < ai+1 (a0=-, an+1=).
n n
P Q
i 1
i
i 1
i 1
8 -2
10
Identifiers : 4, 5, 8, 10,
11, 12, 14
5 14
Internal node : successful
search, Pi
4 8 11 E 7
External node :
E 0 E 1 E 2 E 3 E 4 12 unsuccessful search, Qi
E 5 E 6
P level(a ) Q (level(E ) 1)
n 1
i i
n 0
i i
8 -3
The dynamic programming
approach
Let C(i, j) denote the cost of an optimal binary
search tree containing ai,…,aj .
The cost of the optimal binary search tree with ak
as its root :
k 1
n
C(1, n) min Pk Q 0 Pi Q i C1, k 1 Q k Pi Q i C k 1, n
1 k n
i 1 i k 1
ak
P 1 . .P k-1
P k+1
. .P n
Q 0 . .Q k-1 Q k . .Q n
C(1,k-1) C(k+1,n) 8 -4
General formula
k 1
C(i, j) min Pk Qi-1 Pm Q m C i, k 1
i k j
m i
j
Q k Pm Q m C k 1, j
m k 1
j
min C i, k 1 C k 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
a 1 ...a k-1
a k+1 ...a n
C(1,k-1) C(k+1,n) 8 -5
Computation relationships of
subtrees
e.g. n=4
C(1,4)
C(1,3) C(2,4)
8 -6
Matrix-chain multiplication
n matrices A1, A2, …, An with size
p0 p1, p1 p2, p2 p3, …, pn-1 pn
To determine the multiplication order such that # of scalar
multiplications is minimized.
To compute Ai Ai+1, we need pi-1pipi+1 scalar
multiplications.