CS 3870: Exercise Solutions: Week 1, Part 2
CS 3870: Exercise Solutions: Week 1, Part 2
October 7, 2020
Week 1, part 2
4. Let’s say you have n wine bottles, one of which is filled with poison, but the rest are
filled with fine wine (or, if you prefer, you may think of this as having n blood samples
and a very precise test for coronavirus). You have a test that can detect the presence
of poison, but the test takes a lot of time to perform. How many tests do you need to
perform to find the poisoned bottle? Note: Your test is precise enough that it can detect
even small quantities of poison in a mix, i.e., you’re allowed to mix the contents of several
bottles in a single test.
Describe your strategy. An asymptotic bound on the number of tests as a function of
n is enough (i.e., in O(. . .) notation).
ANSWER: This is effectively binary search. Split the bottles into two halves,
mix a sample from every bottle in the first half into a test container, and test
for poison. If there is poison, put the second set of bottles aside and split the
first half. If there is no poison, put the first set of bottles aside and split the
second half. Repeat until only one bottle remains.
The number of tests required is O(log n).
1
for j from midpoint+1 to len(array)-1:
right_sum = right_sum + array[j]
if right_sum > best_right_sum:
best_j = j
best_right_sum = right_sum
return best_left_sum + array[midpoint] = best_right_sum
def MaxSubarray(array):
return FindMaxSubarray(array, 0, len(array)-1)
There is an issue with the implementation that significantly affects the running time.
(a) What is this implementation issue and why does it make a difference?
(b) Compute the asymptotic running time of the above implementation if len(array)=n,
in O(...) or theta notation. HINT: First figure out the exact size of the recursion
tree, then use this insight and the issue from 5(a) to compute the resulting running
time.
ANSWER: Indeed, this question was tricky, as the problem is precisely the
kind of issue that easily slips between the cracks. The issue is the following:
That is, the call to FindBestCrossing does not extract a slice of the array,
nor does it pass through the arguments low and high.
This means that the running time is not appropriately modelled by a recur-
rence (since the amount of local work in a call is not determined by the current
value of n = high − low + 1).
For an analysis, consider the following.
2
2. Approximately half of these are not leaf nodes, i.e., the tree has Θ(len(array))
non-leaf nodes
3. In every non-leaf node, the call to FindBestCrossing takes Θ(len(array))
time.
Week 2
1, from CLRS Problem 4-1. Give the asymptotic growth of the following recurrence
relations, for example using the Master theorem.
ANSWER:
(b) Write a recurrence relation modelling the running time of a call cantor(1,n)
(c) Use this to estimate the asymptotic running time of such a call
Remark: This is not important for your solution, but in this algorithm a/b represents
integer division, e.g., 5/3 gives 1.
3
cantor(0,15)
10 + 50 = 60
cantor(0,5) cantor(10,15)
1 + 9 = 10 21 + 29 = 50
else
return cantor(low,low+gap) + cantor(high-gap,high);
}
3 (creativity question). At the very end of the slides for Lecture 2 it is announced
that there exists a “clever” algorithm for solving Maximum Subarray in time O(n)
(instead of O(n log n) as the divide-and-conquer scheme from the lecture results in). Can
you find this more clever algorithm? (It does not use any advanced paradigm. Just a
good observation.)
ANSWER: As the hint says on the lecture slide, try to maintain (in an it-
erative loop) a variable that stores the maximum value of a subarray that
ends at the current position (say i). There is a simple rule for this. The best
subarray in A that ends at position i is . . .
4
• The value of the best array ending at i − 1 plus A[i], OR
• just A[i], whichever is bigger.
That is, if we maintain a variable current best then the update step is
current best = A[i] + max(current best, 0).
This is shown as Kadane’s algorithm on Wikipedia (https://en.wikipedia.
org/wiki/Maximum_subarray_problem), where you can also find a detailed
description of it.