1. Problem Denition
-- Input: Frequencies p1, p2, ... , pn for items 1, 2, ... , n. [Assume items in sorted order, 1 < 2 < ... < n]
-- Goal: Compute a valid search tree that minimizes the weighted (average) search time:
C(T) = Sum(items i ) { pi * [search time for i in T] (Depth of i in T + 1) }
Example: If T is a red-black tree, then C(T) = O(log n). (Assuming Sum(i){ pi }= 1.)
2. Comparison with Human Codes
-- Similarities:
-- Output = a binary tree
-- Goal is (essentially) to minimize average depth with respect to given probabilities
-- Dierences:
-- With Human codes, constraint was prex-freeness [i.e., symbols only at leaves]
-- Here, constraint = search tree property
3. Greedy Doesn't Work
-- Intuition: Want the most (respectively, least) frequently accessed items closest (respectively, furthest) from the root.
-- Bottom-up [populate lowest level with least frequently accessed keys]
-- Top-down [put most frequently accessed item at root, recurse]
-- Counter examples:
4. Optimal Substructure
-- Suppose an optimal BST for keys {1, 2, ... , n} has root r , left subtree T1, right subtree T2. T1 is optimal for the keys {1, 2, ... , r - 1} and T2 is optimal for the keys {r + 1, r + 2, ... , n}
-- Proof :
a) Let T be an optimal BST for keys {1, 2, ... , n} with frequencies p1, ... , pn. Suppose T has root r . Suppose for contradiction that T1 is not optimal for {1, 2, ... , r - 1} [other case is similar] with C(T1* ) < C(T1). Obtain T* from T by "cutting+pasting" T1* in for T1.
b) C(T) = Sum(i = 1 to n) { pi * [search time for i in T] }
= pr * 1 + Sum(i = 1 to n-1 ) { pi * [search time for i in T] } + Sum(i=r+1 to n){ pi * [search time for i in T] }
= Sum(i = 1 to n) { pi } + Sum(i = 1 to n-1 ) { pi * [search time for i in T1] } + Sum(i=r+1 to n){ pi * [search time for i in T2] }
= Sum(i = 1 to n) { pi } + C(T1) + C(T2)
c) C(T*) = Sum(i = 1 to n) { pi } + C(T1*) + C(T2) < C(T) contradicting optimality of T.
5. Optimal Substructure
-- Items in a subproblem are either a prefix or a suffix of the original problem.
-- Let {1, 2, ... , n} = original items. We need to compute the optimal BST for subsets {i, i + 1, ... , j-1, j} for every i<=j ( continuous intervals )
6. The Recurrence
-- Notation: For 1 <= i <= j <= n, Let C(i , j) = weighted search cost of an optimal BST for the items {i , i + 1, ... , j - 1, j} [with probabilities pi , pi+1, ... , pj-1, pj ]
-- Recurrence: For every 1 <= i <= j <= n:
C(i , j) = min (r=i to j) { Sum(k = i to j) {pk} + C(i, r-1) , + C( r+1, j) }
(Recall formula C(T) = Sum (k){pk} + C(T1) + C(T2) ) Interpret C(x , y) = 0 if x > y
7. The Algorithm
-- Important: Solve smallest subproblems (with fewest number (j - i + 1) of items) first.
-- Let A = 2-D array. ( A[i , j ] represents optimal BST value of items {i, ... , j} )
For s = 0 to n - 1 [s represents j - i ]
For i = 1 to n [so i + s plays role of j ]
A[i , i + s] = min(r=i to i+s) { Sum(k=i to i+s) {pk} + A[i , r-1] + A[r + 1 , i + s] }
Return A[1 , n]
-- Interpret as 0 if 1st index > 2nd index. Available for O(1)-time lookup
-- Running Time
a) O(n^2) subproblems
b) (j - i ) time to compute A[i , j ]
c) O(n^3) time overall
相关推荐
在Knuth的经典论文《Optimum Binary Search Trees》中,他首先回顾了二叉查找树的基本概念及工作原理,即通过比较节点值来决定搜索方向,从而实现快速查找。具体来说,当需要在一个二叉查找树中查找某个元素时,可以...
l5.5 Optimal binary search trees 356 l6 Greedy Algorithms 370 l6.l An activity-selection problem 371 l6.2 Elements of the greedy strategy 379 l6.3 Huffman codes 385 l6.4 Theoretical foundations for ...
##### 4.5 The Optimal Binary Search Trees(最优二叉搜索树) - **问题定义**:给定一系列关键字及其访问频率,构造一棵二叉搜索树,使得查找这些关键字的平均成本最低。 - **关键点**:最优二叉搜索树问题同样...
Static and dynamic weighted binary search trees (Section 10.1) AVL-trees (Section 10.2) Red-black trees (Section 10.3) Splay trees (Section 10.4) B-, B+ and B*-trees (Sections 11.1-11.3) Tries ...
**Optimal Search Trees**: Trees optimized for search operations, minimizing the average number of comparisons required. **B-Trees**: - **Multiway B-Trees**: B-trees with more than two children per ...
TestBinarySearchTree.cpp: Test program for binary search tree AvlTree.h: AVL tree TestAvlTree.cpp: Test program for AVL trees mapDemo.cpp: Map demos WordLadder.cpp: Word Ladder Program and Word ...
- **Finding:** Perform binary search first to find the topic, then use binary search again to find the book title. #### Discussion Points 1. **Comparison of Methods:** - Method 2 (alphabetical ...
3.8.4 Properties of binary integer programs 3.8.5 Dual feasible solutions for IP2 3.9 The maximum coverage problem and the greedy 3.9.1 Tile greedy approach 3.9.2 Applications of the maxinmum coverage...
This means that you do a binary search in the page list in log M time and get the value in O(1) time within a page. RaptorDB starts off by loading the page list and it is good to go from there and...