Solutions for Hw 2

advertisement
SOLUTIONS FOR HOMEWORK 2
Preliminaries
Master Theorem. Use the definition from our textbook. Another more generic
form of Master theorem can be found at http://en.wikipedia.org/wiki/Master_
theorem.
n vs log n.
Claim 0.1. For any > 0, we have log n = o(n ) = O(n )
Proof. By L’Hopital’s rule,
(log n)0
log n
= lim
n→∞ (n )0
n→∞ n
1/(n ln 2)
= lim
n→∞
n−1
1
= lim
n→∞ n ln 2
= 0.
lim
Hence log n = o(n ) = O(n ).
Notations. We write log2 n as log n, log10 n as lg n, and loge n as ln n.
Question 1
2
Claim 0.2. T (n) = Θ(n log n).
Proof. b = 2, a = 4 and f (n) = n2 . Note f (n) = n2 = Θ(n2 ) = Θ(nlog2 4 ), therefore
T (n) = Θ(nlogb a log n) = Θ(n2 log n) by case 2 of Master theorem.
Question 2
Claim 0.3. T (n) = Θ(n2 ).
Proof. a = 16, b = 4 and f (n) = n. Take = 1 we have f (n) = n = O(n1 ) =
O(nlog4 16−1 ) = O(nlogb a− ), therefore T (n) = Θ(nlogb a ) = Θ(n2 ) by case 1 of
Master theorem.
Question 3
2
Claim 0.4. T (n) = Θ(n log n).
Proof. a = 6, b = 3 and f (n) = n2 log n. Let = 2 − log3 6, as log3 6 < log3 32 = 2,
it is clear that > 0. Hence f (n) = Ω(n2 ) = Ω(nlogb a+ ).
Furthermore, we have regularity condition: af (n/b) = 6f (n/3) = 6( n3 )2 log n3 ≤
6
2
2
9 · n log n ≤ 3 f (n), let c = 3 , we have 0 < c < 1.
Therefore T (n) = Θ(f (n)) = Θ(n2 log n) by case 3 of Master theorem.
1
2
SOLUTIONS FOR HOMEWORK 2
Question 4
Claim 0.5. T (n) = Θ(n log n).
Proof. a = 3, b = 4 and f (n) = n log n. Let = 1 − log4 3, as log4 3 < 1, it is clear
that > 0. Hence f (n) = n log n = Ω(n) = Ω(nlogb a+ ).
Furthermore, we have regularity condition: af (n/b) = 3f (n/4) = 3 n4 log n4 ≤
3
3
3
4 · n log n ≤ 4 f (n), let c = 4 , we have 0 < c < 1.
Therefore T (n) = Θ(f (n)) = Θ(n log n) by case 3 of Master theorem.
Question 5
2
Claim 0.6. T (n) = Θ(n ).
Proof. a = 4, b = 2 and f (n) = n log n. Take = 0.5 we have f (n) = n log n =
O(n1.5 ) = O(nlog2 4−0.5 ) = O(nlogb a− ), therefore T (n) = Θ(nlogb a ) = Θ(n2 ) by
case 1 of Master theorem.
Question 6
2
Claim 0.7. T (n) = Θ(n log n).
Proof. a = 2, b = 2 and f (n) = n log n. It can be easily verified that this question
can not be directly solved by applying the Master theorem in our textbook, however
the Generic Form of Master Theorem does give a solution. We use substitution
method here to directly solve this question.
Without loss of generality, let us assume n = 2k for some integer k.
n
T (n) = 2T ( ) + n log n
2 n
n
n
= 2 2T ( 2 ) + log
+ n log n
2
2
2
n
n
= 22 T ( 2 ) + n log + n log n
2
2
n
= ···
Further expand T ( 2 )
2
k
X
n
k
= 2 T (1) + n
log k−i
2
i=1
= nT (1) + n
k
X
log 2i
i=1
= nT (1) + n
k
X
i
i=1
= nT (1) + n
k(k + 1)
2
as k = log n, we have T (n) = Θ(nk 2 ) = Θ(n log2 n).
When n 6= 2k . It will not affect our result when n does not have the form of 2k .
Let k = dlog ne, we have T (2k−1 ) ≤ T (n) ≤ T (2k ), which still gives us T (n) =
Θ(n log2 n).
SOLUTIONS FOR HOMEWORK 2
3
Remark. A graphical version of our solution is to use Recursion Tree which is
covered in our textbook. You can also prove the result by using mathematical
induction, however coming up with a correct guess in our case is non-trivial.
Question 7
Claim 0.8. T (n) = Θ(nlog2 3 )
Proof. a = 3, b = 2 and f (n) = n. Since logb a ≈ 1.58 we have f (n) = O(nlogb a− )
for = 0.1. By case 1 of Master theorem, T (n) = Θ(nlogb a ) = Θ(nlog2 3 ).
Question 8
Claim 0.9. T (n) = 2n+1 − 1 = Θ(2n ).
Proof.
T (n) = T (n − 1) + 2n
= T (1) +
n
X
2i
i=2
=
n
X
2i
i=0
n+1
=2
− 1.
To see the last step, one can use the following technique:
n
n
X
X
2i )
2i = (2 − 1)(
i=0
i=0
=
n
X
(2i+1 − 2i )
i=0
= (21 − 20 ) + (22 − 21 ) + · · · + (2i − 2i−1 ) + (2i+1 − 2i )
= 2i+1 − 20 .
Download