04.09.2013 Views

Algorithm Design

Algorithm Design

Algorithm Design

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

214<br />

Chapter 5 Divide and Conquer<br />

An Approach Using Partial Substitution<br />

There is a somewhat weaker kind of substitution one can do, in which one<br />

guesses the overall form of the solution without pinning down the exact values<br />

of all the constants and other parameters at the outset.<br />

Specifically, suppose we believe that T(n)= O(n log n), but we’re not<br />

sure of the constant inside the O(-) notation. We can use the substitution<br />

method even without being sure of this constant, as follows. We first write<br />

T(n) 2, and<br />

T(n) 2 and<br />

q = 1 separately, since they are qualitatively different from each other--and<br />

different from the case q = 2 as well.<br />

The Case of q > 2 Subproblems<br />

We begin by unro!ling (5.3) in the case q > 2, following the style we used<br />

earlier for (5.1). We will see that the punch line ends up being quite different.<br />

Analyzing the first few levels: We show an example of this for the case<br />

q = 3 in Figure 5.2. At the first level of recursion, we have a single<br />

problem of size n, which takes time at most cn plus the time spent in all<br />

subsequent recursive calls. At the next level, we have q problems, each<br />

of size n/2. Each of these takes time at most cn/2, for a total of at most<br />

(q/2)cn, again plus the time in subsequent recursive calls. The next level<br />

yields q2 problems of size n/4 each, for a total time of (q2/4)cn. Since<br />

q > 2, we see that the total work per level is increasing as we proceed<br />

through the recursion.<br />

Identifying apattern: At an arbitrary levelj, we have qJ distinct instances,<br />

each of size n/2]. Thus the total work performed at level j is qJ(cn/2]) =<br />

(q/2)icn.<br />

215

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!