


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



ECS 122A: Algorithm Design and Analysis Handout M UC Davis — Charles Martel May 2, 2008
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 )
k points are within d of the dividing line.
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
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 )
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
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 ).