OPTIMAL-BST computes the minimum expected cost and an optimal root for every interval full
This proof shows: correctness, termination.
Fix n≥0, weights p1,…,pn≥0 and q0,…,qn≥0. For 1≤i≤n+1 and i−1≤j≤n, let T(i,j) be the set of BSTs on the keys ki,…,kj, each with the gaps di−1,…,dj as leaves. For j=i−1 the only tree is the single leaf di−1. For T∈T(i,j) let
c(T)=l=i∑jpl(depthT(kl)+1)+l=i−1∑jql(depthT(dl)+1), so the single leaf costs qi−1. Let W(i,j)=∑l=ijpl+∑l=i−1jql and E(i,j)=minT∈T(i,j)c(T); the set is finite and not empty, so the minimum exists.
Lemma 1 (decomposition). For i≤j, each T∈T(i,j) has a root kr with i≤r≤j, a left subtree L∈T(i,r−1) and a right subtree R∈T(r+1,j), and
c(T)=c(L)+c(R)+W(i,j). Each choice of r, L and R gives a tree of T(i,j).
Proof. By the BST property, the keys below and to the left of kr are exactly ki,…,kr−1, and the gaps among them, and each leaf dl lies on the side of kr that the search for a value in dl takes: di−1,…,dr−1 on the left and dr,…,dj on the right. So L∈T(i,r−1) and R∈T(r+1,j). Each node v of L or R has depthT(v)=depthL(v)+1 (or with R), so its term in c(T) is its term in c(L) or c(R) plus its weight. The root has depth 0 and term pr. Adding up, c(T)=c(L)+c(R)+∑vweight(v)=c(L)+c(R)+W(i,j), since every key and every gap of the interval appears once. Conversely, a root kr with any L and R of the right intervals is a BST on ki,…,kj, because all keys of L are less than kr and all keys of R are greater. □
Lemma 2 (recurrence). E(i,i−1)=qi−1, and for i≤j,
E(i,j)=i≤r≤jmin(E(i,r−1)+E(r+1,j))+W(i,j). Proof. The first claim holds because T(i,i−1) has one tree. For i≤j, by Lemma 1, c(T)≥E(i,r−1)+E(r+1,j)+W(i,j) for each tree with root kr, so E(i,j) is at least the right side. Choosing for each r an optimal L and an optimal R gives a tree whose cost equals the term for r, so E(i,j) is at most the right side. □
Invariant of the loops. After the iteration of line L2 with value ℓ (and, for ℓ=0, after line L1), for each interval with j−i+1≤ℓ: w[i,j]=W(i,j), e[i,j]=E(i,j), and, if i≤j, root[i,j] is a value of r that reaches the minimum in Lemma 2.
Base. Line L1 sets e[i,i−1]=w[i,i−1]=qi−1=E(i,i−1)=W(i,i−1).
Step. Take ℓ≥1 and an interval [i,j] with j=i+ℓ−1, visited by line L3. Line L4 sets w[i,j]=w[i,j−1]+pj+qj; the interval [i,j−1] has length ℓ−1, so by the invariant w[i,j−1]=W(i,j−1), and W(i,j)=W(i,j−1)+pj+qj. For each r in line L5, the intervals [i,r−1] and [r+1,j] have length less than ℓ, so line L6 computes tr=E(i,r−1)+E(r+1,j)+W(i,j). Lines L5 to L7 keep the running minimum of ti,…,tr in e[i,j] and an index that reaches it in root[i,j]; the start value ∞ is beaten by ti. After the loop, e[i,j]=minrtr=E(i,j) by Lemma 2. Intervals of the same length do not use each other, so the order of line L3 does not matter.
Termination. All loops have fixed bounds.
The tree. By induction on the length, the tree built from the table (root kroot[i,j], subtrees built from [i,root[i,j]−1] and [root[i,j]+1,j], and the leaf di−1 for an empty interval) has cost E(i,j): by Lemma 1 its cost is E(i,r−1)+E(r+1,j)+W(i,j) with r=root[i,j], which is the minimum. For i=1,j=n this is an optimal tree with cost e[1,n]. ■