A fortune-seeker in the 1800s wants to travel from the east coast to the west, crossing the country stage by stage in a stagecoach. At each stop he can choose which town to head for next, and each leg carries a different price — a proxy, the story goes, for the life-insurance premium on that dangerous stretch. There are far too many complete routes to price one by one. Yet there is a way to find the cheapest crossing that examines each town only once. That method is dynamic programming — Richard Bellman's great idea for tearing a hard sequential decision into a chain of trivial ones.
Dynamic programming (DP) applies whenever a decision breaks naturally into a sequence of steps. Three words carry the whole method:
The crucial economy is that the state summarises history. Once the traveller knows he is in a particular town, it does not matter one bit how he got there — the cheapest way onward is the same. All the roads that led to this town collapse into a single number: the best cost still to come.
Define the value function
In words: the best from here is the cheapest immediate step, plus the best from wherever that step
takes you. This is the
The recursion tells us to work backwards. Start at the destination, where the cost to finish is zero. Step back one stage: for each town in the last stage the value is just the price of the final leg. Step back again, applying the Bellman relation with the values we already computed. Keep going until we reach the start. Because each stage only ever consults the next stage's answers, we solve the whole network in a single sweep.
Here is the stagecoach network — five stages, the towns as nodes, each edge labelled with its cost. Step through to reveal the cheapest crossing:
Working back: from the two stage-4 towns the finishing costs are 3 and 4. The stage-3 values come out
as
A network with
Richard Bellman chose the words partly as camouflage. Working at RAND in the 1950s under a defence secretary who "had a pathological fear of the word research," Bellman needed a name for his multi-stage decision method that sounded impressive but harmless. "Programming" meant planning (as in linear programming), and "dynamic" hinted at time and motion while being, in his words, "something not even a Congressman could object to." The mathematics is far more memorable than the marketing: one recursion that dissolves whole classes of sequential optimisation.
DP only works if the state is Markovian: the value of continuing from a state must depend on that state alone, not on the path taken to reach it. If the cost of the next leg secretly depended on which towns you had already visited (say a loyalty discount, or a fuel level that accumulates), then "which town am I in" is no longer enough — the true state must also record the extra information. Choosing a state that is complete but no larger than necessary is the real art of dynamic programming. Too little and the recursion is simply wrong; too much and the state space explodes.