CMPSCI 311: Theory of Algorithms
David Mix Barrington
Fall, 2003
Midterm Exam #1
Given in class Friday 3 October 2003
Directions: Each of the first two questions
is a statement that may be true
or false. Please state whether it is true or false -- you will
get five points for a correct boolean answer and there is no penalty
for a wrong guess. Then justify your answer -- the remaining
points will depend on the quality and validity of your justification.
- Question 1 (10):
(true/false with justification)
If f, g, and h are positive increasing
functions with f in O(h) and g in Ω(h)$, then the function
f+g must be in Θ(h).
- Question 2 (10):
(true/false with justification)
Let W(n), A(n), and B(n) be the
worst-case, average-case, and best-case costs respectively of a particular
algorithm on inputs of size n. Then A(n) is in Θ(W(n)+B(n)).
- Question 3 (20):
Let T(n) be the function defined by the following recurrence:
T(0) = 3
T(1) = 4
T(n) = (T(n-1)*T(n-2) - 2)/n, for n≥2
Give an exact solution for T(n) and
prove your solution correct by induction.
- Question 4 (40):
Let A be an array of n positive
int
values, where n≥3.
The best triple of A is the set of three distinct indices i, j,
and k, with i≤j≤k, that makes the product
A[i]*A[j]*A[k] as large as possible. You may assume that
this product never causes an overflow.
- (a,20)
Write a brute-force algorithm that takes A as a parameter and returns an
array B such that B[0]=i, B[1]=j, and B[2]=k where i, j, and k
form the best triple of A. Analyze the worst-case
running time of your algorithm in
terms of n (the length of A), finding the correct Θ-class.
- (b,10)
State and justify a lower bound (in Ω form)
on the time needed to solve this problem.
- (c,10)
Informally describe and analyze an algorithm for this problem that is
asymptotically faster than your brute-force algorithm in (a). For full
credit, its time should match the lower bound in (b), though you may get
up to eight points for any asymptotic improvement.
- Question 5 (20):
Two questions about a variant of Mergesort:
- (a,10)
Suppose you have three sorted arrays A, B, and C, each of
length n/3, and you want to merge them into a single sorted array D
of length n containing the same elements. Indicate how you will do this
(pseudocode or even English is fine) and determine the Θ-class of
the running time of your algorithm.
- (b,15)
Consider the variant of Mergesort where you divide the given array into
three equal parts, sort each part, and merge them together as in (a).
State and justify a recurrence for the running time T(n)
of this algorithm on an input array of length n. Solve this recurrence
for the case when n is a power of three. (You may quote the Master
Theorem if it is applicable.) In what Θ-class is the running time
of this algorithm for all n?
Last modified 3 October 2003