Problem Set 3
Which of the following is true for our dynamic programming algorithm for computing a maximum-weight independent set of a path graph? (Assume there are no ties.)
- As long as the input graph has at least two vertices, the algorithm never selects the minimum-weight vertex.
- The algorithm always selects the maximum-weight vertex.
- If a vertex is excluded from the optimal solution of two consecutive subproblems, then it is excluded from the optimal solutions of all bigger subproblems.
- If a vertex is excluded from the optimal solution of a subproblem, then it is excluded from the optimal solutions of all bigger subproblems.
ANSWER: Consider the path
Consider the path
Consider the path
Option 3 is correct. This follows from induction, since the optimal solution to a subproblem depends only on the solutions of the previous two subproblems.
Recall our dynamic programming algorithm for computing the maximum-weight independent set of a path graph. Consider the following proposed extension to more general graphs. Consider an undirected graph with positive vertex weights. For a vertex
, obtain the graph by deleting and its incident edges from , and obtain the graph from by deleting , its neighbors, and all of the corresponding incident edges from . Let denote the value of a maximum-weight independent set of a graph . Consider the formula , where is an arbitrary vertex of of weight . Which of the following statements is true?
- The formula is always correct in trees, and it leads to an efficient dynamic programming algorithm.
- The formula is always correct in trees, but does not lead to an efficient dynamic programming algorithm.
- The formula is correct in path graphs but is not always correct in trees.
- The formula is always correct in general graphs, and it leads to an efficient dynamic programming algorithm.
ANSWER: Clearly, both
Consider a variation of the Knapsack problem where we have two knapsacks, with integer capacities
and . As usual, we are given items with positive values and positive integer weights. We want to pick subsets with maximum total value (i.e., ) such that the total weights of and are at most and , respectively. Assume that every item fits in either knapsack (i.e., for every item ). Consider the following two algorithmic approaches. (1) Use the algorithm from lecture to pick a max-value feasible solution for the first knapsack, and then run it again on the remaining items to pick a max-value feasible solution for the second knapsack. (2) Use the algorithm from lecture to pick a max-value feasible solution for a knapsack with capacity , and then split the chosen items into two sets that have size at most and , respectively. Which of the following statements is true?
- Algorithm (1) is guaranteed to produce an optimal feasible solution to the original problem provided
. - Algorithm (1) is guaranteed to produce an optimal feasible solution to the original problem but algorithm (2) is not.
- Algorithm (2) is guaranteed to produce an optimal feasible solution to the original problem but algorithm (1) is not.
- Neither algorithm is guaranteed to produce an optimal feasible solution to the original problem.
ANSWER: Consider the set of items
Consider the set of items
Consider the set of items
Option 4 is correct based on the above counterexamples.
A correct algorithm would consider both sacks, and the following three cases:
- Item is put into sack 1.
- Item is put into sack 2.
- Item is not put into any sack.
The recurrence, therefore, is:
where
Recall the dynamic programming algorithms from lecture for the Knapsack and sequence alignment problems. Both fill in a two-dimensional table using a double-for loop. Suppose we reverse the order of the two for loops. (I.e., cut and paste the second for loop in front of the first for loop, without otherwise changing the text in any way.)
Are the resulting algorithms still well defined and correct?
- Neither algorithm remains well defined and correct after reversing the order of the for loops.
- The Knapsack algorithm remains well defined and correct after reversing the order of the for loops, but the sequence alignment algorithm does not.
- The sequence alignment algorithm remains well defined and correct after reversing the order of the for loops, but the Knapsack algorithm does not.
- Both algorithms remain well defined and correct after reversing the order of the for loops.
ANSWER: The recurrence for the knapsack problem is
Consider an instance of the optimal binary search tree problem with 7 keys (say 1,2,3,4,5,6,7 in sorted order) and frequencies
. What is the minimum-possible average search time of a binary search tree with these keys?
- 2.08
- 2.42
- 2.9
- 2.18
ANSWER: Option 4 is correct. See the code on GitHub.
The following problems all take as input two strings
and , of length and , over some alphabet . Which of them can be solved in time? [Check all that apply.]
- Compute the length of a longest common substring of
and . (A substring is a consecutive subsequence of a string. So “bcd” is a substring of “abcdef”, whereas “bdf” is not.) - Compute the length of a longest common subsequence of
and . (Recall a subsequence need not be consecutive. For example, the longest common subsequence of “abcdef” and “afebcd” is “abcd”.) - Assume that
and have the same length . Does there exist a permutation , mapping each to a distinct , such that for every ? - Consider the following variation of sequence alignment. Instead of a single gap penalty
, you’re given two numbers and . The penalty of inserting gaps in a row is now defined as , rather than . Other penalties (for matching two non-gaps) are defined as before. The goal is to compute the minimum-possible penalty of an alignment under this new cost model.
ANSWER: Let
To find the longest common substring, we can either keep track of the maximum positive value while populating the array, or find it later by scanning each element (yikes!). Once found, follow the usual reconstruction procedure as explained in the lectures.
Clearly, this can be solved in
The longest subsequence problem is similar to the longest substring, except for a no match, we pick up the match value so far. The recurrence can be written as:
Clearly, this can be solved in
Problem in option 3 can be solved in
Problem in option 4 is a variation of the original sequence alignment dynamic program. With each subproblem, we need to keep track of what gaps we insert, since the costs incurred at the current position depend on whether or not the previous subproblems inserted gaps. Blows up the number of subproblems and running time by a constant factor, but still can be solved in
Recall our dynamic programming algorithms for maximum-weight independent set, sequence alignment, and optimal binary search trees. The space requirements of these algorithms are proportional to the number of subproblems that get solved:
(where is the number of vertices), (where are the lengths of the two strings), and (where is the number of keys), respectively.
Suppose we only want to compute the value of an optimal solution (the final answer of the first, forward pass) and don’t care about actually reconstructing an optimal solution (i.e., we skip the second, reverse pass over the table). How much space do you then really need to run each of three algorithms?
ANSWER: The MWIS problem only ever looks at the last two columns (
The sequence alignment problem may need to look at the previous row (
Unfortunately, the optional BST problems needs to keep track of
Thus, option 1 is correct.
Leave a comment