Dynamic programming: solve backward
The question
A cart can go right or down through a warehouse grid. What is the least total time from A to I?
- 1 SeeFill a grid from the finish.
- 2 TryChange a cost and compare routes.
- 3 ProveBuild the Bellman argument.
- 4 ApplySolve routes with new rules.
Before you start · 3 quick checks on sums, minima, and arrows, about 3 min
Two ideas before you start
2 ideasPay for each move
Two branches from a start meet at the finish. Each branch has two moves with their own costs.
Tap a route to add the costs of the moves it uses. In this drawing, arriving at the finish carries no extra charge.
Exact definition
A state records the information used to decide which moves are legal and what they cost. For a cart with fixed aisle times and no rule that depends on its earlier moves, its bay is enough. A directed edge (an arrow) gives a legal move from one state to another.
A path follows a sequence of directed edges. Its cost is the sum of the edge costs plus any charge at the final state. A shortest path is a path of smallest total cost, even when the costs measure time or energy instead of distance.
Two routes can share a tail
Two entrances reach a shared bay, which has one move to the finish.
Switch entrances. The first move changes, but the remaining cost from the shared bay stays the same.
Exact definition
A continuation starts at the current state and ends at a terminal state, where the trip stops. The value of a state is the smallest cost of a legal continuation, with the terminal charge included. Prior costs contribute to the cost of the full trip. They are outside the continuation cost.
Two histories can share a value calculation when they arrive at the same state at the same stage and admit the same future moves, transitions, and costs. If an earlier move changes whether a later door can be opened, the bay alone fails to capture this information. Record the door access in the state.
Fine print on stages and terminal costs
A finite horizon means there is a fixed last stage. In a right-and-down grid, group bays by how many moves it takes to reach them from the start. Each legal move advances one stage. An arrival charge is paid at the terminal state, so it belongs to the continuation even when there are no moves left.
Working backward means calculating values in reverse stage order. The cart still follows the arrows forward when it drives.
Fill in the warehouse grid
- Start at bay I in the bottom right. Enter its remaining time. There is no arrival charge.
- Work toward A. For each arrow out of a bay, add its time to the value at the next bay. Enter the smallest sum, in whole seconds.
- Press Check cells to mark what you entered. The route appears once every bay is correct.
Where can I work next?
After the finish, look for bays whose outgoing arrows lead only to bays you have filled. If one of the arrows points at a blank, the full cost of that choice is still unknown. A filled bay already holds the cheapest time from there to I, so you can reuse it for every arrow that ends at that bay.
The time in a bay is measured from that bay all the way to I. Use the arrow labels to find the time for an individual move. If two choices of next bay tie, either can be used to trace a cheapest path.
Change the time near the finish
Answer the prediction to open the explorer.
Compare both routes
- Move the slider to change the upper route’s final leg. The first legs and the entire lower route stay fixed.
- Read across each row to see where its total comes from. Try each goal with the same pair of routes.
| Route | First leg | Final leg | Total |
|---|
Which entries need to change?
The terminal value is fixed. Changing the upper final leg changes the value at the upper branch. The lower branch has no path that uses the changed leg, so its continuation value stays fixed. Recompare the choices at the fork. The best cost there can stay fixed even while one of the candidates changes.
In a larger network, an edit can affect states from which the edited edge can be reached. Recompute in reverse stage order within that part of the network. Even there, a value may stay the same if another route still ties or wins.
Why the backward calculation works
Assumptions. Stages are numbered by whole numbers \(t\) from \(0\) through the last stage \(T\). Each stage has finitely many states. At each state \(x\) before \(T\), the set \(A_t(x)\) of allowed moves is finite and nonempty. Each move \(a\) has a fixed real cost \(c_t(x,a)\) and a known next state \(y_a\) at stage \(t+1\). These depend only on the current stage, state, and move. At \(T\) the trip ends and incurs the fixed real terminal cost \(G(x)\). Total cost adds the move costs and this terminal cost.
Conclusion. Write \(V_t(x)\) for the minimum cost from \(x\) at stage \(t\). Then \(V_T(x)=G(x)\). For \(t \lt T\),
\[\begin{aligned} V_t(x)&=\min_{a\in A_t(x)} Q(a),\\ Q(a)&=c_t(x,a)+V_{t+1}(y_a). \end{aligned}\]Here \(Q(a)\) is the candidate cost for the fixed \(t\) and \(x\). Taking any minimizing move at each state gives a path that attains \(V_t(x)\). Zero or negative costs are allowed since every move advances a stage and all paths finish in finitely many moves.
Pick the true next line under the assumptions above. At a nonterminal state, use the values from the next stage.
What the calculation stores
Store a value and one minimizing move at each nonterminal state. Later, recover a shortest route by following those moves. Storing every minimizing move lets you recover other shortest routes when there are ties. A rule that tells you what to do at each stage and state is called a policy. A single recovered route starts at one chosen state, while the policy also gives instructions for other starting points.
With the layers already given, each state needs one scan of its outgoing moves. The work thus grows in proportion to the number of states plus the number of edges, when reading and adding a cost takes constant time. Listing complete routes can take much more work because many complete routes can reuse the same states.
Problems
Draw the stages on paper as you work. Each problem has hints and a worked solution.
Further reading
- R. Bellman, Dynamic Programming.
- D. P. Bertsekas, Dynamic Programming and Optimal Control, Volume I.