Hwk1 Solution

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

CECS 419/619 COMPUTER ALGORITHMS Fall 2016

Homework Set 1
Student Name: Khaled Gamal Abdel Maksoud
Student ID: 1280703

Problem 1 (20 points)


Asymptotic notation properties
Let f(n) and g(n) be asymptotically positive functions. Prove or disprove each of the following conjectures.

a. f(n) = O(g(n)) implies g(n)=O(f(n)).


Solution
Let f(n) = n and g(n) = n2
It is clear that f(n) = O(g(n)) since 0 ≤ n ≤ C n2 where C is a constant ≥ 1 and for all n ≥ 1
On the other hand, g(n) ≠ O(f(n)) since we cannot find constants C > 0 and n0 > 0 such that 0 ≤ n2 ≤ C n for all
n ≥ n0
Now, let f(n) = n and g(n) = 2n
It is clear that f(n) = O(g(n)) since 0 ≤ n ≤ C (2n) where C is a constant ≥ 1 and n ≥ 1
On the other hand, g(n) = O(f(n)) since 0 ≤ 2n ≤ C n where C is a constant ≥ 2 and n ≥ 1
Therefore, the conjecture “f(n) = O(g(n)) implies g(n)=O(f(n))” is not always true.

b. f(n) = O(g(n)) implies lg(f(n)) = O(lg(g(n))), where lg(g(n)) ≥ 1 and f(n)≥ 1 for all sufficiently large n.
Solution
Since f(n) = O(g(n)) then, there exist constants C > 0 and n0 > 0 such that 0 ≤ f(n) ≤ C g(n) for all n ≥ n0
 lg(f(n)) ≤ lg(C g(n)) [ since lg is a monotone function ]
 lg(f(n)) ≤ lg(C) + lg(g(n)) [ since lg(ab) = lg(a) + lg(b) ]
 lg(f(n)) ≤ d + lg(g(n)) where d = lg(C)
If d ≥ 0 then, lg(f(n)) ≤ d lg(g(n)) + lg(g(n)) [ since lg(g(n)) ≥ 1 ]
 lg(f(n)) ≤ (d+1) lg(g(n))
 lg(f(n)) ≤ k lg(g(n)) where k = d+1
 lg(f(n)) = O(lg(g(n)))
If d < 0 then, lg(f(n)) ≤ lg(g(n))
Page 1 of 8
 lg(f(n)) ≤ k lg(g(n)) where k = 1
 lg(f(n)) = O(lg(g(n)))
Therefore, the conjecture “f(n) = O(g(n)) implies lg(f(n)) = O(lg(g(n))), where lg(g(n)) ≥ 1 and f(n)≥ 1 for all
sufficiently large n” is always true.

c. f(n) = O(g(n)) implies 2f(n)=O(2g(n)).


Solution
Let f(n) = 2n and g(n) = n
It is clear that f(n) = O(g(n)) since 0 ≤ 2n ≤ C n where C is a constant ≥ 2 and for all n ≥ 1
On the other hand, 2f(n) = 22n = 4n and 2g(n) = 2n
It is clear that 2f(n) ≠ O(2g(n)) since we cannot find constants C > 0 and n0 > 0 such that 0 ≤ 4n ≤ C 2n for all
n ≥ n0 ( since 4n grows faster than 2n )
Now, let f(n) = n and g(n) = 2n
It is clear that f(n) = O(g(n)) since 0 ≤ n ≤ C (2n) where C is a constant ≥ 1 and n ≥ 1
On the other hand, 2f(n) = 2n and 2g(n) = 22n = 4n
It is clear that 2f(n) = O(2g(n)) since 0 ≤ 2n ≤ C 4n where C is a constant ≥ 1 and n ≥ 1
Therefore, the conjecture “f(n) = O(g(n)) implies 2f(n)=O(2g(n))” is not always true.

d. f(n) = O((f(n))2).
Solution
Let f(n) = n
It is clear that f(n) = O((f(n))2) since 0 ≤ n ≤ C n2 where C is a constant ≥ 1 and for all n ≥ 1
Now, let f(n) = 1/n
It is clear that f(n) ≠ O((f(n))2) since we cannot find constants C > 0 and n0 > 0 such that 0 ≤ 1/n ≤ C (1/n)2 for
all n ≥ n0 ( since 1/n grows faster than (1/n)2 )
Therefore, the conjecture “f(n) = O((f(n))2)” is not always true.

e. f(n) = O(g(n)) implies g(n)= Ω(f(n)).


Solution
Page 2 of 8
Since f(n) = O(g(n)) then, there exist constants C > 0 and n0 > 0 such that 0 ≤ f(n) ≤ C g(n) for all n ≥ n0
 0 ≤ 1/C f(n) ≤ g(n)
 0 ≤ k f(n) ≤ g(n) for all n ≥ n0 [ k = 1/C > 0]
 g(n)= Ω(f(n))
Therefore, the conjecture “f(n) = O(g(n)) implies g(n)= Ω(f(n))” is always true.

f. f(n) + o(f(n))= Θ(f(n)).


Solution
Let h(n) ∈ o(f(n))
 There exist constants C > 0 and n0 > 0 such that 0 ≤ h(n) < C f(n) for all n ≥ n0
 f(n) ≤ f(n) + h(n) [since h(n) ≥ 0]
 f(n) ≤ f(n) + h(n) ≤ f(n) + C f(n)
 f(n) ≤ f(n) + h(n) ≤ (1 + C) f(n)
 d f(n) ≤ f(n) + h(n) ≤ k f(n) [ d and k are constants > 0 and d=1 and k = 1 + C ]
 f(n) + o(f(n))= Θ(f(n))
Therefore, the conjecture “f(n) + o(f(n))= Θ(f(n))” is always true.

Page 3 of 8
Problem 2 (20 points)
a) Use a recursion tree to determine a good asymptotic upper bound on the recurrence
𝑇(𝑛) = 𝑇(𝑛⁄2) + 𝑛2
Solution

T(n) n2 n2 n2 n2 n2

T(n/2) (n/2)2 (n/2)2 (n/2)2 1/4 n2

T(n/4) (n/4)2 (n/4)2 (1/4)2 n2

Depth = lg n
T(n/8) (n/8)2 (1/4)3 n2

T(1) C (constant > 0)

The recursion tree corresponding to the given recurrence is drawn above.


On the ith level of the recursion tree, the problem size is n/2i .
When we reach the leaves level, we have n/2i = 1
Accordingly, i = lg n
T(n) = n2 + 1/4n2 + (1/4)2 n2 + (1/4)3 n2 + … + C [where C is a constant > 0 ]
lg n −1
T(n) = [∑i=0 (1/4)i n2] + C


≤ [∑i=0 (1/4)i n2 ] + C

= [n2 /(1 − 1/4)] + C [since ∑∞ k


k=0 x = 1/1-x]

= 4/3 n2 + C
≤ 4/3 n2 + C n2
= (4/3 + C) n2
= d n2 (where d = 4/3 + C)
We have T(n) ≤ d n2 where d is a constant > 0 and n ≥ 1.
Therefore, T(n) = O(n2 ).

Page 4 of 8
b) Use the substitution method to verify your answer

Solution
Assume T(k) ≤ C k2 for k < n and C is a constant > 0.
T(n) = T(n/2) + n2
≤ C (n/2)2 + n2
= C n2/4 + n2
3
= C n2 - (4 C n2 − n2 ) (desired - residual)
3
≤ C n2 whenever (4 C n2 − n2 ) ≥ 0

≤ C n2 whenever C ≥ 4/3 and n ≥ 1


Therefore, T(n) = O(n2 ).

Page 5 of 8
Problem 3 (20 points)
Use the master method to give tight asymptotic bounds for the following recurrences.
a) 𝑇(𝑛) = 2𝑇(𝑛⁄2) + 𝑛4
Solution
Comparing the given recurrence with the general form T(n) = a T(n/b) + f(n), we get:
a = 2, b = 2 and f(n) = 𝑛4 (f(n) > 0 for all n ≥ 1)
𝑛log𝑏 𝑎 = 𝑛log𝑏 𝑎 = 𝑛
𝑛4 = Ω(𝑛1+ℇ ) for some ℇ > 0
𝑛4 = Ω(𝑛2 ) (we selected ℇ = 1)
Now, we need to prove that a f(n/b) ≤ C f(n) for some positive constants C and n0 where C < 1 and n ≥ n0
 2(𝑛/2)4 ≤ C 𝑛4
 C = 1/8 and n ≥ 1
It is clear now that we can apply Case 3 of the master method.
Therefore, T(n) = Θ(𝑛4 )

b) 𝑇(𝑛) = 𝑇(7𝑛⁄10) + 𝑛
Solution
Comparing the given recurrence with the general form T(n) = a T(n/b) + f(n), we get:
a = 1, b = 10/7 and f(n) = 𝑛 (f(n) > 0 for all n ≥ 1)
𝑛log𝑏 𝑎 = 𝑛log10/7 1 = 𝑛0
𝑛 = Ω(𝑛0+ℇ ) for some ℇ > 0
𝑛 = Ω(𝑛) (we selected ℇ = 1)
Now, we need to prove that a f(n/b) ≤ C f(n) for some positive constants C and n0 where C < 1 and n ≥ n0
 7n/10 ≤ C 𝑛
 C = 7/10 and n ≥ 1
It is clear now that we can apply Case 3 of the master method.
Therefore, T(n) = Θ(𝑛)

c) 𝑇(𝑛) = 16𝑇(𝑛⁄4) + 𝑛2
Solution
Comparing the given recurrence with the general form T(n) = a T(n/b) + f(n), we get:
a = 16, b = 4 and f(n) = 𝑛2 (f(n) > 0 for all n ≥ 1)

Page 6 of 8
𝑛log𝑏 𝑎 = 𝑛log4 16 = 𝑛2
𝑛2 = Θ (𝑛2 ) (since 1/2 𝑛2 ≤ 𝑛2 ≤ 2 𝑛2 for all n ≥ 1)
It is clear now that we can apply Case 2 of the master method.
Therefore, T(n) = Θ(𝑛2 lg 𝑛)

d) 𝑇(𝑛) = 7𝑇(𝑛⁄3) + 𝑛2
Solution
Comparing the given recurrence with the general form T(n) = a T(n/b) + f(n), we get:
a = 7, b = 3 and f(n) = 𝑛2 (f(n) > 0 for all n ≥ 1)
𝑛log𝑏 𝑎 = 𝑛log3 7 = 𝑛𝑑 where 1 < d < 2
𝑛2 = Ω(𝑛𝑑+ℇ ) for some ℇ > 0
𝑛2 = Ω(𝑛2 ) (we selected ℇ = 2-d > 0)
Now, we need to prove that a f(n/b) ≤ C f(n) for some positive constants C and n0 where C < 1 and n ≥ n0
 7(𝑛/3)2 ≤ C 𝑛2
 C = 7/9 and n ≥ 1
It is clear now that we can apply Case 3 of the master method.
Therefore, T(n) = Θ(𝑛2 )

e) 𝑇(𝑛) = 7𝑇(𝑛⁄2) + 𝑛2
Solution
Comparing the given recurrence with the general form T(n) = a T(n/b) + f(n), we get:
a = 7, b = 2 and f(n) = 𝑛2 (f(n) > 0 for all n ≥ 1)
𝑛log𝑏 𝑎 = 𝑛log2 7 = 𝑛𝑑 where 2 < d < 3
𝑛2 = O(𝑛𝑑−ℇ ) for some ℇ > 0
𝑛2 = O(𝑛2 ) (we selected ℇ = d-2 > 0)
It is clear now that we can apply Case 1 of the master method.
Therefore, T(n) = Θ(𝑛log2 7)

f) 𝑇(𝑛) = 2𝑇(𝑛⁄4) + √𝑛
Solution
Comparing the given recurrence with the general form T(n) = a T(n/b) + f(n), we get:
Page 7 of 8
a = 2, b = 4 and f(n) = √𝑛 (f(n) > 0 for all n ≥ 1)
𝑛log𝑏 𝑎 = 𝑛log4 2 = √𝑛
√𝑛 = Θ (√𝑛) (since 1/2√𝑛 ≤ √𝑛 ≤ 2 √𝑛 for all n ≥ 1)
It is clear now that we can apply Case 2 of the master method.
Therefore, T(n) = Θ(√𝑛 lg 𝑛)

g) 𝑇(𝑛) = 𝑇(𝑛 − 2) + 𝑛2
Solution
We cannot apply the master method since the given recurrence does not comply with the general form:
T(n) = a T(n/b) + f(n) (the problem size does not decrease by a factor of 1/b for some b > 1).

Page 8 of 8

You might also like