• Nem Talált Eredményt

Floors and ceilings

In document 3 Growth of Functions (Pldal 41-49)

4.3 The master method

4.4.2 Floors and ceilings

To complete the proof of the master theorem, we must now extend our analysis to the situation in which floors and ceilings are used in the master recurrence, so that the recurrence is defined for all integers, not just exact powers ofb. Obtaining a lower bound on

T(n)=aT(⌈n/b⌉)+ f(n) (4.10)

and an upper bound on

T(n)=aT(⌊n/b⌋)+ f(n) (4.11)

is routine, since the bound ⌈n/b⌉ ≥ n/bcan be pushed through in the first case to yield the desired result, and the bound⌊n/b⌋ ≤ n/bcan be pushed through in the second case. Lower bounding the recurrence (4.11) requires much the same technique as upper bounding the recurrence (4.10), so we shall present only this latter bound.

We modify the recursion tree of Figure 4.3 to produce the recursion tree in Fig-ure 4.4. As we go down in the recursion tree, we obtain a sequence of recursive invocations on the arguments

n,

n/b⌉ ,

⌈⌈n/b⌉/b⌉ ,

⌈⌈⌈n/b⌉/b⌉/b⌉ , ...

Let us denote the jth element in the sequence bynj, where nj =

n if j =0,

nj1/b⌉ if j >0. (4.12)

Our first goal is to determine the depthk such that nk is a constant. Using the inequality⌈x⌉ ≤x+1, we obtain

4.4 Proof of the master theorem 83

From Figure 4.4, we see that T(n)=2(nlogba)+

logXbn⌋−1 j=0

ajf(nj) , (4.13)

which is much the same as equation (4.6), except thatnis an arbitrary integer and not restricted to be an exact power ofb.

We can now evaluate the summation g(n)=

⌊logXbn⌋−1 j=0

ajf(nj) (4.14)

from (4.13) in a manner analogous to the proof of Lemma 4.3. Beginning with case 3, ifa f(⌈n/b⌉)≤c f(n)forn> b+b/(b−1), wherec<1 is a constant,

sufficiently largenj, which is similar to the corresponding proof of case 2, though the algebra is more intricate.

We have now proved the upper bounds in the master theorem for all integersn.

The proof of the lower bounds is similar.

Exercises 4.4-1

Give a simple and exact expression fornjin equation (4.12) for the case in whichb is a positive integer instead of an arbitrary real number.

4.4-2

Show that if f(n)=2(nlogbalgkn), wherek ≥0, then the master recurrence has solutionT(n) =2(nlogbalgk+1n). For simplicity, confine your analysis to exact powers ofb.

4.4-3

Show that case 3 of the master theorem is overstated, in the sense that the regularity conditiona f(n/b) ≤ c f(n)for some constant c < 1 implies that there exists a constantǫ >0 such that f(n)=(nlogba+ǫ).

Problems for Chapter 4 85

Problems

4-1 Recurrence examples

Give asymptotic upper and lower bounds forT(n)in each of the following recur-rences. Assume that T(n)is constant forn ≤ 2. Make your bounds as tight as possible, and justify your answers.

a. T(n)=2T(n/2)+n3. b. T(n)=T(9n/10)+n.

c. T(n)=16T(n/4)+n2. d. T(n)=7T(n/3)+n2. e. T(n)=7T(n/2)+n2. f. T(n)=2T(n/4)+√

n.

g. T(n)=T(n−1)+n.

h. T(n)=T(√ n)+1.

4-2 Finding the missing integer

An arrayA[1. .n] contains all the integers from 0 tonexcept one. It would be easy to determine the missing integer inO(n)time by using an auxiliary arrayB[0. .n]

to record which numbers appear in A. In this problem, however, we cannot access an entire integer in A with a single operation. The elements of Aare represented in binary, and the only operation we can use to access them is “fetch the jth bit ofA[i],” which takes constant time.

Show that if we use only this operation, we can still determine the missing inte-ger inO(n)time.

4-3 Parameter-passing costs

Throughout this book, we assume that parameter passing during procedure calls takes constant time, even if anN-element array is being passed. This assumption is valid in most systems because a pointer to the array is passed, not the array itself.

This problem examines the implications of three parameter-passing strategies:

1. An array is passed by pointer. Time=2(1).

2. An array is passed by copying. Time=2(N), whereNis the size of the array.

3. An array is passed by copying only the subrange that might be accessed by the called procedure. Time=2(q−p+1)if the subarray A[p. .q] is passed.

a. Consider the recursive binary search algorithm for finding a number in a sorted array (see Exercise 2.3-5). Give recurrences for the worst-case running times of binary search when arrays are passed using each of the three methods above, and give good upper bounds on the solutions of the recurrences. LetN be the size of the original problem andnbe the size of a subproblem.

b. Redo part (a) for the MERGE-SORTalgorithm from Section 2.3.1.

4-4 More recurrence examples

Give asymptotic upper and lower bounds forT(n)in each of the following recur-rences. Assume thatT(n)is constant for sufficiently smalln. Make your bounds as tight as possible, and justify your answers.

a. T(n)=3T(n/2)+nlgn.

b. T(n)=5T(n/5)+n/lgn.

c. T(n)=4T(n/2)+n2n.

d. T(n)=3T(n/3+5)+n/2.

e. T(n)=2T(n/2)+n/lgn.

f. T(n)=T(n/2)+T(n/4)+T(n/8)+n.

g. T(n)=T(n−1)+1/n.

h. T(n)=T(n−1)+lgn.

i. T(n)=T(n−2)+2 lgn.

j. T(n)=√ nT(√

n)+n.

4-5 Fibonacci numbers

This problem develops properties of the Fibonacci numbers, which are defined by recurrence (3.21). We shall use the technique of generating functions to solve

Problems for Chapter 4 87

the Fibonacci recurrence. Define thegenerating function(or formal power se-ries)F as whereFiis theith Fibonacci number.

a. Show thatF(z)=z+zF(z)+z2F(z).

5 fori >0, rounded to the nearest integer. (Hint:Observe that|bφ|<1.)

e. Prove thatFi+2≥φifori ≥0.

4-6 VLSI chip testing

Professor Diogenes hasn supposedly identical VLSI1 chips that in principle are capable of testing each other. The professor’s test jig accommodates two chips at

1VLSI stands for “very large scale integration,” which is the integrated-circuit chip technology used to fabricate most microprocessors today.

a time. When the jig is loaded, each chip tests the other and reports whether it is good or bad. A good chip always reports accurately whether the other chip is good or bad, but the answer of a bad chip cannot be trusted. Thus, the four possible outcomes of a test are as follows:

ChipAsays ChipBsays Conclusion

Bis good Ais good both are good, or both are bad Bis good Ais bad at least one is bad

Bis bad Ais good at least one is bad Bis bad Ais bad at least one is bad

a. Show that if more thann/2 chips are bad, the professor cannot necessarily de-termine which chips are good using any strategy based on this kind of pairwise test. Assume that the bad chips can conspire to fool the professor.

b. Consider the problem of finding a single good chip from amongnchips, as-suming that more thann/2 of the chips are good. Show thatn/2⌋pairwise tests are sufficient to reduce the problem to one of nearly half the size.

c. Show that the good chips can be identified with2(n)pairwise tests, assuming that more thann/2 of the chips are good. Give and solve the recurrence that describes the number of tests.

4-7 Monge arrays

Anm×narray Aof real numbers is aMonge arrayif for alli, j,k, andl such that 1≤i <kmand 1≤ j <ln, we have

A[i, j]+A[k,l]A[i,l]+A[k, j].

In other words, whenever we pick two rows and two columns of a Monge array and consider the four elements at the intersections of the rows and the columns, the sum of the upper-left and lower-right elements is less or equal to the sum of the lower-left and upper-right elements. For example, the following array is Monge:

10 17 13 28 23 17 22 16 29 23 24 28 22 34 24

11 13 6 17 7

45 44 32 37 23 36 33 19 21 6 75 66 51 53 34

a. Prove that an array is Monge if and only if for all i = 1,2, ...,m −1 and j =1,2, ...,n−1, we have

Notes for Chapter 4 89

A[i,j]+A[i+1, j+1]≤ A[i,j +1]+A[i+1,j].

(Hint:For the “only if” part, use induction separately on rows and columns.) b. The following array is not Monge. Change one element in order to make it

Monge. (Hint:Use part (a).) 37 23 22 32

21 6 7 10

53 34 30 31

32 13 9 6

43 21 15 8

c. Let f(i)be the index of the column containing the leftmost minimum element of rowi. Prove that f(1)≤ f(2)≤ · · · ≤ f(m)for anym×nMonge array.

d. Here is a description of a divide-and-conquer algorithm that computes the left-most minimum element in each row of anm×nMonge arrayA:

Construct a submatrixAofAconsisting of the even-numbered rows ofA.

Recursively determine the leftmost minimum for each row of A. Then compute the leftmost minimum in the odd-numbered rows ofA.

Explain how to compute the leftmost minimum in the odd-numbered rows ofA (given that the leftmost minimum of the even-numbered rows is known) in O(m+n)time.

e. Write the recurrence describing the running time of the algorithm described in part (d). Show that its solution isO(m+nlogm).

Chapter notes

Recurrences were studied as early as 1202 by L. Fibonacci, for whom the Fibonacci numbers are named. A. De Moivre introduced the method of generating functions (see Problem 4-5) for solving recurrences. The master method is adapted from Bentley, Haken, and Saxe [41], which provides the extended method justified by Exercise 4.4-2. Knuth [182] and Liu [205] show how to solve linear recurrences using the method of generating functions. Purdom and Brown [252] and Graham, Knuth, and Patashnik [132] contain extended discussions of recurrence solving.

Several researchers, including Akra and Bazzi [13], Roura [262], and Verma [306], have given methods for solving more general divide-and-conquer recur-rences than are solved by the master method. We describe the result of Akra and Bazzi here, which works for recurrences of the form

In document 3 Growth of Functions (Pldal 41-49)