Worked examples — Dynamic programming (value - policy iteration)
You have seen the machinery of MDPs, the Bellman equations, and the two big algorithms (value iteration and policy iteration) in the parent note Dynamic Programming. This page does one thing: it runs the numbers, by hand, for every kind of situation the topic can throw at you.
We will not assume you remember any formula. Each example re-states the one line of maths it needs, in words, before touching a number.
The one equation everything uses
Before the matrix, let us pin the single computation that every example below performs. It is the Bellman backup — one update of a state's value. But first we must earn one symbol.
Two backups differ only in the last step:
Here ("sum over actions") means add up over every action, ("sum over next states") means add up over every possible landing spot weighted by its probability, means take the largest over actions, and means return which action gave that largest value.
The scenario matrix
Every DP exercise is one of these cells. The examples that follow are labelled with the cell they cover, and together they hit all of them.
| # | Case class | What makes it tricky | Example |
|---|---|---|---|
| A | Single deterministic backup | just plug in | Ex 1 |
| B | Terminal / degenerate state | , backup must not loop | Ex 1, Ex 4 |
| C | Stochastic transition | must average over with probabilities | Ex 2 |
| D | Full policy evaluation to convergence | fixed point of a linear system | Ex 3 |
| E | Value iteration, several sweeps | values propagate outward from goal | Ex 4 |
| F | Policy improvement flips an action | greedy changes | Ex 5 |
| G | Limiting case and | myopic vs. far-sighted | Ex 6 |
| H | Real-world word problem | translate story → MDP → numbers | Ex 7 |
| I | Exam twist: value iteration = policy iteration with 1 sweep | conceptual + numeric | Ex 8 |
The picture below makes Case C (the stochastic backup) concrete before any numbers appear: from state a single action can land you in two different next states, so the backup must average over them (the two cyan arrows, weighted by probability) before adding the discounted future. It is exactly the geometry Example 2 will compute. Look at the two cyan arrows leaving — those are the two random landing spots; the amber circles are the destinations you average over.

Example 1 — Cells A, B: one deterministic backup next to the goal
Steps.
- Write the backup for this single action: . Why this step? This is the definition of the value of an action; since the move is deterministic the collapses to one term, so nothing else applies when there is exactly one action and one destination.
- Substitute the terminal value . Why this step? A terminal state ends the episode — there is no future reward to collect, so its value is exactly . If we forgot this, the backup would try to look "past" the goal and loop forever (cell B trap).
- Compute: . Why this step? We simply substitute the numbers into the backup from step 1; the term is , so only the immediate reward survives.
Verify: Landing on the goal in one step should cost exactly one step's reward — and is exactly that. Units: reward, consistent. Since the future term vanished, had no effect here, which is correct (the future is worth nothing when you stop).
Example 2 — Cell C: a stochastic move (average over next states)
Steps.
- Because the outcome is random, we cannot use a single . We must average the destination value weighted by its probability : Why this step? is the environment's dice roll; the expected value of a random destination is the probability-weighted sum. This is the in the Bellman equation made concrete.
- Compute the average: , , so . Why this step? We evaluate each product and add them; this collapses the random destination into a single expected value we can feed to the backup.
- Apply the backup: . Why this step? Same rule — the only change was building the expected next value first.
Verify: The answer sits above the reward-only-plus-worst-case floor () and below the all-good-case ceiling (), and leans toward because of the mass points there. Sanity check holds.
Example 3 — Cell D: full policy evaluation as a tiny linear system
Steps.
- Write the Bellman expectation equation for each state. With a deterministic policy the and the each collapse to a single term: Why this step? Policy evaluation is nothing but "each state's value equals its own backup." With two states you get two simultaneous equations.
- By symmetry set . Then . Why this step? The setup is symmetric ( and play identical roles), so their values must be equal — this shortcuts two equations into one.
- Solve: . Why this step? We collect the terms on one side and divide, the standard way to isolate the unknown in a linear equation.
Verify: Plug back: . ✓ It is the fixed point. The value is finite () despite the infinite loop precisely because makes the geometric series converge. This is the contraction-mapping guarantee in action.
Example 4 — Cells B, E: value iteration, watch values propagate

Steps.
- Initialise everything to : , fixed. Why this step? Value iteration starts from any guess; zeros are simplest, and never changes because has no actions to back up through.
- Sweep 1, using with (every move is deterministic, so no needed):
- .
- .
- . Why this step? Each square keeps its best neighbour value. After one sweep only has real goal information; the others are still guessing .
- Sweep 2 (now from sweep 1):
- (stable).
- .
- . Why this step? The corrected value at from sweep 1 now flows one square left into and .
- Sweep 3 — re-back-up all three squares (we never skip states; a state is only "done" when its backup returns the same number):
- (unchanged — already at its true value, so it stays put).
- (unchanged — also settled).
- . Why this step? We do not update only by hand-waving; we re-run the backup for every state and observe that return the same values they already had (they have converged), while is the only one whose value still improves. Correct information marches one square left per sweep — so the farthest square is the last to settle.
Verify: The true cost from is 3 right-steps to : . Value iteration reached . ✓ And notice it took 3 sweeps for the far square to learn — one per step of distance (cell E behaviour confirmed).
Example 5 — Cell F: policy improvement flips an action
Steps.
- Policy improvement makes the policy greedy: . Why this step? The policy improvement theorem says switching to the best-looking action can only help (or leave you equal), never hurt.
- Compare the two numbers: (right) vs. (up). The larger (closer to zero) is . Why this step? "Best" means highest value; among negative numbers the one nearest zero is the largest, so this comparison decides the .
- So — the policy flips from "up" to "right". Why this step? The greedy rule returns whichever action won step 2, and here that action ("right") differs from the old choice ("up"), so the policy genuinely changes.
Verify: New value under the flip is at least the old: . The value strictly increased, so this was a genuine improvement — exactly what the theorem promises. If instead both actions had tied, the policy would be left unchanged and we'd have hit optimality.
Example 6 — Cell G: the two limits of
Steps.
- Case (totally myopic): ; . Greedy wins. Why this step? zeroes out all future terms, so the agent only sees the immediate reward. The future worth is invisible.
- Case (far-sighted): ; . Patient wins. Why this step? A near- discount barely shrinks the future, so the payoff dominates the small .
- Find the tipping point. Set the two action-values equal and solve for : . Why this step? The winner flips exactly where the two -values cross; solving locates that boundary so we know which chooses which action, not just the two endpoints.
Verify: Below the tipping point patience is not yet worth it, above it patience wins. Check the endpoints against : at () the answer was greedy ✓; at () the answer was patient ✓. Both limits sit on the correct side of the boundary, so the tipping-point calculation is consistent. Intuition holds: is "live for today", is "plan for the long run". This same discount idea reappears in TD learning and Q-learning.
Example 7 — Cell H: a word problem (inventory)
Steps.
- Translate the story into and :
- restock: , next-day value ;
- skip: , next-day value . Why this step? Every word problem must first become numbers for the machine — "cost" is a negative reward, "goodwill/earnings" is the destination value.
- Backup each: ; . Why this step? Standard optimality backup on the two actions.
- Take the max: , so restock. Why this step? Value iteration keeps the largest action-value, and beats .
Verify: Restock nets vs. skip's . The up-front is more than repaid by the discounted future : net , which equals ✓. Sensible: the discount didn't kill the future enough to make skipping worthwhile.
Example 8 — Cell I: value iteration = policy iteration with one evaluation sweep
Steps.
- One value-iteration sweep (each state keeps its best action — here only one action exists per state): Why this step? Value iteration does a single -backup per state per sweep — it does not wait for full convergence.
- Now do the policy-iteration equivalent. Policy improvement: for each state the greedy has only one candidate action, so the policy stays ", " — unchanged. Why this step? With one action per state there is nothing to switch to; improvement is a no-op here, which isolates the evaluation part for comparison.
- One-sweep policy evaluation of that policy, starting from : Why this step? A single evaluation sweep applies the same backup — and produces the identical numbers as step 1.
- Conclusion: the two procedures gave the same after one sweep. Value iteration is policy iteration whose evaluation is truncated to a single sweep. Keep iterating either one and you re-derive the fixed point from Example 3. Why this step? Matching the numbers line-for-line is the whole point of the exam twist: the algorithms coincide when policy evaluation is cut to one step.
Verify: After 1 sweep both give ✓ (not yet the fixed point , so it only approaches — forecast confirmed). Running value iteration to convergence returns , checked in Example 3. This truncation idea is what makes model-free methods like SARSA and policy gradients practical.
Recall Quick self-test
One deterministic step to a terminal goal, , : what is ? ::: (future term vanishes since ). Ping-pong loop with each step, : value of each state? ::: , from . Slippery jump , , , : ? ::: . How is value iteration related to policy iteration? ::: Policy iteration with policy evaluation truncated to one sweep. makes the agent behave how? ::: Myopic — only immediate reward matters. When is safe? ::: Only with an absorbing terminal state, so every path ends and totals stay finite.