CM-339-CS-341-1101-Midterm_exam

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. No calculators or additional materials are allowed. Print your initials at the top of each page. 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. Cheating is an academic offense. Your signature on this exam indicates that you understand and agree to the Uni- versity’s policies regarding cheating on exams. Q Marks Init. 1 /16 2 /27 3 /13 4 /13 5 /14 6 /17 Total /100 1

description

cs stuff

Transcript of CM-339-CS-341-1101-Midterm_exam

Page 1: CM-339-CS-341-1101-Midterm_exam

CS 341, Winter 2010Timothy 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.

• No calculators or additional materials are allowed.

• Print your initials at the top of each page.

• All answers should be placed in the spaces given. Backsof pages may be used as scratch papers and will not bemarked unless you clearly indicate otherwise. If you needmore space to complete an answer, you may use the blankpage at the end.

• Cheating is an academic o!ense. Your signature on thisexam indicates that you understand and agree to the Uni-versity’s policies regarding cheating on exams.

Q Marks Init.

1 /16

2 /27

3 /13

4 /13

5 /14

6 /17

Total /100

1

Page 2: CM-339-CS-341-1101-Midterm_exam

Initials: 2

1. [16 marks] For each pair of functions f(n) and g(n), fill in the correct asymptotic notationamong ", o, and ! in the statement f(n) ! ! (g(n)). Include brief justifications of all youranswers.

(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

i=1 i100 vs. g(n) = 1.01n

(d) f(n) = 4n/2 vs. g(n) = 2n!4

Page 3: CM-339-CS-341-1101-Midterm_exam

Initials: 3

2. [27 marks] Short questions.

(a) [4 marks] State the recurrence for the running time of the divide-and-conquer algorithmfor the (two-dimensional) maxima problem from class. (Recall that in this problem, weare given a point set P and want to find all points q ! P that are not dominated by anyother 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 Ofman’s divide-and-conquer algorithm formultiplying two n-bit numbers is faster than the naive “elementary school” algorithmfor 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 givenelements in O(n) time by using a variant of selectionsort. Briefly explain.

Page 4: CM-339-CS-341-1101-Midterm_exam

Initials: 4

(d) [5 marks] Run the (correct) greedy algorithm from class for the disjoint intervals problemon 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 itemswhere item i has value vi > 0 and weight wi > 0. We are also given a number W . Theproblem is to find a subset S # {1, . . . , n} of items, maximizing the total value

!i"S vi,

such that the total weight!

i"S wi is at most W . (Unlike the fractional knapsack problemfrom 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 vi/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 optimalsolution on this example? Explain.

Page 5: CM-339-CS-341-1101-Midterm_exam

Initials: 5

(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 twoelements of T have distance greater than 1. (The distance between two elements x andy 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 therealways exists an optimal solution which contains x, by modifying T #.

Page 6: CM-339-CS-341-1101-Midterm_exam

Initials: 6

3. [13 marks] Analyze the following pseudocode and give a tight (") bound on the running timeas a function of n. Show all your steps. (Here, “. . . ” refers to some constant-time operationsthat 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 do4. . . .5. for k = 2i to 4i do6. . . .

}7. " = n8. while " > 0 do {9. . . .

10. " = "$ 2i

}}

Page 7: CM-339-CS-341-1101-Midterm_exam

Initials: 7

4. [13 marks] Solve the following recurrence by the recursion-tree method (you may assumethat n is a power of 4). Show your work. Express your answer as a tight (") asymptoticbound on T (n).

T (n) ="

3T (n/4) + n2 if n > 15 if n " 1

Page 8: CM-339-CS-341-1101-Midterm_exam

Initials: 8

5. [14 marks] Given an array of n elements A[1], . . . , A[n] where n is even, we want to swapelements 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 willbecome 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 splitproblem that requires only O(log n) extra space (outside of the array A itself). You mayassume that n is a power of 2.

(a) [9 marks] Give a recursive pseudocode. [Hint: this should be very similar to thepseudocode from a certain assignment question.]

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

Page 9: CM-339-CS-341-1101-Midterm_exam

Initials: 9

6. [17 marks] Given n positive numbers a1, . . . , an and W , we want to decide whether thereexist 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 mayassume that n is a power of 2.

You will design and analyze an e#cient algorithm to solve this problem in strictly better thanO(n log n) time.

(a) [9 marks] Give a recursive pseudocode. [Hint: you may use the linear-time selectionalgorithm from class as a subroutine.]

(b) [4 marks] Analyze your algorithm. The running time should satisfy the following re-currence:

T (n) ="

T (n/2) + O(n) if n > 1O(1) if n = 1

Page 10: CM-339-CS-341-1101-Midterm_exam

Initials: 10

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

Page 11: CM-339-CS-341-1101-Midterm_exam

Initials: 11

(Extra space)