Vous êtes sur la page 1sur 3

CSE560 Algorithms (Spring’19)

Problem Set #1
Dynamic Programming Do not submit

1. Let n coins are placed in a row with (not necessarily distint) values c1 , c2 , . . . , cn . Give a linear time
algorithm to pick up maximum amount of money subject to the constraint that no two coins adjacent
can be picked up.

2. Yuckdonald’s is considering opening a series of restaurants along Karachi–Lahore Highway (KLH). The
n possible locations are along a straight line, and the distances of these locations from the start of KLH
are, in kilometers and in increasing order, k1 , k2 , . . . , kn . The constraints are as follows:

• At each location, Yuckdonald’s may open at most one restaurant. The expected profit from opening
a restaurant at location i is pi , where pi > 0 and i = 1, 2, . . . , n.
• Any two restaurants should be at least d kilometers apart, where d is a positive integer.

Give an efficient algorithm to compute the maximum expected total profit subject to the given con-
straints.

3. Given an array of n real numbers, consider the problem of finding the maximum sum in any contiguous
subvector of the input. For example, in the array [31, −41, 59, 26, −53, 58, 97, −93, −23, 84] the
maximum is achieved by summing the third through seventh elements, where 59 + 26 + (−53) + 58 + 97 =
187. When all numbers are positive, the entire array is the answer, while when all numbers are negative,
the empty array maximizes the total at 0.
(a) Give a simple Θ(n2 )-time algorithm to find the maximum contiguous subvector.
(b) Give a O(n log n) divide-and-conquer algorithm.
(c) Give a Θ(n)-time dynamic programming algorithm for this problem.

4. Consider a post office that sells stamps in three different denominations, Rs 1, Rs 7, and Rs 10. Design a
dynamic programming algorithm that will find the minimum number of stamps necessary for a postage
value of n rupees. (Note that a greedy type of algorithm won’t necessarily give you the correct answer,
and you should be able to find an example to show that such a greedy algorithm doesn’t work.) What
is the running time of your algorithm?

5. Given an unlimited supply of coins of denominations x1 , x2 , . . . , xn , we wish to make change for a value
v; that is, we wish to find a set of coins whose total value is v. This might not be possible: for instance,
if the denominations are 5 and 10 then we can make change for 15 but not for 12. Give an O(nv)
dynamic-programming algorithm for the following problem.

Input: x1 , . . . , xn ; v.
Question: Is it possible to make change for v using coins of denominations x1 , . . . , xn ?

Last updated: 2019-02-03 14:56 Page 1 of 3


Problem Set #1 Do not submit

6. Consider the following variation on the change-making problem: you are given denominations x1 , x2 , . . . , xn ,
and you want to make change for a value v, but you are allowed to use each denomination at most once.
For instance, if the denominations are 1, 5, 10, 20, then you can make change for 16 = 1 + 15 and for
31 = 1 + 10 + 20 but not for 40 (because you can’t use 20 twice).

Input: Positive integers x1 , x2 , . . . , xn ; another integer v.


Question: Can you make change for v, using each denomination xi at most once??

Show how to solve this problem in time O(nv).

7. Describe and analyze an algorithm to find the length of the longest substring that appears both forward
and backward in an input string T [1..n]. The forward and backward substrings must not overlap. Here
are several examples:

• Given the input string ALGORITHM, your algorithm should return 0.


• Given the input string RECURSION, your algorithm should return 1, for the substring R.
• Given the input string REDIVIDE, your algorithm should return 3, for the substring EDI. (The
forward and backward substrings must not overlap!)
• Given the input string DYNAMICPROGRAMMINGMANYTIMES, your algorithm should return 4,
for the substring YNAM.

For full credit, your algorithm should run in O(n2 ) time.

8. Suppose you are given three strings of characters: X, Y , and Z, where |X| = n, |Y | = m, and
|Z| = n + m. Z is said to be a shuffle of X and Y iff Z can be formed by interleaving the characters
from X and Y in a way that maintains the left-to-right ordering of the characters from each string.
(a) Show that cchocohilaptes is a shuffle of chocolate and chips, but chocochilatspe is not.
(b) Give an efficient dynamic-programming algorithm that determines whether Z is a shuffle of X and
Y . Hint: the values of the dynamic programming matrix you construct should be Boolean, not
numeric.

9. In dynamic programming, we derive a recurrence relation for the solution to one subproblem in terms of
solutions to other subproblems. To turn this relation into a bottom up dynamic programming algorithm,
we need an order to fill in the solution cells in a table, such that all needed subproblems are solved
before solving a subproblem. For each of the following relations, give such a valid traversal order, or if
no traversal order is possible for the given relation, briefly justify why.
(a) A(i, j) = F (A(i, j − 1), A(i − 1, j − 1), A(i − 1, j + 1))
(b) A(i, j) = F (A(min {i, j} − 1, min {i, j} − 1), A(max {i, j} − 1, max {i, j} − 1))
(c) A(i, j) = F (A(i − 2, j − 2), A(i + 2, j + 2))

Last updated: 2019-02-03 14:56 Page 2 of 3


Problem Set #1 Do not submit

10. Time and space complexity of dynamic programming. Our dynamic programming algorithm for comput-
ing the edit distance between strings of length m and n creates a table of size n × m and therefore needs
O(mn) time and space. In practice, it will run out of space long before it runs out of time. How can
this space requirement be reduced?
(a) Show that if we just want to compute the value of the edit distance (rather than the optimal
sequence of edits), then only O(n) space is needed, because only a small portion of the table needs
to be maintained at any given time.
(b) Now suppose that we also want the optimal sequence of edits. As we saw earlier, this problem can
be recast in terms of a corresponding grid-shaped dag, in which the goal is to find the optimal path
from node (0, 0) to node (n, m). It will be convenient to work with this formulation, and while we’re
talking about convenience, we might as well also assume that m is a power of 2.
Let’s start with a small addition to the edit distance algorithm that will turn out to be very useful.
The optimal path in the dag must pass through an intermediate node (k, m/2) for some k; show
how the algorithm can be modified to also return this value k.
(c) Now consider a recursive scheme:
function find-path((0, 0) → (n, m))
compute the value k above
find-path((0, 0) → (k, m/2))
find-path((k, m/2) → (n, m))
concatenate these two paths, with k in the middle

Show that this scheme can be made to run in O(mn) time and O(n) space.

Last updated: 2019-02-03 14:56 Page 3 of 3

Vous aimerez peut-être aussi