Dynamic programming problems [00QM]
- October 27, 2026
Consulting business [00QN]
- October 27, 2026
The problems in this section all refer to the following scenario.
Scenario [00QO]
- October 27, 2026
You are running a consulting business, with clients on both east and west coasts. Each month, you can run your business from an office in either New York or San Fransisco. In month i, you will incur an operating cost of N_i if you run the business out of New York; you will incur an operating cost of S_i if you run the business out of San Fransisco (the costs can change each month depending on the distribution of client demands).
However, if you run the business out of one city in month i, and then out of the other city in month i+1, then you incur a fixed moving cost of M to switch cities.
Given a sequence of n months, a plan is a sequence of n locations—each one equal to either \mathsf {NY} or \mathsf {SF}—such that the ith location indicates the city in which you will be based in the ith month. The cost of a plan is the sum of the operating costs for each of the n months, plus a moving cost of M for each time you switch cities. The plan can begin in either city.
Given a value for the moving cost M, and sequences of operating costs N_1, \dots , N_n and S_1, \dots , S_n, the problem is to find a plan with minimum total cost.
[DP1] Greedy failure (1 pt) [00QP]
- October 27, 2026
Show that the following greedy algorithm does not correctly solve this problem, by describing inputs for which it does not return the correct answer.
In your example, be sure to explain what the correct answer is and also what the above algorithm finds.
[DP2] Consulting recurrence (1 pt) [00QQ]
- October 27, 2026
Let C(i,c) denote the minimum cost possible if you operate your business for only months 1 through i and end in city c. For example, C(3,\mathsf {NY}) denotes the minimum possible cost for the first three months (disregarding any following months) that end by operating out of New York in month 3.
Define C via a recurrence.
[DP3] Consulting algorithm (1 pt) [00QR]
- October 27, 2026
Describe an efficient algorithm that takes values for n, M, and sequences of operating costs N_1, \dots , N_n and S_1, \dots , S_n, and returns the cost of an optimal plan. Justify the correctness of your algorithm and analyze its asymptotic running time.
[DP4] Train track (2 pts) [00QS]
- October 27, 2026
You are building a toy train track out of a sequence of straight pieces laid end-to-end. Some pieces are one unit long, and some are three units long. How many different ways are there to build a track that is five hundred units long?
For example, there are 9 different ways to build a track that is 7 units long, as illustrated below.
Hint.
- October 27, 2026
Let T(n) denote the number of ways to build a track of length n, and come up with a recurrence for T. What are your choices for the final track segment?
Hint.
- October 27, 2026
To answer this you will need to write a small program. If you use Java, be sure to use BigInteger.
[DP5] Longest path in an ordered graph (3 pts) [00QT]
- October 27, 2026
Let G = (V,E) be a directed, unweighted graph with nodes v_1, \dots , v_n. We say that G is an ordered graph if it has the following properties:
- Each edge goes from a node with a lower index to a node with a higher index. That is, every directed edge has the form (v_i, v_j) with i < j.
- Every node other than v_n has at least one outgoing edge.
Given an ordered graph G, we want to find the longest path from v_1 to v_n.
Consider the following greedy algorithm.
Show that this algorithm does not correctly solve the problem, by giving an example of an ordered graph for which it does not return the correct answer. Be sure to explain what the correct answer is and what incorrect answer is returned by the algorithm.w \gets v_1L \gets 0\mathsf {while} w \neq v_n:Choose the outgoing edge (w, v_j) with the smallest jw \gets v_jIncrement L\mathsf {return} L- Describe an efficient algorithm that takes an ordered graph G and returns the length of the longest path from v_1 to v_n. Justify its correctness and analyze its time complexity.
Hint.
- October 27, 2026
If you know the length of the longest path from v_j to v_n for every j > i, can you figure out the length of the longest path from v_i to v_n?
[DP6] Closest subset without going over (3 pts) [00R3]
- November 2, 2026
This problem is similar to one we did in class, but slightly more general. Suppose we have a set \{x_1, x_2, \dots , x_n\} of n positive integers, and a target sum S, which is a positive integer. In class, we considered finding a subset of \{x_1, \dots , x_n\} whose sum is exactly equal to S. Instead, let’s now consider finding a subset whose sum is as close to S as possible without going over; that is, the subset with the largest possible sum \leq S. For example, given the set \{1,2,7,12\} and the target sum 18, there is no subset which sums exactly to 18, but the one which comes closest without going over is \{12,2,1\} which sums to 15. Note that \{12,7\}, with a sum of 19, is closer to the target in an absolute sense, but it is greater than the target; we are interested in the biggest sum which is \leq the target.
- Describe a simple brute-force algorithm for solving this problem. What is the running time of the algorithm?
- Using dynamic programming, describe an algorithm with running time \Theta (nS). Be sure that you explain how to find not only the maximum possible sum, but also the actual subset which has that sum. Justify the correctness of your algorithm.
- This is known as a psuedopolynomial-time algorithm: the running time is a polynomial in the value of S, but actually exponential in terms of the size of the input (i.e. the number of bits needed to represent S). Give one example of a set of inputs for which your dynamic programming solution would be faster, and one example of a set of inputs for which the brute force algorithm would be faster.
[DP7] Making change (3 pts) [00R4]
- November 2, 2026
Consider the problem of making change for C cents using the fewest possible number of coins. Assume that each coin’s value is an integer.
- Describe a greedy algorithm to make change for C cents using US quarters, dimes, nickels, and pennies. (It turns out that the greedy algorithm is actually optimal for US coins (and most real-world coin systems), though coming up with a proof of this fact is nontrivial.)
- Give a set of coin denominations for which the greedy algorithm does not yield an optimal solution. Your set should include a penny so that there is a solution for every value of C.
- Design and analyze an algorithm to make change using the fewest number of coins that works for any set of coins. That is, as input your algorithm should take:
- n, the number of different coin types;
- a list c_1, c_2, …, c_n giving the values of the different coins (you may assume they are already sorted from smallest to largest); and
- the number of cents C we would like to make change for.
As output your algorithm should either report that it is not possible to make the required amount C using the given coins, or give a multiset (i.e. a set that allows duplicate elements) of coins which add up to C such that the number of coins in the multiset is as small as possible. For example, if given as input c_1 = 1, c_2 = 5, c_3 = 20 and the target value C = 47, your algorithm should output \{20, 20, 5, 1, 1\}. Note that we assume there is an unlimited supply of coins of each type. Be sure to justify your algorithm’s correctness and analyze its time complexity.