Tutorial1 (1)
Tutorial1 (1)
2
Some Basic Properties
3
Question 1
For each of the following statements, answer whether the
statement is true or false.
b) 𝑛( − 𝑛 = Θ(𝑛) False
f) 12𝑛 + 2* + 𝑛) = 𝑂 𝑛) False
4
Question 2
(a) 𝑇+ 𝑛 + 𝑇( 𝑛 = 𝑂 𝑓 𝑛
-! *
(b) -" *
=𝑂 1
(c) 𝑇+ 𝑛 = 𝑂 𝑇( 𝑛
Question 2
Suppose 𝑇+ 𝑛 = 𝑂 𝑓 𝑛 and 𝑇( 𝑛 = 𝑂 𝑓 𝑛 .
Is the following true?
(a) 𝑇+ 𝑛 + 𝑇( 𝑛 = 𝑂 𝑓 𝑛 ? True.
Example
𝑇+ 𝑛 = 2𝑛. + 3𝑛) and 𝑇( 𝑛 = 5𝑛. + 2𝑛(
⇒ 𝑇+ 𝑛 + 𝑇( 𝑛 = O 𝑛.
Question 2
Suppose 𝑇+ 𝑛 = 𝑂 𝑓 𝑛 and 𝑇( 𝑛 = 𝑂 𝑓 𝑛 .
Is the following true?
-! *
(b) -" *
=𝑂 1 ? False.
(c) 𝑇+ 𝑛 = 𝑂 𝑇( 𝑛 ? False.
(b) Define 𝑔 𝑛 = 𝑔* 𝑛 .
Is 𝑔 𝑛 = 𝑂 𝑓 𝑛 ?
Is 𝑔 𝑛 = 𝑂 𝑛𝑓 𝑛 ?
8
Question 3: (a)
(a) For fixed 𝑘, is 𝑔0 𝑛 = 𝑂 𝑓 𝑛 ? Yes.
Then 𝑔( 𝑛 = 𝑇+ 𝑛 + 𝑇( 𝑛 = 𝑂 𝑓 𝑛
𝑔0 𝑛 = 𝑔02+ 𝑛 + 𝑇0 𝑛 = 𝑂(𝑓 𝑛 )
𝑈(𝑛) 𝑉(𝑛)
9
Question 3:
(b) 𝑔0 (𝑛) = ∑0/1+ 𝑇/ 𝑛 where, for each 𝑖, 𝑇/ 𝑛 = 𝑂 𝑓 𝑛 .
*
g 𝑛 = 𝑔* 𝑛 = E 𝑇/ 𝑛
/1+
We display a counterexample.
10
A Counterexample
Set 𝑇3 𝑛 = 𝑖 ⋅ 𝑛, and 𝑓 𝑛 = 𝑛.
0(04+)
=> 𝑔0 𝑛 = 𝑐0 𝑛 where 𝑐0 = ( Which we also know is
true from part (a)
=> So, for fixed 𝑘, 𝑔0 𝑛 = 𝑂(𝑛)
*(*4+)
But g 𝑛 = 𝑔* 𝑛 = 𝑛 ( = Θ(𝑛))
In particular,
𝑔(𝑛) is NOT 𝑂(𝑓 𝑛 ) or even 𝑂 𝑛 𝑓 𝑛 !
𝑂(𝑛) 𝑂(𝑛') 11
Exercise On the Running Time of Triple Nested Loops
for i = 1 to n
for j = i to n
for k = i to j
do one unit of work
Prove that the above code performs 𝛩(𝑛! ) units of work. Use the fact
that∑%"#$ 𝑖𝑐 = Θ 𝑛&'$ , (polynomial series, reviewed in Lecture 0).
ååå1 = åå ( j - i + 1) =å å k
i =1 j =i k =i i =1 j =i i =1 k =1
åå
i =1 k =1
k = Q( n 3 )
12
Exercise On the Running Time of Triple Nested Loops (cont)
Recall (from exercise in first lecture) that ∑%(#$ 𝑘𝑐 = Θ 𝑛&'$ (for c=1
this is the arithmetic series, for c>1 this is the polynomial series). Thus,
by the definition of Θ, there exist constants c1, c2, c'1, c'2, such that:
n n
c ×n £ åk £ c ×n
1
2
2
2
c' ×n £ åk £ c' ×n
1
3 2
2
3
k =1 k =1
Then:
n n -i +1 n n
åå
i =1 k =1
k ³ å c1 × (n - i + 1) 2 ³ c1 å j 2 ³ c1c '1 n3 = W(n3 )
i =1 j =1
n n -i +1
Therefore: å å k = Q( n )
i =1 k =1
3
13
Recursion Exercise
𝑛
𝑇 𝑛 =𝑛+𝑇
2
𝑛 𝑛
=𝑛+ +𝑇 %
2 2
𝑛 𝑛 𝑛
=𝑛+ + %+𝑇 '
2 2 2
When does it stop?
𝑛
𝑇 1 = 1, 𝑇 ? =1
2
ℎ = 𝑙𝑜𝑔% 𝑛
14
Recursion Exercise
𝑛 𝑛 𝑛 𝑛 𝑛
=𝑛+ + ) + … + *+) + *+$ + 𝑇( * )
2 2 2 2 2
1 1 1 1 𝑛
= 𝑛(1 + + ) + … + *+) + *+$ ) + 𝑇( * )
2 2 2 2 2
+2>#$!
Sum of geometric series 𝑎 + 𝑎𝑟 + 𝑎𝑟 ' … =𝑎
+2>
1*
1− 1 − 2+*
=𝑛 2 +𝑇 1 =𝑛 +𝑇 1 =
1 1
1− 1−
2 2
1 − 2)*+,!! 1
=𝑛 +𝑇 1 =𝑛 2 1− +𝑇 1
1 𝑛
2
= 2𝑛 − 2 + 1 = 2𝑛 − 1
𝑇 𝑛 = 𝑂(𝑛)
15