Analysis of Recurrence Relations using the Master Theorem, Assignments of Electrical and Electronics Engineering

Solutions to various recurrence relations using the master theorem. It includes examples of recurrences that fit each of the three cases of the master theorem and explains how to determine which case applies. The recurrences involve functions such as n^2, n^(2 log_4 2), n^(2 log_2 7), and n^(1/2 log n), and the solutions are given in big o notation.

Typology: Assignments

Pre 2010

Uploaded on 07/23/2009

koofers-user-kej-2
koofers-user-kej-2 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ECE 537 - Foundations of Computing
Prof. Sen
Homework #6
Solutions
1. Work problem 4.1-1 in the Cormen et. al text.
Solution:
We must show that
T(n) = clg n
for an appropriate choice of c, and for all nwhen nis sufficiently large. Substituting this into the
recurrence we get
T(n)clg n
2+ 1
=clg n+ 1 lg 2
=clg n
which verifies that T(n) = O(lg n).
2. Work problem 4.1-6 in the Cormen et. al text.
Solution:
Let m= lg n, then 2m=nand n= 2m/2. Thus, we can rewrite the recurrence as
T(2m) = 2T2m/2+ 1.
Now, letting S(m) = T(2m), we can write
S(m) = 2Sm
2+ 1.
It is easy to verify that the solution to this recurrence is S(m) = Θ(lg m)(and your solution to
this problem should include this verification). Thus, T(2m) = Θ(lg 2m) = Θ(m), and T(n) =
Θ(lg n).
3. Work problem 4-1 (at the end of the chapter) in the Cormen et. al text.
Solution:
(a) T(n) = 2T(n/2) + n3.
Solution: This is a divide-and-conquer recurrence with a= 2,b= 2 and f(n) = n3. Let’s
solve it using the recursion tree method. Specifically, the following recursion tree captures
the total amount of computation performed by the recurrence:
1
pf3
pf4
pf5

Partial preview of the text

Download Analysis of Recurrence Relations using the Master Theorem and more Assignments Electrical and Electronics Engineering in PDF only on Docsity!

ECE 537 - Foundations of Computing Prof. Sen Homework # Solutions

  1. Work problem 4.1-1 in the Cormen et. al text. Solution: We must show that T (n) = c lg n

for an appropriate choice of c, and for all n when n is sufficiently large. Substituting this into the recurrence we get

T (n) ≤ c lg

(n

2

= c lg n + 1 − lg 2 = c lg n

which verifies that T (n) = O(lg n).

  1. Work problem 4.1-6 in the Cormen et. al text. Solution: Let m = lg n, then 2 m^ = n and

n = 2m/^2. Thus, we can rewrite the recurrence as

T (2m) = 2T

2 m/^2

Now, letting S(m) = T (2m), we can write

S(m) = 2S

(m

2

It is easy to verify that the solution to this recurrence is S(m) = Θ(lg m) (and your solution to this problem should include this verification). Thus, T (2m) = Θ(lg 2m) = Θ(m), and T (n) = Θ(lg n).

  1. Work problem 4-1 (at the end of the chapter) in the Cormen et. al text. Solution:

(a) T (n) = 2T (n/2) + n^3. Solution: This is a divide-and-conquer recurrence with a = 2, b = 2 and f (n) = n^3. Let’s solve it using the recursion tree method. Specifically, the following recursion tree captures the total amount of computation performed by the recurrence:

n^3

(n/2)^3 (n/2)^3

(n/4)^3 (n/4)^3 (n/4)^3 (n/4)^3

lg n

n^3

2(n/2)^3 = n^3 /(2^1 )^2

4(n/4)^3 = n^3 /(2^2 )^2

The root of this tree corresponds to the place where the first recursive steps are performed. That is, at the root, the problem size is n, and since the recurrence shows that for a problem of size n, an amount of computation corresponding to f (n) = n^3 needs to be performed, we write n^3 at the root. That is, ignoring the recursive calls (for the amount of computation contained in these will be shown in lower levels of the tree), n^3 work is performed at the root level. Now from the root level, there will be two (a) recursive calls, and that is why we show two branches from the root. In addition, since for each of these recursive call, the problem size is cut in half ( 1 /b), the nodes at this level have a problem size of n/b = n/ 2 , and an amount of computation corresponding to f (n/b) = (n/2)^3 must be performed for each recursive call. Thus, we write (n/2)^3 at each node on this level. Next, each of these nodes spawns two more recursive calls, for a total of four recursive calls on the next level, each having a computational load of f (n/b^2 ) = (n/4)^3. The problem size will continue to be reduced until the recurrence “bottoms out” when n = 1. That is, the recurrence will bottom out at the ith level when n/ 2 i^ = 1. That is, when i = lg n. Thus, the recursion tree has lg n + 1 levels, but the last one corresponds to the nodes where n = 1, so we don’t count them (i.e., we’re assuming T (1) = Θ(1)). Thus, we show the height of the recursion tree as lg n on the LHS of the figure. Notice that since this recurrence only involves one previous value of T (·) on the RHS of the original recurrence, every path in the tree bottoms out on the same level.

Now to calculate the total amount of computation performed by the recurrence, we need to sum the work performed at each node. This is easiest to do by first summing across the levels, and then summing the levels. Thus, on the RHS of the above figure we show the results we obtain by summing across the levels. In general, we can write that on the i-th level, an amount of computation equal to:

n^3 (2i)^2

n^3 4 i

number of subproblems increases by a factor of 16. Thus, the amount of work performed on level i is 16 i(n/ 4 i)^2 = n^2 , and the recurrence bottoms out when i = log 4 n. Thus,

T (n) =

log ∑ 4 n− 1

i=

n^2

= log 4 n · n^2 = Θ(n^2 log n).

(d) T (n) = 7T (n/3) + n^2. Solution: This is a divide-and-conquer recurrence with a = 7, b = 3, f (n) = n^2 , and nlogb^ a^ = nlog^3 7. Since 1 < log 3 7 < 2 , we have that n^2 = Ω(nlog^3 7+) for some constant  > 0. We also have a/bk^ = 7/ 32 = 7/ 9 < 1 , so case 3 of the Master Theorem applies, and T (n) = Θ(n^2 ). (e) T (n) = 7T (n/2) + n^2. Solution: This is a divide-and-conquer recurrence with a = 7, b = 2, f (n) = n^2 , and nlogb^ a^ = nlog^2 7. Since 2 < log 3 7 < 3 , we have that n^2 = O(nlog^3 7 −) for some constant  > 0. Thus, case 1 of the Master Theorem applies, and T (n) = Θ(nlg 7). (f) T (n) = 2T (n/4) +

n. Solution: This is a divide-and-conquer recurrence with a = 2, b = 4, f (n) =

n, and nlogb^ a^ = nlog^4 2 = nlog^4 1 / 2 = n^1 /^2 =

n. Since

n = Θ(nlog^4 2 ), case 2 of the Master Theorem applies, and T (n) = Θ(

n log n). (g) T (n) = T (n − 1) + n. Solution: This one is not a divide-and-conquer recurrence. We can solve it using a recursion tree. The tree is linear (one branch). At each level of the tree, the problem size is reduced by 1, and the amount of computation is the same as the problem size at each level. That is,

T (n) = n + (n − 1) + (n − 2) + · · · + 1

=

n(n + 1) 2 = Θ(n^2 ).

(h) T (n) = T (

n) + 1. Solution: Let’s solve this one using a change of variables. First let m = lg n and S(m) = T (2m). Then T (2m) = T (2m/^2 )+1, and S(m) = S(m/2)+1. It is easy to use the recursion tree method to see that S(m) = Θ(log m). Thus, T (n) = Θ(log log n).

  1. For each of the following recurrences, find the closed-form solution. Feel free to use the Master Method, but before applying it, you should check to make sure that the conditions of the Master Theorem are satisfied. Discussion: The solutions we derived using the Characteristic Equation Method for divide-and- conquer recurrences of the form T (n) = aT (n/b) + f (n), assumed that f (n) = Θ(nk) for some positive integer k. This applies to many of the divide-and-conquer recurrence you will come

across in the study of algorithms. The Master Method is more powerful in that it only requires f (n) to be an asymptotically positive function; however, there are some additional conditions that must be checked when using this method. Specifically, in the Master Method, which of the three cases you apply is determined by comparing f (n) to nlogb^ a. If nlogb^ a^ grows polynomially larger than f (n), then Case 1 applies; if they grow at asymptotically the same rate (within a logarithmic factor), then Case 2 can be applied; and if f (n) grows polynomially larger than nlogb^ a, then Case 3 applies, but only if an additional regularity condition is satisfied. Namely for this case we must also check to see that f (n) satisfies the regularity condition that af (n/b) ≤ cf (n) for some constant c < 1. Notice that for Cases 1 and 3, growing asymptotically larger is not sufficient. Rather, we must show that f (n) is either asymptotically larger or smaller than nlogb^ a^ by a factor of n. Thus, there can be cases where say nlogb^ a^ grows asymptotically larger than f (n), but not by a polynomial factor. This case would not be covered by the Master Method. In fact, it falls in between Cases 1 and 2. Similarly, there are recurrences that fall in between Cases 2 and 3. Thus, when applying the Master Method, be careful to check these conditions.

(a) T (n) = 4T (n 2 ) + n Solution: This recurrence fits the form that we derived using the Characteristic Equation Method, and since 4 > 21 , T (n) = Θ(nlog^2 4 ) = Θ(n^2 ). (b) T (n) = 4T (n 2 ) + n^2 Solution: This recurrence fits the form that we derived using the Characteristic Equation Method, and since 4 = 2^2 , T (n) = Θ(n^2 log n). (c) T (n) = 4T (n 2 ) + n^2 lg n Solution: We have a = 4, b = 2, f (n) = n^2 lg n and nlog^2 4 = n^2. Since f (n) = Θ(n^2 lgk^ n), with k = 1, Case 2 of the Master Method applies, and we can write T (n) = Θ(n^2 logk+1^ n) = Θ(n^2 log^2 n). (d) T (n) = 4T (n 2 ) + n^3 Solution: This recurrence fits the form that we derived using the Characteristic Equation Method, and since 4 < 23 , T (n) = Θ(n^3 ). (e) T (n) = 5T (n 2 ) + n^2 lg n Solution: We have a = 5, b = 2, f (n) = n^2 lg n and nlg 5^ = n^2.^3219. Since f (n) = O(n^2.^3219 −), for  = 0. 0219 , Case 1 of the Master Method applies, and we can write T (n) = Θ(nlg 5) = Θ(n^2.^3219 ). (f) T (n) = 3T (n 4 ) + n lg n Solution: We have a = 3, b = 4, f (n) = n lg n and nlog^4 3 = n^0.^7925. Since f (n) = Ω(n^0 .7925+) for  = 0. 2075 , Case 3 applies if the regularity condition holds. Since 3 n 4 lg

(n 4

3 4 n^ lg^ n^ −^

3 2 n^ ≤^ cn^ lg^ n^ for^ c^ =^

3 4 , the Master Method can be applied and we have^ T^ (n) = Θ(n log n). (g) T (n) = 2T (n 2 ) + (^) lgn n Solution: We have a = 2, b = 2, f (n) = n/ lg n and nlg 2^ = n. Since f (n) = O(n) it seems that Case 1 of the Master Method may apply; however, upon checking the exact form that must be satisfied, we find that f (n) 6 = O(n^1 −) for any  > 0. (Note: To see this, recall that we must show that n/ lg n ≤ cn^1 −^ ∀ n ≥ n 0 , where c and n 0 are positive constants.