Docsity

Solved Questions for Sample Midterm Exam - Algorithm Design | ECS 122A, Exams of Computer Science

Toggle sidebar
bg1
ECS 122A: Algorithm Design and Analysis Handout M
UC Davis — Charles Martel May 2, 2008
Sample Midterm Exam
1 Short answers [40 points]
•(10) Part A. Solve the following recurrence by finding a Θ() bound for T(n). Assume
that T(n) is 1 for n≤1, and otherwise is defined by the recurrence.
T(n) = 15T(n/4) + 5n2+ 7n
Solution: Using the master theorem. a= 15, b = 4, f (n) = 5n2+ 7n= Θ(n2) so,
i= 2. Case 3 applies since a < bi,T(n) = Θ(n2)
•(15) Part B. For the closest point algorithm we discussed in class, suppose we knew
that every time we divide some number of points k, when we execute the combine step
(where we use the “strip” points which are within dof the dividing line) at most √k
points are within dof the dividing line.
– i) For this setting write a recurrence relation for W(n), the maximum number
of distance computations (where we compute the distance between two pairs of
points). DO NOT use big O notation. Try and give the constants as precisely as
you can. Note that you can ignore work other than distance computations.
– ii) Solve your recurrence to within Θ accuracy.
Solution:
In the regular version, each point on the left may be compared to 6 other points during
the combine phase. In this problem, at most √n/2 points will be compared to six
other points.
W(n) = 2W(n/2) + 3√n
You are told to give constants as precisely as you can, therefore is not completely
correct to let f(n) = O(√n) Also those who put f(n) = O(n) didn’t receive much
credit because this is the solution for the regular version from the book.
ii) Solve your recurrence to within Θ accuracy.
Using the master’s, W(n) = θ(n)
2·(n/4+1)+(n/2)·2+(3n−n/2−2)·1
3n
n/2+2+n+3n−n/2−2)
3n
4
3
pf3
pf4

Partial preview of the text

Download Solved Questions for Sample Midterm Exam - Algorithm Design | ECS 122A and more Exams Computer Science in PDF only on Docsity!

ECS 122A: Algorithm Design and Analysis Handout M UC Davis — Charles Martel May 2, 2008

Sample Midterm Exam

1 Short answers [40 points]

  • (10) Part A. Solve the following recurrence by finding a Θ() bound for T (n). Assume that T (n) is 1 for n ≤ 1, and otherwise is defined by the recurrence.

T (n) = 15T (n/4) + 5n^2 + 7n

Solution: Using the master theorem. a = 15, b = 4, f (n) = 5n^2 + 7n = Θ(n^2 ) so, i = 2. Case 3 applies since a < bi, T (n) = Θ(n^2 )

  • (15) Part B. For the closest point algorithm we discussed in class, suppose we knew that every time we divide some number of points k, when we execute the combine step (where we use the “strip” points which are within d of the dividing line) at most

k points are within d of the dividing line.

  • i) For this setting write a recurrence relation for W (n), the maximum number of distance computations (where we compute the distance between two pairs of points). DO NOT use big O notation. Try and give the constants as precisely as you can. Note that you can ignore work other than distance computations.
  • ii) Solve your recurrence to within Θ accuracy.

Solution: In the regular version, each point on the left may be compared to 6 other points during the combine phase. In this problem, at most

n/2 points will be compared to six other points. W (n) = 2W (n/2) + 3

n You are told to give constants as precisely as you can, therefore is not completely correct to let f (n) = O(

n) Also those who put f (n) = O(n) didn’t receive much credit because this is the solution for the regular version from the book. ii) Solve your recurrence to within Θ accuracy. Using the master’s, W (n) = θ(n)

2 ·(n/4+1)+(n/2)·2+(3n−n/ 2 −2)· 1 3 n n/2+2+n+3n−n/ 2 −2) 3 n 4 3

2 Short answers Midterm 2 version [40 points]

  • (10) Part A. Solve the following recurrence by finding a Θ() bound for T (n). Assume that T (n) is 1 for n ≤ 1, and otherwise is defined by the recurrence.

T (n) = 15T (n/3) + 5n^2 + 7n

Solution: Using the master theorem. a = 15, b = 3, f (n) = 5n^2 + 7n, i = 2 Case 1 applies since a > bi, T (n) = Θ(nlogba) = Θ(nlog^315 )

  • (15) Part B. For the closest point algorithm we discussed in class, suppose we knew that every time we execute the combine step (where we use the “strip” points which are within d of the dividing line) at most half the points are within d of the dividing line. - i) For this setting write a recurrence relation for W (n), the maximum number of distance computations (where we compute the distance between two pairs of points). DO NOT use big O notation. Try and give the constants as precisely as you can. Note that you can ignore work other than distance computations. - ii) Solve your recurrence to within Θ accuracy.

Solution: In the regular version, each point may be compared to 6 other points during the combine phase. In this problem, at most half the n/2 points (so n/4) will be compared to six other points. W (n) = 2W (n/2) + 3n/ 2 You are told to give constants as precisely as you can. Those who put f (n) = n didn’t receive much credit because this is the solution for the regular version from the book. The answer needed to show the importance of the n/2. ii) Solve your recurrence to within Θ accuracy. Using the master’s, W (n) = θ(nlogn)

1 ·(n/2+1)+(n/2)·2+(2n−n/ 2 −1)· 1 2 n n/2+1+n+2n−n/ 2 −1) 2 n

3 2

6 Dynamic Programming [22 points]

We consider a variant of the knapsack problem. We have n regular items each with a value vi and a weight wi and a global weight limit W. You want to find a set of items whose total value is as large as possible, and the total cost is at most W. We also have three special items with weights wn+1, wn+2, wn+3 and values vn+1, vn+2, vn+3. We can use at most one of these three items in the solution (but we still have to obey the weight limit W for all items).

Part A. Describe how to find the best total value in this setting when all the items are fractional (if you use part of a special item, you cannot use part of another special item).

Be sure to justify your answer and give its running time.

Part B. Describe how to find the best value in the 0-1 setting where you must take all or none of each item. As in A justify the correctness of your solution and give its running time.

We compute ri = vi/wi ratio for the n regular items and find the best set of items 1, 2 ,... i with the highest rj values using the regular greedy algorithm.

For each of the three special items, if its ratio is better than ri, we replace item i by the special item, then i − 1 ,... until we run out of the special item or we hit an item with ratio better than that of the special item.

We compare these three totals, and use the best one.

Each of the steps can be done in Θ(n) time, so total time is Θ(n).

To see correctness, note that we try each option (use special item 1, 2 and three), and use it in the best way (replacing worst ratio items with that item).

Note that we can’t just use the special item with the best ratio. Since its weight may be smaller than another item (e.g. all regular items have ri = 1, W = 10, and one special item has value 20 and weight 2 while another has value 60 and weight 10. The first item has higher ratio, the second is used in an optimal solution.

Part B. Describe how to find the best value in the 0-1 setting where you must take all or none of each item. As in A justify the correctness of your solution and give its running time.

Run the normal D.P. algorithm on the regular items. The final row T [n, j] has the best value of each weight j ≤ W using all n items. Now for each special item, consider it as an n + 1 item continuing the D.P. formulation. The maximum value in each of these three rows gives the best solution with the option of using that item. Thus the maximum of the three n 1 rows is the best solution.

Time is dominated by the initial D.P. Θ(nW ).