You are on page 1of 3

1) Briefly explain the meaning of the terms computational complexity and

asymptotic complexity.

4 marks:
2 marks: measure of cost of applying an algorithm
2 marks: approximation of computational complexity for very large data

2) Briefly describe the meaning of big-O, Θ, and OO notations (you do not need to
write the mathematical definition).

6 marks:
2 marks: big-O
2 marks: Θ
2 marks: OO
(2 marks for good explanation, 1 mark for some knowledge)

3) For one of the notations listed in Question (2), write the formal mathematical
definition.

2 marks: subtract half for small error, subtract 1 for semantic error

4) For each of the following pieces of code, write a function f(n) that indicates the
number of (assignment) operations the code will use based on the value of n, and
state the big-O complexity of the code.

(3 marks each – 2 marks for complexity function, 1 mark for big-O)

i. for (int i = 0, x = 0; i < n; i++)


x += i;

f(n) = 2 + 2n = O(n)

ii. for (int i = 1; i < n; i *= 3)


x += y[i];

f(n) = 1 + 2log3 n = O(log n)


iii. for (int i = 1, x = 0; i <= 10; i++)
x += i;

f(n) = 2 + 10*2 = 22 = O(1)

iv. for (int i = 1, x = 0; i <= n; i++)

1
for (int j = 1; j <= n; j++)
x += y[i][j];

f(n) = 2 + 2n + 2n2 = O(n2)

v. for (int i = 1; i <= n; i++)


for (int j = 1; j <= i; j++)
x += z[i][j];

f(n) = 1 + 2n + 2n(n + 1)/2 = n2 + 3n + 1 = O(n2)

5) Three different algorithms for solving the same problem have the following
complexity functions:

Algorithm 1: f1 (n) = n + 3nlog2n - 13


Algorithm 2: f2 (n) = 9n3 + 20n
Algorithm 3: f3 (n) = 1010n

Answer the following questions about the 3 algorithms:

i. What are the big-O complexities of the 3 algorithms? (State the lowest upper
bound.)

1 mark each:
Alg. 1 is O(n log n)
Alg. 2 is O(n3)
Alg. 3 is O(n)

ii. True or false: Algorithm 2 is O(n4)

1 mark: True

iii. True or false: Algorithm 2 is o(n4)

1 mark: True

iv. True or false: Algorithm 2 is θ(n4)

1 mark: False

v. True or false: Algorithm 2 is Ω(n4)

1 mark: False
vi. Given a typical value for n of 100, which of the 3 algorithms would you
choose as the most efficient?
1 mark: Algorithm 1

2
3

You might also like