Chapter 6: Dynamic programming
14E
Cutting cloth. You are given a rectangular piece of cloth with dimensions , where and are positive integers, and a list of products that can be made using the cloth. For each product you know that a rectangle of cloth of dimensions is needed and that the final selling price of the product is . Assume the, and are all positive integers. You have a machine that can cut any rectangular piece of cloth into two pieces either horizontally or vertically. Design an algorithm that determines the best return on the piece of cloth, that is, a strategy for cutting the cloth so that the products made from the resulting pieces give the maximum sum of selling prices. You are free to make as many copies of a given product as you wish, or none if desired.
18 E
Consider the following variation on the change-making problem (Exercise 6.17): you are given denominations , and you want to make change for a value , but you are allowed to use each denomination at most once. For instance, if the denominations are then you can make change for and for but not for 40(because you can鈥檛 use 20 twice).
Input: Positive integers; another integer .
Output: Can you make change for , using each denomination at most once?Show how to solve this problem in time .
1E
A contiguous subsequence of a list is a subsequence made up of consecutive elements of . For instance, if is
then is a contiguous subsequence but is not. Give a linear-time algorithm for the following task:Input: A list of numbers .Output: The contiguous subsequence of maximum sum (a subsequence of length zero has sum zero).For the preceding example, the answer would be , with a sum of 55. (Hint: For each , consider contiguous subsequences ending exactly at position j.)
20 E
Optimal binary search trees. Suppose we know the frequency with which keywords occur in programs of a certain language, for instance:
We want to organize them in a binary search tree, so that the keyword in the root is alphabetically bigger than all the keywords in the left subtree and smaller than all the keywords in the right subtree (and this holds for all nodes). Figure 6.12 has a nicely-balanced example on the left. In this case, when a keyword is being looked up, the number of comparisons needed is at most three: for instance, in finding 鈥渨hile鈥, only the three nodes 鈥渆nd鈥, 鈥渢hen鈥, and 鈥渨hile鈥 get examined. But since we know the frequency 196 Algorithms with which keywords are accessed, we can use an even more fine-tuned cost function, the average number of comparisons to look up a word. For the search tree on the left, it is
By this measure, the best search tree is the one on the right, which has a cost of Give an efficient algorithm for the following task. Input: n words (in sorted order); frequencies of these words:
Output: The binary search tree of lowest cost (defined above as the expected number of comparisons in looking up a word).
Figure 6.12 Two binary search trees for the keywords of a programming language.
22E
Give an O(nt) algorithm for the following task. Input: A list of n positive integers ; a positive integer . Question: Does some subset of the ai鈥檚 add up to t? (You can use each ai at most once.) (Hint: Look at subproblems of the form 鈥渄oes a subset of add up to ?鈥)
2E
You are going on a long trip. You start on the road at mile post . Along the way there are hotels, at mile posts , where each is measured from the starting point. The only places you are allowed to stop are at these hotels, but you can choose which of the hotels you stop at. You must stop at the final hotel (at distance ), which is your destination. You鈥檇 ideally like to travel miles a day, but this may not be possible (depending on the spacing of the hotels). If you travel x miles during a day, the penalty for that day is . You want to plan your trip so as to minimize the total penalty- that is, the sum, over all travel days, of the daily penalties.Give an efficient algorithm that determines the optimal sequence of hotels at which to stop
Q11E
Given two strings and , we wish to find the length of their longest common subsequence, that is, the largest k for which there are indices and with . Show how to do this in time .
Q15E
Suppose two teams, A and B, are playing a match to see who is the first to win games (for some particular n). We can suppose that A and B are equally competent, so each has a 50% chance of winning any particular game. Suppose they have already played i+j games, of which A has won i and B has won j. Give an efficient algorithm to compute the probability that A will go on to win the match. For example, if i=n-1 and j=n-3 then the probability that A will win the match is , since it must win any of the next three games.
Q16E
The garage sale problem (courtesy of Professor Lofti Zadeh). On a given Sunday morning, there are n garage sales going on, . For each garage sale , you have an estimate of its value to you, . For any two garage sales you have an estimate of the transportation cost of getting from to . You are also given the costs and of going between your home and each garage sale. You want to find a tour of a subset of the given garage sales, starting and ending at home, that maximizes your total benefit minus your total transportation costs. Give an algorithm that solves this problem in time .
(Hint: This is closely related to the traveling salesman problem.)
Q29E
Exon chaining.Each gene corresponds to a subregion of the overall genome (the DNA sequence); however, part of this region might be 鈥渏unk DNA.鈥 Frequently, a gene consists of several pieces called exons, which are separated by junk fragments called introns. This complicates the process of identifying genes in a newly sequenced genome.
Suppose we have a new DNA sequence and we want to check whether a certain gene (a string) is present in it. Because we cannot hope that the gene will be a contiguous subsequence, we look for partial matches鈥攆ragments of the DNA that are also present in the gene (actually, even these partial matches will be approximate, not perfect). We then attempt to assemble these fragments.
Let x denote the DNA sequence. Each partial match can be represented by a triple , where is the fragment and is a weight
representing the strength of the match (it might be a local alignment score or some other statistical quantity). Many of these potential matches could be false, so the goal is to find a subset of the triples that are consistent (nonoverlapping) and have a maximum total weight.
Show how to do this efficiently.