Memra

Elements of DP & proving optimal substructure

◈ 4 cards

The cut-and-paste proof, the subproblem-graph running-time view, and a cautionary non-example (longest simple path).

Proving optimal substructure: cut-and-paste

Before you trust a DP recurrence you must prove the problem has optimal substructure. The standard tool is the cut-and-paste argument (a proof by contradiction):

> Suppose an optimal solution to the whole problem uses a suboptimal solution to some subproblem. "Cut out" and "paste in" the optimal subproblem solution . Because is at least as good as , the resulting whole solution is at least as good as — and the subproblem is independent, so the paste is legal. This contradicts being optimal. Therefore the subproblem solution inside was optimal.

Rod cutting, concretely. If the optimal cut of a length- rod left a length- remainder cut suboptimally, replacing that remainder's cut with the optimal one raises total revenue — contradiction. Hence the remainder is cut optimally, which is exactly what the recurrence assumes.

Two dials of optimal substructure

Problems differ along two axes, and their product estimates the running time:

  • How many subproblems an optimal solution uses: rod cutting uses 1 (the remainder); matrix-chain uses 2 (left and right of a split).
  • How many choices you weigh: rod cutting has (first-cut position); matrix-chain has (split point); LCS has .

Running time (\#subproblems) (\#choices). Rod: . Matrix-chain: . LCS: .

The subproblem graph makes it precise

Make a directed graph: a vertex per distinct subproblem, an edge for each dependency (each choice that points to a smaller subproblem). Then the bottom-up running time is — and the correct bottom-up fill order is a reverse topological sort of this graph (solve the things-you-depend-on first). Rod cutting: vertices, edges .

A non-example: longest simple path

Not every problem has optimal substructure. Shortest path does: if is shortest, then and are each shortest, and they share no vertices but — the subproblems are independent.

Longest simple path does not. Consider with extra edges. The longest simple might be , yet alone is not the longest simple -to- path (a longer one detours through other vertices). The subpaths compete for the same vertices — they are not independent — so you cannot glue independently optimal subpaths. (Indeed, longest simple path is NP-complete; no polynomial DP is known.)

The lesson: optimal substructure secretly requires independent subproblems. Always check independence before writing the recurrence.

D&CDPGreedySubproblemsdisjointoverlappingone, smallerOptimalsubstructurenot neededrequiredrequiredTable pays offnoyesnoChoice madeno choiceafter subproblemsbefore subproblemsOnly DP exploits overlap; only greedy chooses first.
The three paradigms on the axes that actually separate them. Divide-and-conquer recurses on disjoint pieces, so a table buys nothing. DP and greedy both need optimal substructure; the difference is that DP makes its choice after solving the subproblems, while greedy commits before.
qrtsq to r is not a longest q-to-r path.
The non-example, drawn. A longest simple path q to t is q → r → t, yet its subpath q → r is not a longest q-to-r path — the dashed route q → s → t → r is longer. The two candidates fight over the vertices r and t, so the subpaths are not independent and cannot be glued. No optimal substructure, no DP.
NORMAL ~/memra/learn/comp-372/elements-of-dp-optimal-substructure utf-8 LF