divide and conquer-patterns

# Chapter 5 fundamental techniques 264 the iterative

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: recurrence equation that is more challenging to solve than this one. Thus, it is useful to develop some general ways of solving the kinds of recurrence equations that arise in the analysis of divide-and-conquer algorithms. Chapter 5. Fundamental Techniques 264 The Iterative Substitution Method One way to solve a divide-and-conquer recurrence equation is to use the iterative substitution method, which is more colloquially known as the “plug-and-chug” method. In using this method, we assume that the problem size n is fairly large and we then substitute the general form of the recurrence for each occurrence of the function T on the right-hand side. For example, performing such a substitution with the merge-sort recurrence equation yields the equation T (n) = 2(2T (n/22 ) + b(n/2)) + bn = 22 T (n/22 ) + 2bn. Plugging the general equation for T in again yields the equation T (n) = 22 (2T (n/23 ) + b(n/22 )) + 2bn = 23 T (n/23 ) + 3bn. The hope in applying the iterative substitution method is that, at some point, we will see a pattern that can be converted into a general closed-form equation (with T only appearing on the left-hand side). In the case of the merge-sort recurrence equation, the general form is T (n) = 2i T (n/2i ) + ibn. Note that the general form of this equation shifts to the base case, T (n) = b, when n = 2i , that is, when i = log n, which implies T (n) = bn + bn log n. In other words, T (n) is O(n log n). In a general application of the iterative substitution technique, we hope that we can determine a general pattern for T (n) and that we can also ﬁgure out when the general form of T (n) shifts to the base case. From a mathematical point of view, there is one point in the use of the iterative substitution technique that involves a bit of a logical “jump.” This jump occurs at the point where we try to characterize the general pattern emerging from a sequence of substitutions. Often, as was the case with the merge-sort recurrence equation, this jump is quite reasonable. Other times, however, it may not be so obvious what a general form for the equation should look like. In these cases, the jump may be more dangerous. To be completely safe in making such a jump, we must fully justify the general form of the equation, possibly using induction. Combined with such a justiﬁcation, the iterative substitution method is completely correct and an often useful way of characterizing recurrence equations. By the way, the colloquialism “plug-and-chug,” used to describe the iterative substitution method, comes from the way this method involves “plugging” in the recursive part of an equation for T (n) and then often “chugging” through a considerable amount of algebra in order to get this equation into a form where we can infer a general pattern. 5.2. Divide-and-Conquer 265 The Recursion Tree Another way of characterizing recurrence equations is to use the recursion tree method. Like the iterative substitution method, this technique uses repeated substitution to solve a recurrence equation, but it differs from the iterative substitution method in that, rather than being an algebraic approach, it is a visual approach. In using the recursion tree method, we draw a tree R where each node represents a different substitution of the recurrence equation. Thus, each node in R has a value of the argument n of the function T (n) associated with it. In addition, we associate an overhead with each node v in R, deﬁned as the value of the nonrecursive part of the recurrence equation for v. For divide-and-conquer recurrences, the overhead corresponds to the running time needed to merge the subproblem solutions coming from the children of v. The recurrence equation is then solved by summing the overheads associated with all the nodes of R. This is commonly done by ﬁrst summing values across the levels of R and then summing up these partial sums for all the levels of R. Example 5.3: Consider the following recurrence equation: T (n) = if n < 3 b 3T (n/3) + bn if n ≥ 3. This is the recurrence equation that we get, for example, by modifying the mergesort algorithm so that we divide an unsorted sequence into three equal-sized sequences, recursively sort each one, and then do a three-way merge of three sorted sequences to produce a sorted version of the original sequence. In the recursion tree R for this recurrence, each internal node v has three children and has a size and an overhead associated with it, which corresponds to the time needed to merge the subproblem solutions produced by v’s children. We illustrate the tree R in Figure 5.4. Note that the overheads of the nodes of each level sum to bn. Thus, observing that the depth of R is log3 n, we have that T (n) is O(n log n). Overhead bn bn bn Figure 5.4: The recursion tree R used in Example 5.3, where we show the cumula- tive overhead of each level. Chapter 5. Fundamental Techniques 266 The Guess-and-Test Method Another method for solving recurrence equations is the guess-and-test technique. This technique involves ﬁrst making an educated guess as to...
View Full Document

## This document was uploaded on 03/26/2014.

Ask a homework question - tutors are online