Algorithm Search⌘K

Proofs: Optimal binary search tree dynamic programming

These are the human proofs of Optimal binary search tree dynamic programming, at full length.

OPTIMAL-BST computes the minimum expected cost and an optimal root for every interval full

This proof shows: correctness, termination.

Fix n≥0n \ge 0, weights p1,…,pn≥0p_1, \ldots, p_n \ge 0 and q0,…,qn≥0q_0, \ldots, q_n \ge 0. For 1≤i≤n+11 \le i \le n + 1 and i−1≤j≤ni - 1 \le j \le n, let T(i,j)\mathcal{T}(i, j) be the set of BSTs on the keys ki,…,kjk_i, \ldots, k_j, each with the gaps di−1,…,djd_{i-1}, \ldots, d_j as leaves. For j=i−1j = i - 1 the only tree is the single leaf di−1d_{i-1}. For T∈T(i,j)T \in \mathcal{T}(i, j) let

c(T)=∑l=ijpl (depthT(kl)+1)+∑l=i−1jql (depthT(dl)+1),c(T) = \sum_{l=i}^{j} p_l\,(\mathrm{depth}_T(k_l) + 1) + \sum_{l=i-1}^{j} q_l\,(\mathrm{depth}_T(d_l) + 1),

so the single leaf costs qi−1q_{i-1}. Let W(i,j)=∑l=ijpl+∑l=i−1jqlW(i, j) = \sum_{l=i}^{j} p_l + \sum_{l=i-1}^{j} q_l and E(i,j)=min⁡T∈T(i,j)c(T)E(i, j) = \min_{T \in \mathcal{T}(i, j)} c(T); the set is finite and not empty, so the minimum exists.

Lemma 1 (decomposition). For i≤ji \le j, each T∈T(i,j)T \in \mathcal{T}(i, j) has a root krk_r with i≤r≤ji \le r \le j, a left subtree L∈T(i,r−1)L \in \mathcal{T}(i, r - 1) and a right subtree R∈T(r+1,j)R \in \mathcal{T}(r + 1, j), and

c(T)=c(L)+c(R)+W(i,j).c(T) = c(L) + c(R) + W(i, j).

Each choice of rr, LL and RR gives a tree of T(i,j)\mathcal{T}(i, j).

Proof. By the BST property, the keys below and to the left of krk_r are exactly ki,…,kr−1k_i, \ldots, k_{r-1}, and the gaps among them, and each leaf dld_l lies on the side of krk_r that the search for a value in dld_l takes: di−1,…,dr−1d_{i-1}, \ldots, d_{r-1} on the left and dr,…,djd_r, \ldots, d_j on the right. So L∈T(i,r−1)L \in \mathcal{T}(i, r-1) and R∈T(r+1,j)R \in \mathcal{T}(r+1, j). Each node vv of LL or RR has depthT(v)=depthL(v)+1\mathrm{depth}_T(v) = \mathrm{depth}_L(v) + 1 (or with RR), so its term in c(T)c(T) is its term in c(L)c(L) or c(R)c(R) plus its weight. The root has depth 0 and term prp_r. Adding up, c(T)=c(L)+c(R)+∑vweight(v)=c(L)+c(R)+W(i,j)c(T) = c(L) + c(R) + \sum_{v} \mathrm{weight}(v) = c(L) + c(R) + W(i, j), since every key and every gap of the interval appears once. Conversely, a root krk_r with any LL and RR of the right intervals is a BST on ki,…,kjk_i, \ldots, k_j, because all keys of LL are less than krk_r and all keys of RR are greater. □\square

Lemma 2 (recurrence). E(i,i−1)=qi−1E(i, i - 1) = q_{i-1}, and for i≤ji \le j,

E(i,j)=min⁡i≤r≤j(E(i,r−1)+E(r+1,j))+W(i,j).E(i, j) = \min_{i \le r \le j} \bigl( E(i, r - 1) + E(r + 1, j) \bigr) + W(i, j).

Proof. The first claim holds because T(i,i−1)\mathcal{T}(i, i-1) has one tree. For i≤ji \le j, by Lemma 1, c(T)≥E(i,r−1)+E(r+1,j)+W(i,j)c(T) \ge E(i, r-1) + E(r+1, j) + W(i, j) for each tree with root krk_r, so E(i,j)E(i, j) is at least the right side. Choosing for each rr an optimal LL and an optimal RR gives a tree whose cost equals the term for rr, so E(i,j)E(i, j) is at most the right side. □\square

Invariant of the loops. After the iteration of line L2 with value ℓ\ell (and, for ℓ=0\ell = 0, after line L1), for each interval with j−i+1≤ℓj - i + 1 \le \ell: w[i,j]=W(i,j)w[i, j] = W(i, j), e[i,j]=E(i,j)e[i, j] = E(i, j), and, if i≤ji \le j, root[i,j]root[i, j] is a value of rr 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)e[i, i-1] = w[i, i-1] = q_{i-1} = E(i, i-1) = W(i, i-1).

Step. Take ℓ≥1\ell \ge 1 and an interval [i,j][i, j] with j=i+ℓ−1j = i + \ell - 1, visited by line L3. Line L4 sets w[i,j]=w[i,j−1]+pj+qjw[i, j] = w[i, j-1] + p_j + q_j; the interval [i,j−1][i, j-1] has length ℓ−1\ell - 1, so by the invariant w[i,j−1]=W(i,j−1)w[i, j-1] = W(i, j-1), and W(i,j)=W(i,j−1)+pj+qjW(i, j) = W(i, j-1) + p_j + q_j. For each rr in line L5, the intervals [i,r−1][i, r-1] and [r+1,j][r+1, j] have length less than ℓ\ell, so line L6 computes tr=E(i,r−1)+E(r+1,j)+W(i,j)t_r = E(i, r-1) + E(r+1, j) + W(i, j). Lines L5 to L7 keep the running minimum of ti,…,trt_i, \ldots, t_r in e[i,j]e[i, j] and an index that reaches it in root[i,j]root[i, j]; the start value ∞\infty is beaten by tit_i. After the loop, e[i,j]=min⁡rtr=E(i,j)e[i, j] = \min_r t_r = 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]k_{root[i,j]}, subtrees built from [i,root[i,j]−1][i, root[i,j]-1] and [root[i,j]+1,j][root[i,j]+1, j], and the leaf di−1d_{i-1} for an empty interval) has cost E(i,j)E(i, j): by Lemma 1 its cost is E(i,r−1)+E(r+1,j)+W(i,j)E(i, r-1) + E(r+1, j) + W(i, j) with r=root[i,j]r = root[i, j], which is the minimum. For i=1,j=ni = 1, j = n this is an optimal tree with cost e[1,n]e[1, n]. ■\blacksquare

Back to Optimal binary search tree dynamic programming