Docsity

3 Questions on Algorithm Design - Assignment 3 | ECS 122A, Assignments of Computer Science

Toggle sidebar
bg1
ECS 122A: Algorithm Design and Analysis Homework 3
UC Davis: Charles Martel, Susan Margulies Due Friday, May 2, 2008, 3:30 PM
Problem Set 3
Homework Info Homeworks are due by 3:30 PM on the due date. They are to be turned in at
the marked box in K
¯emper Hall, room 2131 (right side). No late homeworks will be accepted.
Much of what one learns in this course comes from trying to solve the homework problems, so work hard on
them. Doing a conscientious job on the homeworks is the best preparation for the exams. We hope that you
will ultimately solve the majority of the problems, but don’t be surprised if some of them stump you; some
of the problems may be quite challenging.
Your solutions should be terse, correct, and legible. Understandability of the solution is as necessary as
correctness. Expect to lose points if you provide a ”correct” solution with a not-so-good writeup. As with
an English paper, you can’t expect to turn in a first draft: it takes refinement to describe something well.
Typeset solutions are always appreciated.
If you can’t solve a problem, briefly indicate what you’ve tried and where the difficulty lies. Don’t try to
pull one over on us.
If you think a problem was misgraded, please see the grader first. (The TA will be the grader for the
homework).
1 Subset Sum, KT: 6.4 (25 pts)
We consider some variants of the subset sum problem discussed in class: given a set of positive
integers L={x1, x2, . . . xn}and a target B, the Subset Sum problem asks you to find S⊆Lsuch
that the sum of the elements in Sis no greater than Bbut is also as close to Bas possible.
a. We assumed the xiwere integers. Why was this important? Would it cause a problem if b
was not an integer (assuming the xiare still 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 xito a total which already
used.
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 3xi).
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).
ii. Describe how to find the actual items in the best solution.
pf3

Partial preview of the text

Download 3 Questions on Algorithm Design - Assignment 3 | ECS 122A and more Assignments Computer Science in PDF only on Docsity!

ECS 122A: Algorithm Design and Analysis Homework 3 UC Davis: Charles Martel, Susan Margulies Due Friday, May 2, 2008, 3:30 PM

Problem Set 3

Homework Info Homeworks are due by 3:30 PM on the due date. They are to be turned in at

the marked box in K ¯

emper Hall, room 2131 (right side). No late homeworks will be accepted.

Much of what one learns in this course comes from trying to solve the homework problems, so work hard on them. Doing a conscientious job on the homeworks is the best preparation for the exams. We hope that you will ultimately solve the majority of the problems, but don’t be surprised if some of them stump you; some of the problems may be quite challenging.

Your solutions should be terse, correct, and legible. Understandability of the solution is as necessary as correctness. Expect to lose points if you provide a ”correct” solution with a not-so-good writeup. As with an English paper, you can’t expect to turn in a first draft: it takes refinement to describe something well. Typeset solutions are always appreciated.

If you can’t solve a problem, briefly indicate what you’ve tried and where the difficulty lies. Don’t try to pull one over on us.

If you think a problem was misgraded, please see the grader first. (The TA will be the grader for the homework).

1 Subset Sum, KT: 6.4 (25 pts)

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).

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.

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 3xi).

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).

ii. Describe how to find the actual items in the best solution.

2 ECS 122A Handout 3: Problem Set 3

2 Longest Increasing Subsequence, KT: 6.6 (25 pts)

Given an array A of n integers a[1], a[2],... , a[n] in unsorted order, we say that a[j 1 ], a[j 2 ],... , a[jt] is an increasing subsequence of A of length t, if the following conditions are met:

  • j 1 < j 2 < · · · < jt
  • a[j 1 ] ≤ a[j 2 ] ≤ · · · ≤ a[jt]

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 , 18] is an increasing subsequence of length 4. We want to find an algorithm that, given A finds in polynomial time 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.

b. Describe a dynamic programming algorithm having running time polynomial in n (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

  • Specify what the dynamic programming matrix (or vector) is supposed to contain; how big it is; and how to retrieve the answer to the subsequence problem once the matrix (or vector) is filled.
  • Specify which entries can be filled at the beginning, and with which values; and give a recursive formula for the other entries.
  • Estimate the time needed to fill one entry, and give the total time needed to fill the whole matrix and to find the answer to the problem.

3 Closest Point, KT: 5.4 (25 pts)

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.