`
leonzhx
  • 浏览: 802305 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论
阅读更多

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


 

 

 

  • 大小: 20.9 KB
  • 大小: 16.5 KB
分享到:
评论

相关推荐

    Optimal Binary Search Tree

    在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 ...

    算法导论--Introduction.to.Algorithms

    15.5 Optimal binary search trees 397 16 Greedy Algorithms 414 16.1 An activity-selection problem 415 16.2 Elements of the greedy strategy 423 16.3 Huffman codes 428 16.4 Matroids and greedy methods ...

    Dynamic Programming技术PPT课件.pptx

    ##### 4.5 The Optimal Binary Search Trees(最优二叉搜索树) - **问题定义**:给定一系列关键字及其访问频率,构造一棵二叉搜索树,使得查找这些关键字的平均成本最低。 - **关键点**:最优二叉搜索树问题同样...

    高级数据结构PPT 英文版

    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 ...

    Algorithms and Data Structures - Niklaus Wirth

    **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 ...

    c++数据结构与算法实现

    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 ...

    Lecture_1_Introduction.pdf

    - **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 ...

    graphic theory

    ##### 3.4 二叉搜索树 (Binary-Search Trees) 二叉搜索树是一种特殊的二叉树,其中每个节点的关键字都大于其左子树中任何节点的关键字,并且小于其右子树中任何节点的关键字。这种结构非常适合于查找、插入和删除...

    np难问题近似算法(绝版好书)

    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...

    BobBuilder_app

    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...

Global site tag (gtag.js) - Google Analytics