This assignment consists of two questions. The first question is to be completed individually. The second question can be completed in groups of size up to three. Further instructions about group work will be provided separately.
This question is to be attempted individually.
Consider the following pseudocode:
int f(int n) {
if (n>1) {
f(n/2);
f(n/2);
for(int i = 1; i <= g(n); i++) {
println("Hello world!");
}
f(n/2);
}
}
int g(int n) {
int sum = 0;
for(int i = 1; i <= n; i++) sum += i;
return sum;
}
Here println() is a function that prints a line of text. Assume n is a power of 2.
(a) Express the return value of g(n) in terms of n. [5 marks]
(b) What is the time complexity of g(n), in terms of n and in big-O? [5 marks]
(c) Let T(n) be the number of lines printed by f(n). Write down a recurrence formula for T(n), including the base case. [5 marks]
(d) Solve the recurrence in (c), showing your working steps. For full credit give the exact answer (not big-O) and do not use Master Theorem. [20 marks]
This question can be attempted in groups.
There are n samples that need to be tested for coronavirus. The testing process (known as PCR) involves using a special machine, testing one sample at a time, which is both time consuming and expensive. Thus, we would like to have a method that is better than the trivial method of testing each sample one by one sequentially. Fortunately this is possible because we expect most samples to return a negative result (not containing the virus), and so we can ‘combine’ multiple samples to be tested together. For example, we can take one drop from each of the samples 1, 2, 3 to form a new sample, and test this mixed sample. If it returns a negative result, then all samples 1, 2, 3 are negative. But if it returns a positive result, we only know some of them contain the virus and not which one(s), or indeed how many of them, contain the virus, and further tests are needed.
Assume each individual sample contains sufficient contents so many drops can be taken from each of them, and that if a sample contains the virus, any drop taken from it will contain the virus.
(a) Suppose we know that exactly one of the n samples contains the virus. Design an algorithm for identifying it that takes O(log n) tests. [20 marks]
(b) Suppose instead we know that at most one of the n samples contains the virus. Design an algorithm for identifying the positive sample (or determine that there are none) using as few tests as you can. [20 marks]
(c) Suppose instead we know that at most two of the n samples contain the virus. Repeat
(b). [25 marks]
In each part, you should:
You can assume n is some “nice” number such as powers of 2; state your assumptions. (You can have different assumptions in each part if you want.)