Sei sulla pagina 1di 3

CS 102: INTRO.

TO THE ANALYSIS OF ALGORITHMS ASSIGNMENT 3 SOLUTIONS

Problem 1.

(a). T (n) = T ( 9n ) + n. 10

(b). T (n) = 2T ( n) + log n. Solution. (a). T (n) = T (

9n ) + n. 10 This is a divide-and-conquer recurrence with a = 1, b = 10/9, f (n) = n, and nlogb a = nlog10/9 1 = n0 = 1. This is case 3 of the master theorem, thus T (n) (n). (b). T (n) = 2T ( n) + log n. Let m = lg n or that n = 2m . This T (n) = T (2m ) = 2T ( 2m ) + m = 2T (2m/2 )+m. Now we dene S(m) = T (2m ). Therefore S(m) = S(m/2)+m. We can apply the master theorem to get that S(m) (m lg m). f we substitute m = lg n back in, T (n) = S(lg n) ((lg n) lg lg n).

Problem 2.

(a). n T (n) = 16T ( ) + n2 . 4

(b). T (n) = T ( n) + 1. Solution. (a). n T (n) = 16T ( ) + n2 . 4 Here we have that a = 14, b = 4 and f (n) = n2 . Thus logb a = log4 16 = 2. This puts us in the second case of the master theorem where f (n) = n2 (nlogb a ) = (n2 ). As is implied by case two of the master theorem we conclude that T (n) (n2 log n).
1

CS 102: INTRO. TO THE ANALYSIS OF ALGORITHMS

ASSIGNMENT 3

SOLUTIONS 2

(b).

Let m = lg n or that n = 2m . Thus T (n) = T (2m ) = T ( 2m ) + 1 = T (2m/2 ) + 1. Now we dene S(m) = T (2m ). Therefore S(m) = S(m/2) + 1. We can apply the master theorem to this to get that S(m) (lg m). If we substitute m = lg n back in, T (n) = S(lg n) (lg lg n). Problem 3. Analyze the running time of the following recursive procedure as a function of n. You may assume that each assignment or division takes unit time. An asymptotic analysis is ne. Procedure DC(int n) if n < 2 then return; else count := 0; for i := 1 to 8 do DC(n div 2); for i:=1 to n^3 do count := count + 1; Solution. Let us refer to the running time of DC(n) as T (n). Since DC(n) is a recursive procedure we can write T (n) as a recursive function. Line (2) is the base case, if n < 2 then the running time is unit cost. Lines (3-8) are the general case. Lines (5-6) take time 8 T (n/2) and lines (7-8) take n3 . Adding in an extra 1 for evaluating the condition in line (2) and the initialization in line (4), we see that calls with n 2 takes 8T (n/2) + n3 + 1 time units. We have now dened T (n) as follows: T (n) = 1 if n < 2, 8T (n/2) + n3 + 1 otherwise.

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

We can use the master theorem to translate it into -notation. We have b = 8, c = 2 and f (n) = n3 . Since f (n) (nlog2 8 ) = (n3 ), case 2 applies so T (n) (f (n) log n) = (n3 log n). Problem 4. Find an asymptotic upper bound for the recurrence T (n) = T (n a) + T (a) + n where a 1 is constant, by using recursion tree and/or iteration to generate a guess (see the next problem). Solution. We assume that T (n) (1) for n a, i.e. that T (1), T (2), . . . , T (a) are all constants. To get a guess we iterate a few times. T (n) = T (n a) + T (a) + n = T (n 2a) + [T (a) + n a] + [T (a) + n] = T (n 3a) + [T (a) + n 2a] + [T (a) + n a] + [T (a) + n] = T (n 4a) + [T (a) + n 3a] + [T (a) + n 2a] + [T (a) + n a] + [T (a) + n]

CS 102: INTRO. TO THE ANALYSIS OF ALGORITHMS

ASSIGNMENT 3

SOLUTIONS 3

The bracketed terms are of order O(n). Every iteration add a term of order O(n). Now consider how many times does it take for this recurrence to reach one of the base cases, i.e. how many time we add a term of order O(n2 ). This happens after k iterations where n ka is an integer between 1 and a, or when k = n/a . For every iteration we add a factor of O(n). Thus we can guess that T (n) O(kn) = O( n/a n) = O(n2 ). Problem 5. Complete the substitution method by proving (using strong induction) that the guess generated above is indeed an asymptotic upper bound for the recurrence T (n) = T (n a) + T (a) + n Solution. Now to prove that T (n) O(n2 ). We will use strong induction to prove that T (n) cn2 for some c. This will imply that T (n) O(n2 ). Assume that T (n) cn2 is true for all arguments less than some n. Later well make sure to pick our initial conditions to cover values at least up to n = a. T (n) = T (n a) + T (a) + n c(n a)2 + ca2 + n = cn2 2cna + 2ca2 + n = cn2 (2cna 2ca2 n) cn2 , if 2cna 2ca2 n 0. Therefore, 2cna 2ca2 n 0 c(2na 2a2 ) n For n 2a we have that c n 2na 2a2 1 = 2 2a 2a n

Since n 2a, this last term is less than or equal to 1/a. Thus if we choose c 1/a, 2cna 2ca2 n 0 as desired. No we pick c such that c 1/a and so that all initial conditions are satised, i.e. large enough so that T (n) < cn2 for 0 < n < 2a. With this choice of c we have that T (n) cn2 for all n.

Potrebbero piacerti anche