Hexa Scholar

Dynamic programming: solve backward

A gold parcel cart faces two warehouse aisles around a stack of navy crates.
A parcel cart reaches a fork in a warehouse aisle.

The question

A cart can go right or down through a warehouse grid. What is the least total time from A to I?

  1. 1 SeeFill a grid from the finish.
  2. 2 TryChange a cost and compare routes.
  3. 3 ProveBuild the Bellman argument.
  4. 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 ideas

Pay 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.

1 See

Fill in the warehouse grid

Each arrow is one legal move. The number beside it is the travel time in seconds. Enter the remaining time at each bay.

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.

2 Try

Change the time near the finish

Answer the prediction to open the explorer.

3 Prove

Why the backward calculation works

Theorem · Bellman recursion

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.

4 Apply

Problems

Draw the stages on paper as you work. Each problem has hints and a worked solution.

Key point Write down the state and its legal moves before filling the table. Keep a variable in the state when its value changes the choices or costs ahead.
Level Undergraduate Prerequisites Adding costs, comparing totals, and reading directed moves (refreshed above) Time ~30 min

Further reading