Note: The exercises are optional, but highly recommended. If you are uncertain about any problem or want to discuss, write in the course discussion channel, or ask a teacher.
The complexity exercises are divided into two parts, one early in the course and a second part later on. These early A complexity exercises are only core exercises, and we recommend that you try all of them. The second part will come later in the course.
You can try these exercises in connection with the first complexity lecture.
A1. What is the asymptotic time complexity (in n) of the following operations, assuming that they are implemented efficiently?
The moral is that all data structures make tradeoffs – choosing a different representation will make some operations faster but others slower.
A2. Calculate the asymptotic complexities of the following code fragments. Although they are toy examples, the techniques you use are just the same as for realistic programs. Each procedure has been written so that its asymptotic complexity is the same as the order of growth of the mathematical function it computes (why is that?).
First write down the mathematical function T(n) that the procedure computes (i.e. how many times the statement sum+=1
is executed).
Fragment f2 is solved for you already.
f1:
sum = 0
for i = 1 to n:
sum += 1
f2:
sum = 0
for i = 1 to n:
for j = 1 to n-1:
sum += 1
Answer:
f3:
sum = 0
for i = 1 to n:
for j = 1 to i:
sum += 1
f4:
sum = 0
for i = 0, 2, 4, ..., n/2:
for j = 0, 3, 6, 9, ..., n/3:
for k = 0, 5, 10, 15, ..., n/5:
sum += 1
f5:
sum = 0
for i = 1 to n:
sum += 1
for j = 0 to n:
sum += 1
f6:
sum = 0
for i = 1 to n:
for j = 1 to i:
sum += 1
for k = 1 to n:
sum += 1
A3. Consider an array of n integers. What are the worst-case and average-case asymptotic complexities of sorting the array using:
Parts of this question are answered by the lectures on sorting. For other parts, you may find the answer in the course book.
A4. Suppose you have an array of n elements, then you can check if it has duplicate elements in O(n2) time by looping over every pair of elements. But if the array is sorted, you can do it in O(n) time. How?
A5 (exercise 1.4.12 from S&W). Write a program that, given two sorted arrays of n integer values, prints all elements that appear in both arrays, in sorted order. The running time of your program should be proportional to n in the worst case.
This program is very similar to a step in one of the algorithms that you have seen in the lectures, but with an small but important difference.
A6. This question is about adding an operation to dynamic arrays that deletes the last element of the array.
A7. Calculate the asymptotic complexities of the following code fragments, just as you did in A2 above.
f7:
sum = 0
i = 1
while i ≤ n:
for j = 1 to n:
sum += 1
i *= 2
f8:
sum = 0
i = 1
while i ≤ n:
for j = 1 to n*n:
sum += 1
i *= 2
f9: (This one is trickier than it seems)
sum = 0
i = 1
while i ≤ n:
for j = 1 to i:
sum += 1
i *= 2
Note: The only difference between f7 and f9 are that in f7 the inner loop goes to n, while in f9 the inner loop goes to i. This makes a difference!