# 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 . The vertices selected by the algorithm are , where , the minimum, is selected. Thus, option 1 is incorrect.

Consider the path . The vertices selected by the algorithm are , where the maximum isn’t selected. Thus, option 2 is incorrect.

Consider the path . The vertices selected by the algorithm are . However, was not included in the optional solution of the subproblem . Note that, was not included in the optional solution of the subproblem either, because the its previous vertex was “heavier”, and since all weights are positive, the sum of the next weight and the heavier vertex is certainly greater than . Thus, option 4 is incorrect.

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 and are trees if is a tree. If there was no cycle in , there’re none in and either. Also, removing an edge and all its incident edges preserves connectivity among the remaining vertices. Thus, and exhibit optimal substructures, and hence the algorithm is correct for trees. However, we can’t prove optimal substructure property for arbitrary general graphs.
Thus, option 1 is correct, and the rest are not. The time complexity is ; see MWIS in a Tree - Chen + Kuo + Sheu].

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 and two sacks of equal capacities . Algorithm 1 picks and in the first iteration, and in the second ( is not picked). Total value of both sacks . However, the optimal solution would put and in one sack, and and in the other, bringing the total value of both sacks to . Thus, option 1 is incorrect.

Consider the set of items and two sacks of capacities and , respectively. Algorithm 1 picks and in the first iteration, and nothing in the second iteration. Total value of both sacks . However, the optimal solution would put and in one sack, and in the other, bringing the total value of both sacks to . Thus, option 2 is incorrect.

Consider the set of items and two sacks of equal capacities . Algorithm 2 picks , but this set cannot fit into two sacks, so one is dropped when redistributing. Total value of both sacks . However, the optimal solution would put and in one sack, and in the other ( is not picked), bringing the total value of both sacks to . Thus, option 3 is incorrect.

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 is the index corresponding to the items, corresponding to sack 1, and corresponding to sack 2.

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 . What we care about is that the array entries on the R.H.S. have already been populated by the time we attempt to populate . Clearly, the order of the for loops doesn’t matter; the lecture has in the inner loop, but if we have it in the outer, the -th entries for some are still populated before -th entry. Thus, option 4 is correct.

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 denote the prefix of including the character at the i-th position, and denote the prefix of including the character at the j-th position. The characters at the last positions of and either match or don’t; a match increases the length of the longest common substring found so far by one. Thus, if denotes the length of the longest common substring of and , the recurrence can be written as:

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 time. Thus, option 1 is correct.

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 time. Thus, option 2 is correct. Alternatively, this reduces to sequence alignment by setting the gap penalty to 1 and making the penalty of matching two different characters to be very large.

Problem in option 3 can be solved in time, without dynamic programming. We simply count the frequency of each symbol in each string. The permutation exists if and only if every symbol occurs exactly the same number of times in each string.

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 time. Thus, option 4 is correct.

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?

- \(\Theta(1), \Theta(n) \text{ and } \Theta(n^2) \)
- \(\Theta(n), \Theta(mn) \text{ and } \Theta(n^2) \)
- \(\Theta(1), \Theta(n) \text{ and } \Theta(n) \)
- \(\Theta(1), \Theta(1) \text{ and } \Theta(n) \)

**ANSWER:** The MWIS problem only ever looks at the last two columns ( and ), thus, we can get away with storing just those two values.

The sequence alignment problem may need to look at the previous row ( or ), or the previous column in the current row (). Thus, we can only store two rows, which takes linear space.

Unfortunately, the optional BST problems needs to keep track of subproblems, and can’t be solved in reduced space.

Thus, option 1 is correct.