You are on page 1of 2

PUSAT PENGAJIAN SAINS KOMPUTER

(School of Computer Sciences)

CPT212- Design and Analysis of Algorithms

WEEK 2 TUTORIAL : Analysis of Algorithms

Semester II 2019/2020

1) Implement the 𝑎𝑟𝑟𝑎𝑦𝑀𝑎𝑥 algorithm (in any programming language). Include a counter
into the program to calculate the number of primitive operations. You may use
https://onlinegdb.com as your compiler.

a. Show that the best-case time complexity (𝑓(𝑛) = 5𝑛) occurs when the maximum
element is 𝐴[0].
b. Show that the worst-case time complexity (𝑓(𝑛) = 7𝑛 − 2) occurs when the 𝐴 is sorted
in increasing order.

2) The following recursiveMax algorithm is a recursive solution to the array maximum


problem in Question 1:

Assuming we count each comparison, array reference, recursive call, 𝑚𝑎𝑥 calculation,
and return as a single primitive operation, show that 𝑟𝑒𝑐𝑢𝑟𝑠𝑖𝑣𝑒𝑀𝑎𝑥 has a computational
complexity of 𝑓(𝑛) = 7𝑛 − 4.

3) Implement recursiveMax algorithm (in any programming language). Include a counter


into the program to calculate the number of primitive operations. Show that the number of
primitive operations is indeed 𝑓(𝑛) = 7𝑛 − 4. You may use https://onlinegdb.com as your
compiler.

Clue: Use a global variable to count.

2𝑛
4) Given two functions, 𝑓1 (𝑛) = 𝑛2 and 𝑓2 (𝑛) = , determine the value of 𝑛 where the
4
output of 𝑓1 (𝑛) > 𝑓2 (𝑛). Then determine the value of 𝑛 where 𝑓2 (𝑛) > 𝑓1 (𝑛). What can
you conclude from both functions in terms of growth rate? What does it mean in terms of
which algorithm is better?
5) What order is an algorithm that has the following growth rate functions? What are the
common names of the growth rates?

a. 𝑓(𝑛) = 8𝑛3 − 9𝑛
b. 𝑓(𝑛) = 7 log 2𝑛 + 20
c. 𝑓(𝑛) = 7 log 2𝑛 + 𝑛
d. 𝑓(𝑛) = (2 + 𝑛)(3 + log 𝑛)
𝑛
e. 𝑓(𝑛) = 11 log 𝑛 + − 3452
2
f. 𝑓(𝑛) = 𝑛(3 + 𝑛) − 7𝑛

6) The following rules can be used to simplify the task of obtaining a big-O characterization
for various algorithms:

Use the big-O notation to estimate the time complexity for the following.

a. 𝑎(𝑛) = 𝑏(𝑛)𝑐(𝑛), where 𝑏(𝑛) = 𝑛2 + 3 and 𝑐(𝑛) = 𝑛 + 1


b. 𝑎(𝑛) = 𝑏(𝑛) + 𝑐(𝑛), where 𝑏(𝑛) = 𝑛6 + 𝑛5 + 𝑛3 + 1000 and 𝑐(𝑛) = 5000𝑛2 − 3
c. 𝑎(𝑛) = 𝑏(𝑛)𝑐(𝑛), where 𝑏(𝑛) = 2𝑛 and 𝑐(𝑛) = 3 log 𝑛5

Prepared by Teh Je Sen (2020)

-2-

You might also like