O(n^2)-time Algorithm to OPTIMAL-BST Question

Knuth [184] has shown that there are always roots of optimal subtrees such that root[i, j - 1] ≤ root[i, j] ≤ root[i + 1, j] for all 1 ≤ i < j ≤ n. Use this fact to modify the OPTIMAL-BST procedure to run in Θ(n^2) time.


The original O(n^3)-time algorithm is:

OPTIMAL-BST(p, q, n)
1 for i ← 1 to n + 1
2   do e[i, i - 1] ← qi-1
3     w[i, i - 1] ← qi-1
4 for l ← 1 to n
5   do for i ← 1 to n - l + 1
6     do j ← i + l - 1
7       e[i, j] ← ∞
8       w[i, j] ← w[i, j - 1] + pj + qj
9       for r ← i to j
10         do t ← e[i, r - 1] + e[r + 1, j] + w[i, j]
11           if t < e[i, j]
12             then e[i, j] ← t
13               root[i, j] ← r
14 return e and root


To modify, change line 9 of OPTIMAL-BST into:

for r ← root[i, j-1] to root[i+1, j]
First prove this fact. Suppose we already have Optimal BST T[i+1, j], now we make T’[i, j] by add i into T[i+1, j]. So element i will be the left child of i+1, say its height is h. We can get e’[i, j] for T’[i, j]: e’[i, j] = e[i+1, j] + (h+1)pi + (h+2)qi-1 + qi. Of course the optimal T[i, j] should have its e[i, j] ≤ e’[i, j]. Assume that r[i, j] > root[i+1, j], in that case root[i+1, j] will appear in the left subtree of r[i, j] in T[i, j]. That leads to the height of element i being larger than h, as well as the rest i+1 ~ j elements have e value larger than e[i+1, j] (because e[i+1, j] is optimal). In all we have e[i, j] > e[i+1, j] + (h+1)pi + (h+2)qi + qi-1 = e’[i, j]. So r[i, j] ≤ r[i+1, j]. So does root[i, j - 1] ≤ root[i, j].
Next prove the running time is Θ(n^2) by showing that each e[i, j]‘s reference is Θ(n). We call the group of states e[i, j] with the fixed j-i (=k) the level-k group (obviously there’re n-k nodes in the group). The calculation of e[i, j] takes root[i+1, j] – root[i, j-1] + 1 iterations. Thus, for all level-k group states, their calculations take root[k,1] – root[1,k] + n – k iterations in all. Since 1 ≤ root[k, 1], root[1, k] ≤ n, the number of iterations is thus Θ(n). And the k varies from 0 to n-1. The overall complexity is Θ(n^2). This is a common trick to optimize a Θ(n^3) dp algorithm for some kind of problems into a Θ(n^2) one.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值