0% found this document useful (0 votes)
18 views

Tutorial 1 Solution

This document discusses asymptotic notations and complexity analysis of algorithms. It provides examples of functions and calculates their time complexities in terms of Big O, Omega and Theta notations. It also discusses the definitions of various asymptotic notations and solves problems to determine relationships between functions.

Uploaded by

Sara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Tutorial 1 Solution

This document discusses asymptotic notations and complexity analysis of algorithms. It provides examples of functions and calculates their time complexities in terms of Big O, Omega and Theta notations. It also discusses the definitions of various asymptotic notations and solves problems to determine relationships between functions.

Uploaded by

Sara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

[CSN212] Study Group 1

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)

Also, since while loop terminates when s >= n

k(k + 1)/2 ≥ n
k(k + 1) ≥ 2n
(k + 1)2 ≥ 2n

k + 1 ≥ 2n
√ √
k ≥ 2n − 1 = Ω( n)

Hence, time complexity T (n) = k = Θ(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.

T1 (n) = T1 (n/2) + T2 (n)


T2 (n) = T2 (n/2) + 1

Convert variable k = ⌊log n⌋, giving 2k n. Thus, we get

T1 (2k ) = T1 (2k−1 ) + T2 (n)


T2 (2k ) = T2 (2k−1 ) + 1

Convert function where T2′ (k) = T2 (2k ).

T2′ (k) = T2′ (k − 1) + 1


k
X
= 1 = Θ(k)
i=1

Thus, T2 (2k ) = T2′ (k) = Θ(k), or T2 (n) = Θ(log n). Similarly, convert functions where T1′ (k) = T1 (2k ).

T1′ (k) = T1′ (k − 1) + Θ(log n)


k
X
= Θ(log n) = Θ(k log n)
i=1

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

We thus compute T2 (n, k) and T1 (n) as follows

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

f (n) − cg(n) = (13n3 + 12n + 11) − (2000n2 + 100000)


= (10n3 + 12n + 11) + (2n3 − 2000n2 ) + (n3 − 100000)
≥ (10n3 + 12n + 11) + (2000n2 − 2000n2 ) + (109 − 105 )
(Substituting n = 1000 decreases value, since n > n0 = 1000)
≥0 (Since each term is clearly positive)

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
=∞

Thus, f (n) = ω(g(n)) and g(n) = o(f (n)).

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

f (n) − cg(n) = (n2 − 1000n + 3) − (1200n + 15)


≥ (10000n − 1000n − 1200n − 12)
(Substituting n = 1000 decreases value, since n > n0 = 10000)
≥ (7800n − 12) ≥ 0 (Clearly positive as n > n0 = 10000)

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
=∞

Thus, f (n) = ω(g(n)) and g(n) = o(f (n)).

3. f (n) = 19000n2 + 5n + 3
g(n) = n2 − 109 .

Solution. For Ω(·). Let n0 = 0 and c = 1.

f (n) − cg(n) = 19000n2 + 5n + 3 − n2 + 109


= 18999n2 + 5n + 3 + 109 >= 0 (for n ≥ 0)

Hence f (n) = Ω(g(n)) and g(n) = O(f (n)).


For O(·). Let n0 = 106 and c = 20000.

f (n) − cg(n) = 19000n2 + 5n + 3 − 20000n2 + 20000 × 109


= n(5 − n) + 3 + 2 × 1013 − 999n2
≤ n(5 − 106 ) + 2. · · · × 1013 − 9.99 × 1014
(Substituting n = 106 on negative terms increases value, since n > n0 = 106 )
≤0 (for n ≥ 106 )

Hence f (n) = O(g(n)), and thus f (n) = Θ(g(n)).

4
4. f (n) = 2n − 500n3 + 10
g(n) = 3n + 10n2 + 200.

Solution. For O(·). Let n0 = 1 and c = 1.

f (n) − cg(n) = 2n − 500n3 + 10 − 3n − 10n2 − 200


3
= 2n (1 − ( )n ) − 500n3 − 10n2 − 190
2
3
≤ 2n (1 − ( )) − 500n3 − 10n2 − 190 (for n ≥ 1)
2
≤0

Hence f (n) = O(g(n)), and g(n) = Ω(f (n)).

For o(·). We need to show

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

Thus, f (n) = o(g(n)) and g(n) = ω(f (n)).

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

Thus, f (n) = n2 + 32 and g(n) = 100n + log n.

5
f (n) = n2 + 32
g(n) = 100n + log n.
For Ω(·). Let n0 = 101 and c = 1.

f (n) − cg(n) = n2 + 32 − 100n − log n


log n
= n(n − 100 − ) + 32
n
log n
≥ n(1 − ) (for n ≥ 101)
n
≥0

Hence f (n) = Ω(g(n)) and g(n) = O(f (n)).


For O(·). Let n0 = 106 and c = 20000.

f (n) − cg(n) = 19000n2 + 5n + 3 − 20000n2 + 20000 × 109


= n(5 − n) + 3 + 2 × 1013 − 999n2
≤ n(5 − 106 ) + 2. · · · × 1013 − 9.99 × 1014
(Substituting n = 106 on negative terms increases value, since n > n0 = 106 )
≤0 (for n ≥ 106 )

Hence f (n) = O(g(n)), and thus f (n) = Θ(g(n)).


For ω(·). We need to show

f (n) n2 + 32
limn→∞ =
g(n) 100n + log n
n2 (1 + 32/n2 )
=
n(100 + logn n )
n
= (for n → ∞)
100
=∞

Thus, f (n) = ω(g(n)) and g(n) = o(f (n)).

You might also like