Leaving Certificate Applied Mathematics2.9 Multi-stage problems and dynamic programming

All books

2.9 Multi-stage problems and dynamic programming — Practise with help

Warm-up: remember

  1. On a network, what is the shortest distance from the destination to itself?
  2. Why does Dijkstra's algorithm settle the nearest unsettled vertex?
Answers and feedback
  1. 0 — you are already there.
  2. 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 AA; B,CB, C; DD. Edges: AB=3AB = 3, AC=1AC = 1, BD=2BD = 2, CD=8CD = 8. Find the least cost from AA to DD by backward recursion.

Hint

Start at the last stage, DD, where the cost to the end is 0.

Next step

From BB: 33? No — from BB the cost to DD is BD=2BD = 2. From CC it is CD=8CD = 8.

Full working

Stage 3 (DD): cost to the end =0= 0.

Stage 2:

  • From BB: BD=2BD = 2, so cost =2+0=2= 2 + 0 = 2.
  • From CC: CD=8CD = 8, so cost =8+0=8= 8 + 0 = 8.

Stage 1 (AA):

  • A→BA \to B then 22: 3+2=53 + 2 = 5.
  • A→CA \to C then 88: 1+8=91 + 8 = 9.

Least is 55, along A→B→DA \to B \to D. Here the greedy choice happens to be wrong too: the cheapest first edge is AC=1AC = 1, but the better total is through AB=3AB = 3.

2. For that network, a student says "the cheapest first edge is AC=1AC = 1, so the answer is 9". Explain the mistake.

Hint

What does the cheap first edge lead to?

Next step

AC=1AC = 1 is cheap, but from CC the only way on is CD=8CD = 8.

Full working

The student has made the greedy choice: take the cheapest edge now. But the cheap first edge leads to an expensive continuation. A→CA \to C costs 1, and then C→DC \to D costs 8, giving 9 in total. The more expensive first edge A→BA \to B costs 3, but then B→DB \to D 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 PP; Q,RQ, R; S,TS, T; UU. Edges: PQ=1PQ = 1, PR=4PR = 4, QS=3QS = 3, QT=6QT = 6, RS=2RS = 2, RT=5RT = 5, SU=4SU = 4, TU=1TU = 1. Find the least total cost from PP to UU.

Hint

Work back: UU is 0, then stage 3 (S,TS, T), then stage 2, then stage 1.

Next step

From SS: SU=4SU = 4. From TT: TU=1TU = 1. Then from QQ: min⁡(3+4,6+1)\min(3+4, 6+1).

Full working

Stage 4 (UU): 00.

Stage 3:

  • SS: SU=4SU = 4, cost =4= 4.
  • TT: TU=1TU = 1, cost =1= 1.

Stage 2:

  • QQ: min⁡(QS+4,  QT+1)=min⁡(3+4,  6+1)=min⁡(7,7)=7\min(QS + 4,\; QT + 1) = \min(3 + 4,\; 6 + 1) = \min(7, 7) = 7.
  • RR: min⁡(RS+4,  RT+1)=min⁡(2+4,  5+1)=min⁡(6,6)=6\min(RS + 4,\; RT + 1) = \min(2 + 4,\; 5 + 1) = \min(6, 6) = 6.

Stage 1 (PP):

  • min⁡(PQ+7,  PR+6)=min⁡(1+7,  4+6)=min⁡(8,10)=8\min(PQ + 7,\; PR + 6) = \min(1 + 7,\; 4 + 6) = \min(8, 10) = 8.

Least total cost =8= 8, along P→Q→S→UP \to Q \to S \to U (1+3+4=81 + 3 + 4 = 8) or P→Q→T→UP \to Q \to T \to U (1+6+1=81 + 6 + 1 = 8). 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 QQ and RR have the same cost to the end, 7 and 6 respectively, but from PP only QQ is used. Explain.

Hint

Add the cost of the first edge to each.

Next step

PQ+7=8PQ + 7 = 8; PR+6=10PR + 6 = 10.

Full working

From PP the two options are P→QP \to Q then 77 more, giving 1+7=81 + 7 = 8, and P→RP \to R then 66 more, giving 4+6=104 + 6 = 10. The first is cheaper.

Even though RR has a lower cost to the end than QQ (66 against 77), the edge to RR is much more expensive (44 against 11). 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 AA to FF. 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 AA 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

44 choices in each of the 4 stages after the first: 444^4.

Full working

A route picks one state in each of the 4 stages after the first, so there are 44=2564^4 = 256 routes. Listing them all would mean 256 sums.

Backward recursion keeps one number per state: 5×4=205 \times 4 = 20 states, so 20 numbers. The saving grows with the size of the problem — with 10 stages and 10 states per stage it is 10910^9 routes against 100 numbers.

Settings

Colour scheme

Light or dark pages. Your colour and layout stay the same.

Colour

The paper, ink and lettering. Your layout stays the same.

Quiet colours
Bold colours
Layout

How pages are arranged. Your colours stay the same.

Reading offline

Keep this book on this device and read it offline.