Sub topic 3
Optimal substructure: building answers from sub-answers
This chapter assumes you can write recursive Python functions with base cases and that you have seen how naive recursion repeats work. The previous chapter covered overlapping subproblems. This chapter covers the second property a problem needs before dynamic programming applies: optimal substructure.
What the property says
A problem has optimal substructure when an optimal solution to the whole problem contains optimal solutions to its subproblems. This lets you build the best answer from the best answers to smaller pieces, so you never have to look at worse sub-answers.
Overlapping subproblems make DP worth doing, because repeated work can be cached. Optimal substructure makes DP correct, because the cached sub-answers are the right ones to combine.
A worked example: climbing stairs at minimum cost
You stand below a staircase. cost[i] is the price of leaving step i. From any position you can climb one or two steps, and you may start on step 0 or step 1 for free. You want the cheapest way to reach the top, which is position len(cost).
Suppose the cheapest route to position i arrives from position i - 1. Then the part of that route ending at i - 1 must itself be the cheapest route to i - 1. If a cheaper route to i - 1 existed, you could swap it in and get a cheaper route to i, which contradicts the assumption that the original route was cheapest. This swap argument is called cut-and-paste, and it is the standard way to prove optimal substructure.
The proof gives a rule: the best cost to reach i is the smaller of two options. The first is the best cost to reach i - 1 plus cost[i - 1]. The second is the best cost to reach i - 2 plus cost[i - 2].
def min_cost_to_reach(cost, i):
if i < 2:
return 0 # starting on step 0 or 1 is free
from_one_below = min_cost_to_reach(cost, i - 1) + cost[i - 1]
from_two_below = min_cost_to_reach(cost, i - 2) + cost[i - 2]
return min(from_one_below, from_two_below)
cost = [10, 15, 20]
print(min_cost_to_reach(cost, len(cost))) # 15The function only ever combines optimal sub-answers. It never asks for the second-best way to reach a step, because optimal substructure guarantees it will never need one.
When the property fails
Some problems that look similar do not have optimal substructure. A classic case is the longest simple path in a graph, where a simple path is one that never repeats a vertex.
Take a cycle of four vertices: q–r, r–t, t–s, s–q. The longest simple path from q to t is q → r → t or q → s → t. Neither is longer, since a longer walk would revisit a vertex. Now look at the piece q → r inside the first path. The longest simple path from q to r on its own is q → s → t → r. That path uses t, so you cannot extend it to t without repeating a vertex.
The subproblems interfere with each other because they compete for the same vertices. That interference breaks the cut-and-paste argument. Shortest paths do not have this problem: a shortest path never benefits from revisiting a vertex, so its pieces are independent.
| Problem | Optimal substructure? | Why |
|---|---|---|
| Minimum-cost stairs | Yes | Swapping in a cheaper prefix lowers the total |
| Shortest path (non-negative weights) | Yes | Subpaths of shortest paths are shortest |
| Longest simple path | No | Sub-answers can share vertices and conflict |
Optimal substructure is not greedy choice
Greedy algorithms need optimal substructure and also need a "safe" first choice that is always part of some optimal answer. DP needs only optimal substructure. It tries every choice at each step and keeps the best one.
Coin change with coins [1, 3, 4] and amount 6 shows the difference. Greedy takes 4, then 1, then 1, for three coins. The optimal answer is 3 + 3, two coins. The problem still has optimal substructure: after the first coin, the rest must be an optimal way to make the remaining amount. Greedy fails because it commits to one first coin without comparing it against the others.
Exercise
In the stairs problem, a new rule forbids taking two one-step moves in a row. Does "best cost to reach position i" still have optimal substructure on its own? If not, what extra information fixes it?
Show answer
No. The best route to i - 1 might end with a one-step move, and then you could not take another one-step move to reach i. The sub-answer no longer contains enough information. Adding the last move type to the subproblem, so it becomes "best cost to reach i with the last move being one step or two steps", restores optimal substructure.
Exercise
With coins [1, 3, 4] and amount 6, list the three subproblems the DP compares after choosing the first coin, and give the optimal coin count for each.
Show answer
After choosing 1, the remaining amount is 5, which needs 2 coins (4 + 1). After choosing 3, the remaining amount is 3, which needs 1 coin. After choosing 4, the remaining amount is 2, which needs 2 coins (1 + 1). The best choice is 3 followed by 1 more coin, for a total of 2.