



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 Homework 3 UC Davis: Charles Martel, Susan Margulies Due Friday, May 2, 2008, 3:30 PM
We consider some variants of the subset sum problem discussed in class: given a set of positive integers L = {x 1 , x 2 ,... xn} and a target B, the Subset Sum problem asks you to find S ⊆ L such that the sum of the elements in S is no greater than B but is also as close to B as possible.
a. We assumed the xi were integers. Why was this important? Would it cause a problem if b was not an integer (assuming the xi are still integers). Solution The value of xis correspond to offsets in the array indices of the table used (e.g. we refer to a cell of index [i − 1 , j − xi] when computing the new value in cell [i, j]). So, they were assumed to be integers. If xis were not integers, then we have to scale up the problem; that is, increase all xis and B by the same factor so that all the xis are now integers. For example, if the items were 12.15 and 6.3, we have to multiply both the values by 100 so that the new items are 1215 and 630 and the value of B is also increased by a factor of 100. Solution No. We simply replace B by bBc since a non-integer value can never be obtained using all integers.
b. Describe how to modify the dynamic programming formulation so that it only uses a single array of size B + 1 to find the target value closest to B (you do not need to find the actual set of items). Be careful that you don’t add the value of the item xi to a total which already used xi once. Solution The original subset sum recursive formula sets M [i, j] = 1 if and only if the sum j can be exactly obtained when using numbers x 1 ,... , xi. The recursive formula is as follows:
M [i, j] =
1 if j = 0 M [i − 1 , j] if j < xi max
M [i − l, j], M [i − 1 , j − xi]
otherwise
This recursive formula creates an (n+1)×(B +1) table. However, by studying this recurrence relation, we notice that we only need to know the values of the previous row to fill in the current row. More precisely, to fill an entry in the i-th row, we only need the values to its left in the (i − 1)-th row. Due to the recurrence, if a slot in the table has value 1, all the slots below it (in the same column) will also have the value 1. So, we can use just a single array of size B + 1 to represent some row and reuse it repeatedly to represent successive rows. While computing values for the next row from the previous row, we overwrite the array from right to left. Instead, if we overwrite from left to right, we would no longer have the values in the previous row required to compute row values in the right of the current row. The following code computes the target value closest to B:
ALGORITHM: Space Efficient Subset Sum INPUT: X = {x 1 ,... , xn} and an integer B OUTPUT: 1, if there exists a subset of X that sums to exactly B 0, otherwise Let A be an array of size B + 1 Let A[0] ← A[x 1 ] ← 1 , A[i] ← 0 for i ← 2 to n do for j ← B by −1 to xi do if A[j − xi] = 1 then A[j] ← 1 end if end for end for return A[n]
The rightmost index with value 1 gives the computed solution. Note that the for loop for j only goes down to xi since we can’t use item i to help with any sums smaller than xi (so those cells don’t change in this iteration).
c. Suppose we can now use multiple copies of an item (e.g. we could put three copies of item one to get a total of 3x 1 ).
i. Describe how to find the best total value you can achieve by modifying the standard Subset Sum Dynamic programming formulation. Give the time and space used by your solution (try to minimize both). Solution To determine the recurrence for T(i,j) when using multiple copies of items, consider the following two exhaustive cases:
For example, given the array [1, 4 , 7 , 3 , 11 , 2 , 5 , 13 , 6], [3, 5] is one possible increasing subsequence of length 2, while [1, 4 , 7 , 11 , 13] is an increasing subsequence of length 5. We want to find an efficient algorithm that, given A, finds the length of the longest increasing sequence of A.
a. Consider the following attempt at a dynamic programming solution. We use a vector M [1,... , n] with the intended meaning that M [k] contains the length of the longest increasing subsequence of a[1],... , a[k]. After the vector is filled, the solution to our problem is contained in M [n]. We give the following recursive formula for M :
M [i] =
1 if i = 1 M [i − 1] if A[i − 1] > A[i] M [i − l] + 1 otherwise
Prove that this algorithm is wrong, by describing a counter-example. That is, describe an input instance A such that the above recursive rules construct a vector M that does not contain the right entries, and such that, in particular, the last entry of M is not the length of the longest increasing subsequence of A. Solution: A counterexample is A = [3, 4 , 1 , 2]. The longest increasing subsequence has length 2, but the algorithm just counts the number of indices i such that A[i] > A[i − 1]. Thus, in this case, the algorithm returns 3 (as if A[1],A[2],A[4] were an increasing subsequence).
b. Describe an efficient dynamic programming algorithm (running time O(n^2 ) is enough to get full credit) that, given A, finds the length of the longest increasing subsequence of A. The presentation of your solution should be structured as follows
Solution:
δ(A[j], A[k]) =
1 if A[j] ≤ A[k] 0 otherwise
Clearly, M [1] = 1 because A[1] will always be a longest increasing subsequence size 1. This is our base case. We fill the array according the following recursive formula which,
to compute M [i], looks for the longest increasing sequence ending at an A[j] < A[i]. We then tack on A[i] to get a sequence one longer:
M [i] = max 1 ≤j<i
M [j] × δ(A[j], A[i])
We consider some variants of the closest point algorithm.
a. Suppose we have a set of 16 points. What is the maximum number of distance computations (places where for any two points pi and pj we compute the distance between pi and pj ) done by the algorithm we described in class? Indicate (by a picture if you like) where you would place the 16 points to maximize the number of distance computations done. b. Same as a), but now use the combine step as described in the text. c. Given two points, p 1 and p 2 in the plane, the L∞-distance between them is given by max(|x 1 − x 2 |, |y 1 − y 2 |). Modify the closet-pair algorithm to use the L∞-distance. Be sure to analyze the width of the strip now used and the size of a box you will now use (the old algorithm used a d by 2d box), and the maximum number of points which can be in the box you now use.
d. One possible way to speed up the algorithm is to find two points which are “close” before the algorithm starts (for example, after sorting by the X-coordinate, compute the distance between each consecutive pair in this sorted list, and pick the closest). How might finding a “close” pair initially help speed up a slightly modified version of the algorithm (at least in a typical case)? Would it help your bad example constructed for part a)?
Solution: In this problem, we are asked to consider a variation of the closest point problem, where a different distance metric is used. In this case, we are using the L∞ distance where L∞(p 1 , p 2 ) = max{|x 1 − x 2 |, |y 1 − y 2 |}.
The algorithm itself needs no modification since points outside the strip differ in their x coordinate by at least d from a point on the other side, and points outside the box differ in y-coor. by at least d. Thus no valid cross-compares are missed. However, we must be certain that during the cross-subproblem comparison, that we can still achieve the same point comparison bound.
Given the vertical d × 2 d box described in the algorithm, we can easily tighten our analysis for this case. We can now say there is at most one point in a d by d box if we assume the top, bottom and right boundary lines are not included (points on these lines cannot be closer than d, so aren’t considered).Thus there are now at most 2 points in the d × 2 d box. A similar tight analysis can limit the Euclidean distance metric box to 6 points,