2.9 Multi-stage problems and dynamic programming — Practise with help
Warm-up: remember
- On a network, what is the shortest distance from the destination to itself?
- Why does Dijkstra's algorithm settle the nearest unsettled vertex?
Answers and feedback
- 0 — you are already there.
- Because any other route would pass through an unsettled vertex that is at least as far away, and every weight is positive.
Practise with help
1. A network has stages ; ; . Edges: , , , . Find the least cost from to by backward recursion.
Hint
Start at the last stage, , where the cost to the end is 0.
Next step
From : ? No — from the cost to is . From it is .
Full working
Stage 3 (): cost to the end .
Stage 2:
- From : , so cost .
- From : , so cost .
Stage 1 ():
- then : .
- then : .
Least is , along . Here the greedy choice happens to be wrong too: the cheapest first edge is , but the better total is through .
2. For that network, a student says "the cheapest first edge is , so the answer is 9". Explain the mistake.
Hint
What does the cheap first edge lead to?
Next step
is cheap, but from the only way on is .
Full working
The student has made the greedy choice: take the cheapest edge now. But the cheap first edge leads to an expensive continuation. costs 1, and then costs 8, giving 9 in total. The more expensive first edge costs 3, but then costs only 2, giving 5.
The least total is 5, not 9. Dynamic programming avoids this mistake by looking at the cost of the whole remaining journey from each state, not just the next edge.
3. A four-stage network has stages ; ; ; . Edges: , , , , , , , . Find the least total cost from to .
Hint
Work back: is 0, then stage 3 (), then stage 2, then stage 1.
Next step
From : . From : . Then from : .
Full working
Stage 4 (): .
Stage 3:
- : , cost .
- : , cost .
Stage 2:
- : .
- : .
Stage 1 ():
- .
Least total cost , along () or (). There are two optimal routes with the same cost, which is worth noticing — a dynamic programming table records the best value, and ties mean more than one route achieves it.
4. In that network, both and have the same cost to the end, 7 and 6 respectively, but from only is used. Explain.
Hint
Add the cost of the first edge to each.
Next step
; .
Full working
From the two options are then more, giving , and then more, giving . The first is cheaper.
Even though has a lower cost to the end than ( against ), the edge to is much more expensive ( against ). The decision at a stage combines the edge cost and the value of the state it leads to — neither alone decides it.
5. Explain why dynamic programming does not need to list every possible route.
Hint
How many numbers does it keep per state?
Next step
One: the least cost to the end.
Full working
It keeps one number per state — the least cost from that state to the end — and reuses it wherever that state appears. A route that passes through a state later uses the same number, so the work for that state is done once.
Listing every route would examine each route separately, and the number of routes grows very quickly with the number of stages. Dynamic programming does work proportional to the number of states and edges, not the number of routes.
6. A student works forwards, keeping the least cost to reach each state. Explain why this is harder than working backwards.
Hint
At a state in the middle, what do you still not know?
Next step
You do not know how much the rest of the journey will cost.
Full working
Working forwards, when you reach a state you know the cheapest way to get there, but not the cost of the rest of the journey — which is what you actually want to minimise. The cheapest way to reach a state may lead to an expensive continuation, exactly as the greedy choice did.
Working backwards, the value at each state is the cost of the remaining journey, which is precisely what the decision needs. That is why backward recursion is the natural direction for this kind of problem: the unknown is at the end, and the recursion starts where the unknown is known.
(Forward dynamic programming is possible and is used for some problems, but it needs the same values, and the backward version is the one this course uses.)
A question from Section 2.8
7. The multi-stage network of Example 1 has 4 stages and 4 routes from to . Dijkstra's algorithm and backward recursion both give 8. Explain what each method does differently, given they agree.
Hint
One settles vertices in order of distance from the start; the other fills in values from the end.
Next step
Both find shortest distances; they differ in the order the work is done.
Full working
Dijkstra's algorithm works forwards from the start: it settles the nearest unsettled vertex, working outwards from until everything is settled. It does not use the stage structure at all.
Backward recursion works backwards from the end, one stage at a time, using the fact that you must pass through exactly one vertex per stage.
On a network with stages, backward recursion does less work, because the stage structure tells it which vertices to consider next. On a general network with no stages, Dijkstra's algorithm applies and backward recursion does not. Both are correct; each suits a different shape of problem.
8. A network has 5 stages with 4 states in each. Roughly how many routes are there from the first stage to the last, and how many numbers does backward recursion keep?
Hint
A route picks one state per stage. How many ways?
Next step
choices in each of the 4 stages after the first: .
Full working
A route picks one state in each of the 4 stages after the first, so there are routes. Listing them all would mean 256 sums.
Backward recursion keeps one number per state: states, so 20 numbers. The saving grows with the size of the problem — with 10 stages and 10 states per stage it is routes against 100 numbers.