Suppose a workshop sells a length of material in smaller pieces. A piece of length 1 earns 2 units, length 2 earns 5, and length 3 earns 7. What is the most it can earn from a length of 3? Selling the whole piece earns 7. Cutting it into lengths 1 and 2 earns 2 + 5 = 7. Three pieces of length 1 earn 6. For a longer length, there are many more choices.
Background for this page. You should be able to follow loops and list indexing. A recurrence expresses an answer using smaller answers. An optimum is the best feasible answer under a stated objective. Related explanations: Divide and conquer: merge sort and recurrences · Summations for algorithm analysis.
The useful observation is that after choosing the first cut, the remaining piece is the same kind of problem at a smaller length. Let R(n) be the best revenue for length n, with R(0) = 0. If P(i) is the price of a piece of length i, then
R(n) = max₁≤ᵢ≤ₙ (P(i) + R(n − i)).
This is a recurrence for an optimal answer. It works because any optimal plan for the whole piece must use an optimal plan for its remaining piece. If the remainder were not optimal, replacing it with a better one would improve the whole plan. This is called optimal substructure.
A plain recursive program may solve the same remaining length many times. Dynamic programming saves those answers. We can store results as we find them (memoization) or fill a table from small lengths upward (tabulation). The table makes the dependency order visible:
# Find the best revenue for every length up to total.
def best_revenue(prices, total):
# Length zero earns nothing.
revenue = [0] * (total + 1)
for length in range(1, total + 1):
best = float('-inf')
# Try every possible first piece.
for first in range(1, length + 1):
candidate = prices[first] + revenue[length - first]
best = max(best, candidate)
revenue[length] = best
return revenue[total]
This assumes prices has an entry for every length from 1 through total. There are n table entries and up to n choices per entry, so the time is O(n²) and the table uses O(n) space. If some lengths have no price, we must change the input rule and skip those choices. The recurrence is only as good as its assumptions.
The same pattern appears in other forms. Matrix-chain multiplication chooses where to put parentheses so multiplying a chain of matrices uses the fewest scalar operations; the final matrix is the same, but the work can differ greatly. Longest common subsequence compares prefixes of two sequences and reuses answers for smaller prefixes. An optimal binary search tree uses known search frequencies to choose a tree with low expected search cost. Each problem has its own state and recurrence. The method is not “make a table”; it is define the smaller questions precisely and prove that their answers combine correctly.
Recover the choices, not just their value
For prices 2, 5, and 7 at lengths one through three, the best revenues are 0, 2, 5, and 7. To find an actual cutting plan, store the first-piece length whenever a candidate improves the best revenue. For total length three, a tie policy might keep the first candidate: first piece one, followed by the saved plan for length two. That gives lengths one and two, earning seven. Choosing the whole length-three piece also earns seven.
The revenue table alone proves an achievable value, but does not directly report which cuts produced it. Storing choices makes reconstruction explicit. If the recurrence uses a smaller answer before it has been computed, tabulation fails; dependency order is part of the algorithm.
Practice. Prices for lengths 1, 2, and 3 are 2, 5, and 7. Find R(3) using the recurrence. Why can a greedy choice of the highest price per piece fail on other price lists?
Answer. The first-piece choices give 2 + R(2) = 7, 5 + R(1) = 7, and 7 + R(0) = 7, so R(3) = 7. A locally attractive piece may leave a remainder with poor choices; the recurrence checks complete plans.
Try another case. Can a table with every entry initially zero handle prices that are all negative when the entire material must be sold?
Reasoning. Only if candidate maxima start below all valid revenues. Starting each best value at zero would incorrectly allow selling nothing. The example’s negative-infinity initialization avoids that error.