CSC 505, Spring 2005 Week 6 Lectures page 1 of 9

Similar documents
Introduction to Algorithms

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Introduction to Algorithms

Randomized Algorithms, Quicksort and Randomized Selection

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

CS473-Algorithms I. Lecture 10. Dynamic Programming. Cevdet Aykanat - Bilkent University Computer Engineering Department

II (Sorting and) Order Statistics

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Divide and Conquer

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Selection (deterministic & randomized): finding the median in linear time

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

CMSC Introduction to Algorithms Spring 2012 Lecture 16

We ve done. Now. Next

The divide and conquer strategy has three basic parts. For a given problem of size n,

The complexity of Sorting and sorting in linear-time. Median and Order Statistics. Chapter 8 and Chapter 9

CMPS 2200 Fall Dynamic Programming. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

Divide and Conquer Algorithms

EECS 2011M: Fundamentals of Data Structures

Elements of Dynamic Programming. COSC 3101A - Design and Analysis of Algorithms 8. Discovering Optimal Substructure. Optimal Substructure - Examples

Lecture 5: Sorting Part A

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

Subsequence Definition. CS 461, Lecture 8. Today s Outline. Example. Assume given sequence X = x 1, x 2,..., x m. Jared Saia University of New Mexico

CPSC 311 Lecture Notes. Sorting and Order Statistics (Chapters 6-9)

Chapter 3 Dynamic programming

7. Sorting I. 7.1 Simple Sorting. Problem. Algorithm: IsSorted(A) 1 i j n. Simple Sorting

CSE 3101: Introduction to the Design and Analysis of Algorithms. Office hours: Wed 4-6 pm (CSEB 3043), or by appointment.

Lecture 2: Getting Started

Sorting. There exist sorting algorithms which have shown to be more efficient in practice.

Algorithm Design Techniques part I

IN101: Algorithmic techniques Vladimir-Alexandru Paun ENSTA ParisTech

Module 3: Sorting and Randomized Algorithms. Selection vs. Sorting. Crucial Subroutines. CS Data Structures and Data Management

Lecture 8: Mergesort / Quicksort Steven Skiena

Sorting & Growth of Functions

Module 3: Sorting and Randomized Algorithms

Deterministic and Randomized Quicksort. Andreas Klappenecker

Design and Analysis of Algorithms

Quick Sort. CSE Data Structures May 15, 2002

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

15.4 Longest common subsequence

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

Scribe: Sam Keller (2015), Seth Hildick-Smith (2016), G. Valiant (2017) Date: January 25, 2017

Data Structures and Algorithms Week 4

Unit-5 Dynamic Programming 2016

Lecture 9: Sorting Algorithms

Introduction. e.g., the item could be an entire block of information about a student, while the search key might be only the student's name

Quicksort (CLRS 7) We saw the divide-and-conquer technique at work resulting in Mergesort. Mergesort summary:

CS 171: Introduction to Computer Science II. Quicksort

Sorting is a problem for which we can prove a non-trivial lower bound.

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Efficient Sequential Algorithms, Comp309. Problems. Part 1: Algorithmic Paradigms

Lecture 3. Recurrences / Heapsort

11/8/2016. Chapter 7 Sorting. Introduction. Introduction. Sorting Algorithms. Sorting. Sorting

Algorithms - Ch2 Sorting

Searching, Sorting. part 1

Scribe: Virginia Williams, Sam Kim (2016), Mary Wootters (2017) Date: May 22, 2017

Analysis of Algorithms - Medians and order statistic -

Lecture #2. 1 Overview. 2 Worst-Case Analysis vs. Average Case Analysis. 3 Divide-and-Conquer Design Paradigm. 4 Quicksort. 4.

Subset sum problem and dynamic programming

CS Data Structures and Algorithm Analysis

CPSC 311: Analysis of Algorithms (Honors) Exam 1 October 11, 2002

CSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting. Dan Grossman Fall 2013

Scan and its Uses. 1 Scan. 1.1 Contraction CSE341T/CSE549T 09/17/2014. Lecture 8

Lecture 15 : Review DRAFT

Dr. Amotz Bar-Noy s Compendium of Algorithms Problems. Problems, Hints, and Solutions

Analysis of Algorithms - Quicksort -

Fundamental mathematical techniques reviewed: Mathematical induction Recursion. Typically taught in courses such as Calculus and Discrete Mathematics.

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

Data Structures and Algorithms Chapter 4

Homework3: Dynamic Programming - Answers

1 Probabilistic analysis and randomized algorithms

Question 7.11 Show how heapsort processes the input:

(Refer Slide Time: 01.26)

ECE608 - Chapter 15 answers

Scan and Quicksort. 1 Scan. 1.1 Contraction CSE341T 09/20/2017. Lecture 7

Data Structures. Sorting. Haim Kaplan & Uri Zwick December 2013

Divide and Conquer Sorting Algorithms and Noncomparison-based

CMSC 451: Lecture 10 Dynamic Programming: Weighted Interval Scheduling Tuesday, Oct 3, 2017

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

COMP2012H Spring 2014 Dekai Wu. Sorting. (more on sorting algorithms: mergesort, quicksort, heapsort)

Fast Sorting and Selection. A Lower Bound for Worst Case

CSC Design and Analysis of Algorithms. Lecture 5. Decrease and Conquer Algorithm Design Technique. Decrease-and-Conquer

CS 561, Lecture 1. Jared Saia University of New Mexico

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT.

COE428 Lecture Notes Week 1 (Week of January 9, 2017)

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Merge Sort & Quick Sort

Assignment 1. Stefano Guerra. October 11, The following observation follows directly from the definition of in order and pre order traversal:

Module 3: Sorting and Randomized Algorithms

5. DIVIDE AND CONQUER I

Data Structures and Algorithms. Roberto Sebastiani

Lecture 2: Divide&Conquer Paradigm, Merge sort and Quicksort

Algorithms and Data Structures (INF1) Lecture 7/15 Hua Lu

Algorithms Dr. Haim Levkowitz

COMP 250 Fall recurrences 2 Oct. 13, 2017

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT.

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Dynamic Programming I Date: 10/6/16

Recurrences and Divide-and-Conquer

Transcription:

CSC 505, Spring 2005 Week 6 Lectures page 1 of 9 Objectives: learn general strategies for problems about order statistics learn how to find the median (or k-th largest) in linear average-case number of comparisons (and time) learn how to find the median in linear worst-case time learn about dynamic programming as a general algorithmic technique see dynamic programming applied to a variety of problems learn the principle of optimality and how to verify that it applies in a given situation. Order statistics Problem is to find the i-th smallest of a set of n keys. This is easy to do in linear time if i = 1 or i = n (min or max), and not too hard if i is close to either end. The obvious general approach is to sort the n keys and extract the one in position i of the sorted order. This takes Θ(n lg n) using any comparison-based approach, and is overkill. Let s look at a divide-and-conquer approach similar to Quicksort. function Select(A, p, r, i) returns a key x such that i p keys of A[p],..., A[r] (not including x) are x and r i are x Called initially as Select(A, 1, n, i) if r p > 1 then choose a pivot y (at random) partition A[p],..., A[r] (as in Quicksort) so that: A[j] y for all p j q 1 A[q] = y A[j] > y for all q + 1 j r if i = q then return y else if i < q then return Select(A, p, q 1, i) else return Select(A, q + 1, r, i) endif else return A[p] endif end Select If partitioning is done choosing a random pivot, we can assume that the pivot is equally likely to be in any one of the n positions of A[1],..., A[n]. We can do a worst average-case analysis think about a randomized algorithm playing against a deterministic adversary who can do everything to make the algorithm behave badly except load the dice. In other words, we can simplify the analysis by assuming that the key we re looking for is always in the larger of the two partitions (and is never exactly in position q). The expected number of comparisons under this assumption is given by the recurrence: T (n) n 1 + 1 n n max(t (j 1), T (n j)), T (1) = 0 j=1 c Written by Dr. Carla D. Savage and Dr. Matthias F. Stallmann: All rights reserved.

CSC 505, Spring 2005 Week 6 Lectures page 2 Assuming further that T (n) is nondecreasing, we have T (n) n 1 + 1 n n 1 + 2 n n T (max(j 1, n j)) j=1 n 1 k= n/2 T (k) History elimination to get an exact solution is tricky in this case. Instead, we prove by induction that T (n) cn for a specific constant c that will be chosen later. The basis, n = 1, is trivial, since T (1) = 0 (we re counting comparisons; if we were measuring overall execution time, we would have T (1) = c 0 and would have to ensure that c was chosen to be c 0 ). For the induction step, assume T (k) ck for all k < n. Our goal is to prove that T (n) cn. n 1 T (n) n 1 + 2 ck n k= n/2 n 1 + 2c ( ) n(n 1) (n/2 1)(n/2 1) n( 2 ) 2 3n = n 1 + c 4 + c + c n ( 1 + 3c 4 ) n + d where c and c are constants where d is yet another constant cn as long as c > 4 and n 4d/(c 4) We can assume that T (n) is constant when n < 4d/(c 4) and redo the proof with n = 4d/(c 4) 1 as the basis. It does not make sense to do an induction proof using the assertion T (n) O(n) that s a statement about the value of T (n) for infinitely many n. An induction proof must make a specific assertion that holds for a single n the proof then shows the assertion to be true for all n. In this case, the assertion is T (n) 4n. Let s look at what happens when we try to use a similar induction for the Quicksort recurrence the only difference is that the summation starts at k = 1 instead of n/2 : T (n) n 1 + 2 n 1 ck n k=1 n 1 + 2c ( ) n(n 1) n 2 = n 1 + c(n 1) = (c + 1)n (c + 1) At this point it s tempting to say that T (n) (c+1)n and therefore clearly O(n). But the inductive assertion is that T (n) cn, where c is a particular constant. As n gets large enough (i.e. n c + 1), (c + 1)n (c + 1) > cn no matter what c is. An algorithm that s linear in the worst case The recursive partitioning algorithm presented above has quadratic worst-case behavior just like Quicksort. In order to guarantee a linear number of comparisons in the worst case, we need to make sure a fixed fraction of the input keys are eliminated from consideration before each recursive call.

CSC 505, Spring 2005 Week 6 Lectures page 3 How? The solution to a much smaller selection problem guarantees a good enough pivot. The following algorithm, called the big five algorithm, both because of the cardinality and stature of its inventors (Blum, Floyd, Pratt, Rivest, and Tarjan) and because of the importance of the number 5 in its design, implements this idea. function BigFive(S, k) is returns the k-th smallest key in the set of keys S Put the keys of S into groups of 5 and sort each group Let M = {y y is the 3rd smallest key (median) in its group } Let x = BigFive(S, S /10 + 1) x is the median of M, i.e. the median of medians Use x as a pivot to divide S {x} into two subsets: L = {y S {x} y x} U = {y S {x} y > x} if L k then return BigFive(L, k) else if L = k 1 then return x else return BigFive(U, k L 1) endif end BigFive The picture below shows the results of the comparisons after x has been chosen.... x... Suppose, to keep the analysis simple, that the original set has n = 5(2r + 1) keys, for some r. Regardless of where k falls in relation to the pivot, at least 3(r + 1) keys can always be eliminated from further consideration, which means that at most 7r + 2 keys can be involved in the next recursive call. This means the set of keys is reduced to at most 70% of its original size, a process that by itself would yield a linear number of comparisons. There is, however, another recursive call, the one that computes the median of the set M. So the recurrence ends up being T (n) T ( n 5 ) median of M + T ( 7 10 n) + recurse on max( L, U ) c 5 5 n sort n/5 groups of 5 + n 1 partition with T (n) constant when n is small (for example, the recurrence no longer makes sense if n 25 at that point T (n/5) is going to require at most c 5 comparsions). Here c 5 is the number of comparisons required to find the third largest of 5 keys (because mergesort can do this in 8 comparisons, we know c 5 8). Again, let s prove by induction that the solution is O(n) I mean (let s be sure to phrase this carefully) that there exist c and n 0 such that T (n) cn when n n 0. Appropriate choices for c and n 0 will become clear after working through the details of the proof.

CSC 505, Spring 2005 Week 6 Lectures page 4 For the basis, we only need to ensure that c is chosen large enough so that T (n) cn for all n < n 0. This can easily be done since there are only a finite number of n for which the inequality must be satisfied. The induction hypothesis is that T (i) ci for all i < n and we need to show that T (n) cn. Using the recurrence relation and substituting the hypothesis we have T (n) c n 5 + c7n 10 + c 5n 5 + n 1 ( 9c = 10 + c ) 5 5 + 1 n 1 cn when c 2c 5 + 10 So c = 26 and n 0 = 25 will work (the number of comparisons for mergesort when n = 25 is far less than 16 25 = 400; even insertion sort with 12 25 = 300 comparisons will work for the basis). The analysis in the text differs in the details in two important ways: 1. It does not assume that n is an odd multiple of 5. This introduces n/5 in place of n/5 and 7n/10+6 in place of 7n/10 for the recursive calls. The +6 is an overestimate it assumes that any group with less than 5 elements contributes nothing and that the middle group contributes nothing (in case the number of groups is even rather than odd); it also does not account for the fact that n is smaller when there is an incomplete groups. But this does not matter. 2. It takes the total time for the partitioning combined with that for sorting groups of 5 to be an. The only effect on our analysis would be that we would get ( 9c T (n) 10 + c ) 5 5 + 1 n + 7c 1. (the 7c comes from substituting n/5 + 1 for n/5 plus the 6c from the 7n/10 + 6). This is cn as long as c > 2c 5 + 10 and n (70c 10)/(c 2c 5 10). It s clear that we can choose c and n 0 to make this happen. Dynamic programming as an alternative to recursion Recursive formula for the Fibonnaci numbers: f 0 = f 1 = 1 f n = f n 1 + f n 2 for n > 1 and the obvious recursive algorithm for computing them: function Fib(n) is if n = 0 or n = 1 then return 1 else return Fib(n 1)+Fib(n 2) end Fib Time bound analysis (using deliberate underestimate to simplify): T (n) 2T (n 2) + c 1, T (0) = T (1) = c 0 T (n) c 0 2 n/2 + c 1 (2 n/2 1) Ω(2 n/2 ) Actually, T (n) (c 0 + c 1 )(1.6) n, the 1.6 being the Golden ratio (1 + 5)/2. But there are lots of duplicate recursive calls.

CSC 505, Spring 2005 Week 6 Lectures page 5 1 1 2 3 5 8 13 0 1 2 3 4 5 6 Figure 1: How the n-th Fibonnaci number depends on previous entries. n 1 n n 2 n 2 n 3 n 3 n 4 The duplication can be avoided if we remember that the call for a particular value has already been done and retrieve the return value (saved earlier). The text calls this technique memoizing. Here s the algorithm that does it. Assume done[i] = false for i = 0,..., n function Fib(n) is if n = 0 or n = 1 then fib[n] 1 else if not done[n] then fib[n] Fib(n 1)+Fib(n 2) done[n] true; return f ib[n] end Fib Instead of using augmenting a recursive algorithm by filling in a table to avoid dupliucated calls, why not skip the recursion altogether. This works as long as the table can be filled in such a way that each value depends only on previously computed values. This is the essence of dynamic programming (see Fig. 1). function Fib(n) is fib[0] fib[1] 1 for i 2 to n do fib[i] fib[i 1] + fib[i 2] return f ib[n] end Fib Longest common subsequence Given two strings x and y, find the longest sequence of symbols that occurs as a subsequence of both. For example, let x = abcbaabbcba and y = bacabacbba. Then acaba is a subsequence: a b c b a abbc b a b a c ab a c b b a but is it a longest subsequence? We can t make it longer on either end, but consider

CSC 505, Spring 2005 Week 6 Lectures page 6 b a b c b b a C[i, j] 1 2 3 4 5 6 7 b 1 1 1 1 1 1 1 1 b 2 1 1 2 2 2 2 2 c 3 1 1 2 3 3 3 3 a 4 1 2 b 5 1 2 a 6 1 2 Figure 2: Computing the longest common subsequence of babcbba and bbcaba. a b c b a abb c b a b a c a b a c b b a Brute force algorithm takes Θ(min(m, n) 4 min(m,n) ), try all possible subsequences of length up to min(m, n) (there are 2 2j = 4 j ways to choose a subsequence of length j in each string). and compare them (comparing two subsequences of length j requires j comparisons of symbols). So we have (letting k = min m, n): k j=1 j 4 j = 4(1 4k ) (1 4) 2 k 4k+1 1 4 = Θ(min(m, n) 4min(m,n) ) A recursive formulation of a simpler problem: finding the length of the subsequence C[i, j] = length of LCS of x[1... i], y[1... j] C[i, j] = 0 if i = 0, j = 0,..., n C[i 1, j 1] + 1 if x[i] = y[j] max(c[i, j 1], C[i 1, j]) otherwise There are only Θ(mn) possible subproblems (the number of possible i, j pairs). This suggests a solution derived from a table in which each entry depends only on those of the previous row or of the previous column in the current row. See, for example, the table in Fig. 2. Details of the algorithm are in Fig. 3. The actual longest subsequence is easy to compute from the table (the extra table of arrows in the text is unnecessary since the direction of the arrow can be deduced from the numbers in the table). Execution time is Θ(mn) (pretty obvious) - a constant amount of work per table entry. The number of permutations with a specific number of inversions P (n, k) = no. of permutations of n items with exactly k inversions = 0 if k < 0 or k > n(n 1)/2 n 1 P (n 1, k i) otherwise i=0

CSC 505, Spring 2005 Week 6 Lectures page 7 function Lcs(x, y) is x is a string of m characters, y has n characters returns z,the longest common subsequence of x and y for j 0 to n do C[0, j] 0 for i 1 to m, j 1 to n do if x[i] = y[j] then C[i, j] C[i 1, j 1] + 1 else C[i, j] max(c[i, j 1], C[i 1, j]) Get-Seq(z, m, n, C[m, n]); return z end Lcs procedure Get-Seq(z, i, j, k) is uses C[1... i, 1... j] table to figure out z[1... k] while i > 0 and j > 0 and k > 0 do if x[i] = y[j] then z[k] x[i]; i i 1; j j 1; k k 1; else if C[i, j 1] = k then j j 1 else i i 1 end Get-Seq Figure 3: Algorithms for computing the length of a longest common subsequence and retrieving the actual sequence. P [n, k] 0 1 2 3 4 5 6 7 8 9 10 1 1 0 0 0 0 0 0 0 0 0 0 2 1 1 0 0 0 0 0 0 0 0 0 3 1 2 2 1 0 0 0 0 0 0 0 4 1 3 5 6 5 3 1 0 0 0 0 5 1 4 6 1 5 Figure 4: Computing all permutations of 6 or fewer elements with specific numbers of inversions.

CSC 505, Spring 2005 Week 6 Lectures page 8 function Get-P(n, k) is if k < 0 or k > n (n 1)/2 then return 0 else return P [n, k] end Get-P function Permutations(n, k) is P [1, 0] 1 for i 2 to n do for j 0 to min(k, i (i 1)/2) do sum 0 for h 0 to i 1 do sum sum + Get-P(i 1, j h) P [i, j] sum return Get-P(n, k) end Permutations Figure 5: Algorithm for computing number of permuations having a particular number of inversions. melon 1 x 0.1 0.1 cherry peach 0.8 3 x 0.3 apple pear 1.5 total = 2.4 Figure 6: A balanced search tree for apple, cherry, melon, peach, and pear. The table for computing P (6, 10) is shown in Fig. 4 (partially filled in). The algorithm, as shown in Fig. 5, is pretty simple Get-P is used to avoid any out-of-bounds array references. Optimal binary search trees Unlike the book, we assume here that every search is successful. We are given a set of n keys and access probabilities p 1,..., p n so that the probability that the i-th key will be accessed is p i. The search tree has to organize the keys so that the key at the root of each subtree is greater than (with respect to the ordering of keys) all keys in its left subtree and less than all keys in its right subtree. The goal is to minimize the expected number of comparisons for a search. For example, suppose the words apple, cherry, melon, peach, and pear had access probabilities 0.3, 0.2, 0.1, 0.2, and 0.2, respectively. We might try to construct a balanced tree see Fig. 6. Or we might try a greedy approach by putting the most frequently accessed item at the root, as in Fig. 7. A slight improvement, but is it the best we can do? Even with such a small example, it would be daunting to enumerate all possible search trees.

CSC 505, Spring 2005 Week 6 Lectures page 9 1 x 0.3 apple 0.3 peach 0.4 cherry pear 1.2 melon 4 x 0.1 0.4 total = 2.3 Figure 7: A greedy search tree for apple, cherry, melon, peach, and pear. total = 0.8 1 x 0.2 cherry melon 1 x 0.1 peach 0.1 0.8 3 x 0.3 2 x 0.3 apple pear 1.5 total = 2.4 2 x 0.3 1 x 0.3 total = 0.7 apple melon 1 x 0.1 peach 0.1 1.0 cherry pear 1.2 total = 2.3 Figure 8: Replacing a subtree to get a better overall tree. Principle of optimality To show that this problem is a candidate for dynamic programming we introduce the principle of optimality: each instance of the problem can be reduced to a collection of subinstances so that an optimal solution to the original instance implies that each subinstance must also be solved optimally. In case of the longest common subsequence problem, every subinstance asked for a longest common subsequence between a prefix of x and a prefix of y. The longest common subsequence of x 1 x 2... x i and y 1 y 2... y j was either the lcs of x 1 x 2... x i 1 and y 1 y 2... y j 1 concatenated with x i (if x i = y j ), or the lcs of x 1 x 2... x i and y 1 y 2... y j 1, or the lcs of x 1 x 2... x i 1 and y 1 y 2... y j. In each case we use the lcs of prefixes whose combined length is shorter to deduce the lcs of the original instance. If we have an lcs z of x and y and we look at the prefix z 1,..., z k of z that is common to x 1 x 2... x i and y 1 y 2... y j, then z 1,..., z k must be an lcs of x 1 x 2... x i and y 1 y 2... y j otherwise we could replace z 1,..., z k with a longer subsequence of x 1 x 2... x i and y 1 y 2... y j and obtain a longer subsequence for all of x and y. Principle of optimality applied to binary search trees Suppose the root of the search tree has been chosen. This is a decision the algorithm must make, but we assume it when proving the principle of optimality. I claim that both the left and the right subtree must be optimal binary search trees with respect to the frequencies of the items in them (no longer probabilities because they don t add up to 1). Otherwise, we could replace the subtree with one that had a smaller expected number of comparisons and get a smaller number for the whole tree. See Fig. 8.

CSC 505, Spring 2005 Week 6 Lectures page 10 r L G Figure 9: Recursive formulation of the optimal binary search tree. The principle of optimality gives us a recursive formulation describing any instance in terms of subinstances. For binary search trees, it goes as illustrated in Fig. 9 (assuming the root r has been chosen). A(T ) = average access cost in tree T ( r 1 ) P (i) = p r + contribution of root i=1 every key on the left (1 + A(L)) + contributes expected cost+1 ( n i=r+1 P (i) ) (1 + A(G)) same with each key on the right Initially the left subtree begins with the first (least) key and the right subtree ends with the last (greatest). But once L and G are split into further subtrees, a general subinstance will be a tree containing the i-th through j-th key for arbitrary i and j (the keys in a subtree must be consecutive). Since we don t know how to choose the root, we must try out every possibility. Let T i,j be the optimal tree on keys i,..., j. P [i, j] = j k=i p k A[i, j] = P [i, j] A(T i,j ) R[i, j] = the root of T i,j A[i, j] = P [i, j] + A[i, R[i, j] 1] + A[R[i, j] + 1, j] = P [i, j] + min (A[i, r 1] + A[r + 1, j]) i r j And A[i, j] = 0 when T i,j is empty, i.e. when j < i. On the example, the table representing A[i, j] would be filled in as shown in Fig. 10. As usual we compute a single number, the average access cost of the optimal tree, instead of the structure. The R[i, j] array can be filled in as we go along to allow us to reconstruct the tree (these are the numbers in the second row of each table entry). A detailed description of the algorithm is in Fig. 11.

CSC 505, Spring 2005 Week 6 Lectures page 11 A[i, j] R[i, j] 0 1 apple 2 cherry 3 melon 4 peach 5 pear 1 0.0 0.3 0.7 1.0 apple 1 1 1 2 0.0 0.2 0.4 0.9 cherry 2 2 2 3 0.0 0.1 0.4 0.8 melon 3 4 4 4 0.0 0.2 0.6 peach 4 4 5 0.0 0.2 pear 5 Figure 10: Table for computing the optimal binary search tree for the five fruit. function Opt-Bin-Tree(p[1,..., n]) is p[i] is the probability that the i-th item in a sorted sequence of n items is searched for. The function returns the expected number of comparisons for an optimal search tree. The R matrix can be used to recover the actual tree. for i 1 to n do A[i, i 1] 0.0; P [i, i 1] 0.0 for d 0 to n 1 do d represents j i for i 1 to n d do j i + d Min min i r j (A[i, r 1] + A[r + 1, j]) R[i, j] the r that achieved the minimum P [i, j] P [i, j 1] + p[j]; A[i, j] Min + P [i, j] return A[1, n] end Opt-Bin-Tree Figure 11: Algorithms for computing cost of optimal binary search tree and retrieving the tree.