Tutorial 1 Solution
Tutorial 1 Solution
Asymptotic Notations
1 Complexity of Functions
Compute the time complexity of the following functions, in terms of Θ(·) bound
1. void function(int n){
int i = 1, s = 1;
while (s < n) {
s = s + i;
i++;
}
}
Solution. The number of times while loop runs decides the worst case complexity T (n).
The While loop runs as long as s < n. If the loop runs k times, we have
S = 1 + 2 + 3 + 4 + 5 + 6. . . + k < n
k(k + 1)/2 < n
k(k + 1) < 2n
√ √
k < 2n = O( n)
k(k + 1)/2 ≥ n
k(k + 1) ≥ 2n
(k + 1)2 ≥ 2n
√
k + 1 ≥ 2n
√ √
k ≥ 2n − 1 = Ω( n)
1
2. void fun(int n)
{
int i = 1;
while (i < n) {
int j = n;
while (j > 0) {
j = j / 2;
}
i = i * 2;
}
}
Solution. Let T1 (n) be complexity of external look and T2 (n) be complexity of the internal loop.
Each can be defined using last term.
Thus, T2 (2k ) = T2′ (k) = Θ(k), or T2 (n) = Θ(log n). Similarly, convert functions where T1′ (k) = T1 (2k ).
Thus, T1 (2k ) = T1′ (k) = Θ(k log n), or T2 (n) = Θ(log2 n).
2
3. void fun(int n)
{
solution = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j = j + i)
solution = solution +1;
}
Solution. Let T1 (n) be complexity of external look and T2 (n, j) be complexity of the internal loop
which is also dependent of the loop variable of outer loop. Each can be defined using last term.
T1 (k) = T1 (k − 1) + T2 (n, k)
T2 (n, k) = T2 (n − k, k) + 1
T2 (n, k) = Θ(n/k)
T1 (k) = T1 (k − 1) + Θ(n/k)
Xn
T1 (k) = Θ(n/k)
k=1
n
X
= Θ(n) 1/k = Θ(n log n)
k=1
2 Explicit Notations
Recall the definition of O(·), Ω(·), o(·), ω(·), θ(·).
Find the value of c and n0 (wherever required) for proving all possible relationships wherever applicable.
1. f (n) = 13n3 + 12n + 11
g(n) = 2000n2 + 100000.
Solution. For Ω(·). Let n0 = 1000 and c = 1. For any n > n0 = 1000 we have
Thus, f (n) >= cg(n) for c = 1 and n > 1000. Hence f (n) = Ω(g(n)) and g(n) = O(f (n)).
For ω(·). We need to show
f (n) 13n3 + 12n + 11
limn→∞ =
g(n) 2000n2 + 100000
n3 (13 + 12/n2 + 11/n3 )
= 2
n (2000 + 100000/n2 )
13n
= (since 1/∞ = 0)
2000
=∞
3
2. f (n) = n2 − 1000n + 3
g(n) = 1200n + 15.
Solution. For Ω(·). Let n0 = 10000 and c = 1. For any n > n0 = 1000 we have
Thus, f (n) >= cg(n) for c = 1 and n > 10000. Hence f (n) = Ω(g(n)) and g(n) = O(f (n)).
For ω(·). We need to show
f (n) n2 + 1000n + 3
limn→∞ =
g(n) 1200n + 15
n (1 + 1000/n + 3/n2 )
2
=
n(1200 + 15/n)
n
= (since 1/∞ = 0)
1200
=∞
3. f (n) = 19000n2 + 5n + 3
g(n) = n2 − 109 .
4
4. f (n) = 2n − 500n3 + 10
g(n) = 3n + 10n2 + 200.
f (n) 2n − 500n3 + 10
limn→∞ = n
g(n) 3 + 10n2 + 200
3
2n (1 − 500 2nn + 10
2n )
= n2 200
3n (1 + 10 +
3n 3n )
2
= ( )n (for n → ∞)
3
=0
5
5. f (n) = n log n + n2
log log n
g(n) = n log n + 100n.
5 log log n
Solution. Let k = n log n and k ′ = n log n .
5
k = n log n
5
log k = × log n = 5
log n
k = 25 = 32
log log n
k ′ = n log n
log log n
log k = × log n = log log n
log n
k ′ = 2log log n = log n
5
f (n) = n2 + 32
g(n) = 100n + log n.
For Ω(·). Let n0 = 101 and c = 1.
f (n) n2 + 32
limn→∞ =
g(n) 100n + log n
n2 (1 + 32/n2 )
=
n(100 + logn n )
n
= (for n → ∞)
100
=∞