CPS 130 Exam 1

Summer 2002

3:30-4:45, Friday May 31st Closed book exam

NAME:

Problem Max Obtained
1 (a)  15
1 (b)  15
2 (a)  10
2 (b)  10
3  15
4 (a)  10
4 (b)  10
4 (c)  15
Total  100

Comments:

• You can use any of the algorithms covered in class without describing them.

• When asked to describe an algorithm it is completely okay to do so with words (and a few accompanying pictures if it helps the description). Remember to include an argument for both correctness and running time.

HONOR CODE

I have obeyed the honor code.

SIGNATURE:

[30 points ] Problem 1:

a) Using the iteration method find an asymptotic tight bound for the recurrence:

T (n) = \{ \begin{matrix}1 & if n ≤ 3 \\ T (\sqrt{n}) + 1 & if n ≥ 4 \end{matrix}

b) Show using the substitution method (induction) that the recurrence above has solution T (n) = O(lg lg n).

[20 points ] Problem 2:

Let A be an array of n (not necessarily distinct) integers.

a) Describe an O(n)-algorithm to test whether any item occurs more than ⌈n/2⌉ times in A.

b) Describe an O(n)-algorithm to test whether any item occurs more than ⌈n/4⌉ times in A.

[15 points ] Problem 3:

In this problem we consider a monotonically decreasing function f : N → Z (that is, a function defined on the natural numbers taking integer values, such that f(i) > f(i + 1)). Assuming we can evaluate f at any i in constant time, we want to find n = min{i ∈ N|f(i) ≤ 0} (that is, we want to find the value where f becomes negative).

We can obviously solve the problem in O(n) time by evaluating f(1), f(2), f(3), … f(n). Describe an O(log n) algorithm.

(Hint: Evaluate f on O(log n) carefully chosen values between 1 and 2n - but remember that you do not know n initially).

[35 points ] Problem 4:

The maximum partial sum problem (MP S) is defined as follows. Given an array A[1..n] of integers, find values of i and j with 1 ≤ i ≤ j ≤ n such that

∑_{k=i}^{j} A[k]

is maximized.

Example: For the array [4,-5,6,7,8,-10,5], the solution to MP S is i = 3 and j = 5 (sum 21).

To help us design an efficient algorithm for the maximum partial sum problem, we consider the left position ℓ maximal partial sum problem (LMP S_{ℓ}). This problem consists of finding value j with ℓ ≤ j ≤ n such that

∑_{k=ℓ}^{j} A[k]

is maximized. Similarly, the right position r maximal partial sum problem (RMP S_{r}), consists of finding value i with 1 ≤ i ≤ r such that

∑_{k=i}^{r} A[k]

is maximized.

Example: For the array [4,-5,6,7,8,-10,5] the solution to e.g. LMP S_{4} is j = 5 (sum 15) and the solution to RMP S_{7} is i = 3 (sum 16). a) Describe O(n) time algorithms for solving LMP S_{ℓ} and RMP S_{r} for given ℓ and r.

7 b) Using an O(n) time algorithm for LMP S_{ℓ}, describe a simple O(n^{2}) algorithm for solving MP S. c) Using O(n) time algorithms for LMP S_{ℓ} and RMP S_{r}, describe an O(n log n) divide-and-conquer algorithm for solving MP S.