Você está na página 1de 11

CS 341, Winter 2010

Timothy Chan

Midterm Examination

Last Name:

First Name:

Signature:
ID number:

Date: Feb 22, 2010.


Start Time: 7:00pm. End Time: 9:00pm.

Number of pages (including cover and 1 blank page): 11.

/16

No calculators or additional materials are allowed.

/27

Print your initials at the top of each page.

/13

All answers should be placed in the spaces given. Backs


of pages may be used as scratch papers and will not be
marked unless you clearly indicate otherwise. If you need
more space to complete an answer, you may use the blank
page at the end.

/13

/14

/17

Cheating is an academic offense. Your signature on this


exam indicates that you understand and agree to the Universitys policies regarding cheating on exams.

Total

/100

Marks

Init.

Initials:

1. [16 marks] For each pair of functions f (n) and g(n), fill in the correct asymptotic notation
among , o, and in the statement f (n) ! (g(n)). Include brief justifications of all your
answers.
(a) f (n) = 2010n3 vs. g(n) = n3.5

(b) f (n) = (log n)341 + n0.350 vs. g(n) = (log n)350 + n0.341

(c) f (n) =

!n

100
i=1 i

(d) f (n) = 4n/2

vs. g(n) = 1.01n

vs. g(n) = 2n4

Initials:
2. [27 marks] Short questions.

(a) [4 marks] State the recurrence for the running time of the divide-and-conquer algorithm
for the (two-dimensional) maxima problem from class. (Recall that in this problem, we
are given a point set P and want to find all points q P that are not dominated by any
other point in P . Assume that the input points have been pre-sorted from left to right.)

T (n) =

"

(b) [4 marks] True or False: Karatsuba and Ofmans divide-and-conquer algorithm for
multiplying two n-bit numbers is faster than the naive elementary school algorithm
for all values of n. Briefly explain.

(c) [4 marks] True or False: for k 100, we can find the k-th smallest element of n given
elements in O(n) time by using a variant of selectionsort. Briefly explain.

Initials:

(d) [5 marks] Run the (correct) greedy algorithm from class for the disjoint intervals problem
on the following input intervals: {[1, 8], [2, 4], [3, 6], [5, 7], [9, 10]}. Show your work.
(Recall that in this problem, we want to find a largest subset of disjoint intervals.)

(e) [5 marks] Consider the following problem called 0/1-Knapsack : We are given n items
where item i has value vi > 0 and weight wi > 0. We are also given a number W . The
!
problem is to find a subset S {1, . . . , n} of items, maximizing the total value iS vi ,
!
such that the total weight iS wi is at most W . (Unlike the fractional knapsack problem
from class, we are not allowed to choose a fraction of an item.)
Consider the following greedy strategy: Choose an item i that has the largest ratio v i /wi ,
with wi W . Remove the item and subtract wi from W . Repeat.
Run this algorithm on the following example with n = 3: v1 = 50, v2 = 20, v3 = 1,
w1 = 50, w2 = 10, w3 = 5, W = 55. Does this greedy strategy give a correct optimal
solution on this example? Explain.

Initials:

(f) [5 marks] Consider the following optimization problem: Given a set S of n numbers,
find a subset T S, maximizing the number of elements in T , such that every two
elements of T have distance greater than 1. (The distance between two elements x and
y is defined as |x y|.)
Example: for S = {0.2, 0.3, 0.7, 1.3, 1.5, 1.8, 2.7}, one optimal solution is {0.4, 1.5, 2.7},
which has size 3.
Let T be an optimal solution. Let x be the smallest element in S. Prove that there
always exists an optimal solution which contains x, by modifying T .

Initials:

3. [13 marks] Analyze the following pseudocode and give a tight () bound on the running time
as a function of n. Show all your steps. (Here, . . . refers to some constant-time operations
that do not change the values of i, j, k, ", and n.)
1. for i = 1 to n do {
2.
for j = 1 to i do {
3.
for k = 1 to i do
4.
...
5.
for k = 2i to 4i do
6.
...
}
7.
"=n
8.
while " > 0 do {
9.
...
10.
" = " 2i
}
}

Initials:

4. [13 marks] Solve the following recurrence by the recursion-tree method (you may assume
that n is a power of 4). Show your work. Express your answer as a tight () asymptotic
bound on T (n).
"
3 T (n/4) + n2 if n > 1
T (n) =
5
if n 1

Initials:

5. [14 marks] Given an array of n elements A[1], . . . , A[n] where n is even, we want to swap
elements so that at the end, the array becomes
A[1], A[3], . . . , A[n 1], A[2], A[4], . . . , A[n].
Call this the split problem. Example: if the array initially contains 1, 2, 3, 4, 5, 6, 7, 8, it will
become 1, 3, 5, 7, 2, 4, 6, 8.
You will design and analyze an O(n log n)-time divide-and-conquer algorithm for the split
problem that requires only O(log n) extra space (outside of the array A itself). You may
assume that n is a power of 2.
(a) [9 marks] Give a recursive pseudocode. [Hint: this should be very similar to the
pseudocode from a certain assignment question.]

(b) [5 marks] Analyze your algorithm by writing a recurrence for the running time. You
do not need to solve this recurrence.

Initials:

6. [17 marks] Given n positive numbers a1 , . . . , an and W , we want to decide whether there
!
exist a value t such that i : ai t ai = W , i.e., the sum of all elements at most t is exactly W .
Example: for the numbers 21, 1, 10, 34, 5, 8 and W = 24, the answer is yes (t = 10). You may
assume that n is a power of 2.

You will design and analyze an efficient algorithm to solve this problem in strictly better than
O(n log n) time.
(a) [9 marks] Give a recursive pseudocode. [Hint: you may use the linear-time selection
algorithm from class as a subroutine.]

(b) [4 marks] Analyze your algorithm. The running time should satisfy the following recurrence:
"
T (n/2) + O(n) if n > 1
T (n) =
O(1)
if n = 1

Initials:

10

(c) [4 marks] Solve the recurrence in (b) by the Master method to determine the running
time T (n).

Initials:
(Extra space)

11

Você também pode gostar