classes.soe.ucsc.edu cmps102 fall01 solutions 3

Upload: ashok-chakravarthy

Post on 05-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 Classes.soe.Ucsc.edu Cmps102 Fall01 Solutions 3

    1/3

    CS 102: INTRO. TO THE ANALYSIS OF ALGORITHMS

    ASSIGNMENT 3

    SOLUTIONS

    Problem 1. (a).

    T(n) = T(9n

    10) + n.

    (b).

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

    T(n) = T(9n

    10) + n.

    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, thusT(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 define 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 wesubstitute m = lg n back in,

    T(n) = S(lg n) ((lg n)lglg n).

    Problem 2. (a).

    T(n) = 16T(n

    4) + n2.

    (b).

    T(n) = T(

    n) + 1.

    Solution. (a).

    T(n) = 16T(n

    4) + n2.

    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

  • 8/2/2019 Classes.soe.Ucsc.edu Cmps102 Fall01 Solutions 3

    2/3

    CS 102: INTRO. TO THE ANALYSIS OF ALGORITHMS ASSIGNMENT 3 SOLUTIONS 2

    (b).

    T(n) = T(

    n) + 1.

    Let m = lg n or that n = 2m. Thus T(n) = T(2m) = T(2m) + 1 =T(2m/2) + 1. Now we define 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 wesubstitute 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 afunction of n. You may assume that each assignment or division takes unit time.An asymptotic analysis is fine.

    Procedure DC(int n)

    if n < 2 then return;

    else

    count := 0;

    f o r i : = 1 t o 8 d o

    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 arecursive procedure we can write T(n) as a recursive function. Line (2) is the basecase, 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 forevaluating 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 defined T(n) asfollows:

    T(n) =

    1 if n < 2,

    8T(n/2) + n3 + 1 otherwise.

    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/oriteration

    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) areall 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]

  • 8/2/2019 Classes.soe.Ucsc.edu Cmps102 Fall01 Solutions 3

    3/3

    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(n

    2

    ). This happens afterk iterations where n ka is an integer between 1 and a, or when k = n/a. Forevery iteration we add a factor of O(n). Thus we can guess that T(n) O(kn) =O(n/an) = O(n2). Problem 5. Complete the substitution method by proving (using strong induc-tion) that the guess generated above is indeed an asymptotic upper bound for therecurrence

    T(n) = T(n a) + T(a) + nSolution. Now to prove that T(n) O(n2). We will use strong induction to provethat T(n) cn2 for some c. This will imply that T(n) O(n2). Assume thatT(n) cn2 is true for all arguments less than some n. Later well make sure topick 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 thatc n

    2na 2a2=

    1

    2a 2a2nSince 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 allinitial conditions are satisfied, 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.