Learn
Lesson · Dynamic Programming and Sequential Decisions · The recursion, backwards

Filling the Table from the End

The value of being somewhere is the cost of the next step plus the value of where that step leads. Write that down at every node, fill it in from the last stage back, and a routing problem falls out in one pass. The formula is the easy half. The half this lesson is built on is what the table does not contain: it holds one number per node and no route at all, so the routes have to be reconstructed afterwards — and when two continuations are worth the same, there is more than one of them.

f(v) = min over arcs v>w of [ c(v,w) + f(w) ], f(w) = 0 in the last stage filled from the LAST stage back, so every f(w) is settled before f(v) is written the value at the start is the answer; the ROUTE comes back from the argmins a node with two argmins is a TIE, and a tie on the route means two best answers arcs A>B 0 A>C 4 B>D 7 B>E 4 C>D 3 C>E 2 D>F 1 E>F 4 value 8 change A>B from 0 to 2 and the three optimal routes collapse to one
One recursion, and the reason every tie is kept

Core ideas

What this lesson establishes

Three ideas, and the formula is only the first of them. The other two are about what the table is and what it costs.

1

A node's value is a number, and it is not a route

What the recursion writes at each node is one quantity: the best total from there to the finish. It does not contain a path and cannot be asked for one. The routes are recovered at the end by walking the argmins forward from the start, and a node may have several argmins, so what comes back is a set of routes rather than a route. Reading the value as though it were a plan is what makes ties invisible.

2

Backwards is forced, not stylistic

f(v) is defined in terms of f(w) for the nodes v can reach, so it can only be written once those are settled. That is the whole reason the last stage comes first: its value is zero by definition, and every other value is built on values already final. A forward pass would need the answer before it could compute it, which is why the forward object in this lesson is an enumeration and not a second method.

3

The saving is that each node is priced once

Walking every path prices each path separately, and paths multiply as stages are added. The recursion prices each node once, and nodes add. On the network in the lab the two counts are 4 paths against 6 node values, which is no saving at all — and that is deliberate, because at this size you can check the recursion against the thing it replaces by eye. The saving is a shape, not a number.

The material

One sentence, written down at every node

The definition, the order it forces, and then the instance whose answer is three routes rather than one.

Definition · A staged network and its value function

A staged network is a list of stages, each holding some nodes, together with arcs that only ever run forward: an arc may join a node in one stage to a node in any later stage, never to one in the same stage or an earlier one. Each arc carries a cost.

The value function f gives each node the cheapest total from that node to the finish: f(w) = 0 for every node in the last stage, and f(v) = min over arcs out of v of c(v,w) + f(w) otherwise. An argmin at v is an arc attaining that minimum; there may be more than one.

The lab takes the stages as names separated by semicolons and the arcs one clause each, tail then > then head then cost. An arc that runs backwards or sideways is refused with the two stage numbers named, because that is a modelling error rather than a typing one: a network with a cycle in it has no last stage to start from and the recursion has nowhere to bottom out.

stages A ; B C ; D E ; F arcs A>B 0 A>C 4 B>D 7 B>E 4 C>D 3 C>E 2 D>F 1 E>F 4 filled from the END: f(F) = 0 last stage f(D) = 1 + f(F) = 1 f(E) = 4 + f(F) = 4 f(B) = min( 7 + 1 , 4 + 4 ) = min( 8 , 8 ) = 8 TIE: D and E both f(C) = min( 3 + 1 , 2 + 4 ) = min( 4 , 6 ) = 4 D f(A) = min( 0 + 8 , 4 + 4 ) = min( 8 , 8 ) = 8 TIE: B and C both six values written, and the answer is f(A) = 8

Read the last two lines again. f(A) is 8 and there are two ways to attain it, and f(B) is 8 with two ways to attain that. Following the argmins forward from A therefore produces three routes and not one: A B D F, A B E F and A C D F. The lab prices each of them again from the arcs before printing it, and all three come to 8.

The enumeration beside it, and what it is for

This network has four paths in total. Walked forwards and priced independently they come to 8, 8, 8 and 10, so the best is 8 and exactly three paths attain it. That agrees with the recursion, which is the point: the lab runs both and prints both, and shows nothing as an answer when they disagree. The enumeration is an oracle, not an alternative algorithm — it is exponential on purpose, and offering it as a way to solve these problems would undo the lesson.

Example · One arc cost, and the number of best answers

Change A>B from 0 to 2 and nothing else. Every value in the table is the same except f(A), which becomes min(2 + 8, 4 + 4) = 8 — still 8, but now attained only through C. The tie at B is still there, f(B) is still 8 with D and E both attaining it, and the four paths now cost 10, 10, 8 and 10.

So the answer is the same number and one route instead of three, and the tie that remains is on no optimal route at all. A recursion that dropped ties would give identical output on both networks. That is worth sitting with: the two cases are one arc cost apart, and nothing in the value 8 distinguishes them.

An arc that skips a stage

Add A>E 3 to the original network. The recursion needs no adjustment at all: f(E) is already settled when f(A) is written, because E lives in a later stage, and the new candidate is 3 + 4 = 7. That beats 8, so f(A) becomes 7 and the single optimal route is A E F. Walking all five paths forwards agrees. What makes this work is not a special case in the code but the ordering: a value is written only once everything it depends on is final, and a forward jump does not disturb that.

Example · Where the stages actually come from

In a routing problem the stages are given: they are the columns of the map. In most of the problems on this course they are not. A stage is whatever index makes the arcs run one way — an activity, a time period, a candidate, a number of periods left — and choosing it is choosing the model.

That is why this lesson comes first and why it is the easiest one here. Everything about the recursion is visible because the hard half has been done for you. The rest of the course does the hard half.

One last property, easy to miss because it looks like an implementation detail: a value is written once and never revised. If you find yourself wanting to go back and improve f at a node already filled in, either the ordering is wrong or the state is — and the second of those is the subject of everything that follows.

Interactive

Filling the table from the end

Every value the recursion writes, beside every path it stands for

Filling the table from the endthe value of being somewhere is the step plus the value of where it leads
an arc the recursion chosea tie, kept rather than resolvedthe value of each nodean arc it did not choose

Method

Filling a table backwards by hand

Five steps, and the last two are the ones people skip. Doing them in this order means you never revise a value.

1

Check that every arc runs forward

Stage numbers on the tail and the head, and the head's must be larger. If any arc fails this there is no last stage to start from and the recursion has no base case. The lab refuses such an arc by name rather than drawing it.

2

Set the last stage to zero and work leftwards

One column at a time, right to left. Within a column the order does not matter, because nothing in a stage depends on anything else in it.

3

At each node, price every arc out of it and record every argmin

c(v,w) + f(w) for each arc, then the minimum — and then a second pass for any other arc attaining it. Writing down only the first is where the ties go.

4

Reconstruct the routes by walking the argmins forward

From the start, branching wherever a node has more than one argmin. This is the step that turns a table of numbers into something you can act on, and it is the step at which a reader discovers the answer is a set.

5

Price each reconstructed route again, from the arcs

Add the arc costs along it and check the total against the value at the start. The recursion and the reconstruction are two pieces of arithmetic, and only the second one produces the thing you were actually asked for.

Values, routes, and what a tie is

Score 0 / 0

The same network twice, one arc cost apart

Both tables below are filled from F back. Only the cost of A>B differs, and only the last line of each table does.

stages A ; B C ; D E ; F A>C 4 B>D 7 B>E 4 C>D 3 C>E 2 D>F 1 E>F 4 A>B 0 A>B 2 f(F) 0 0 f(D) = 1 + f(F) 1 1 f(E) = 4 + f(F) 4 4 f(B) = min(7+1, 4+4) 8 tie D,E 8 tie D,E f(C) = min(3+1, 2+4) 4 4 f(A) min(0+8, 4+4) = 8 min(2+8, 4+4) = 8 tie B,C C only optimal routes A B D F A C D F A B E F A C D F all four paths walked forwards, priced from the arcs: A B D F 8 10 A B E F 8 10 A C D F 8 8 A C E F 10 10 value at the start 8 8 optimal routes 3 of 4 1 of 4 nodes with a tie B, A B

The two columns agree on the answer and disagree on how many answers there are. Nothing in the number 8 carries that difference, and neither does the drawing unless the ties are drawn — which is why the lab colours a tied arc differently from a chosen one and reports the tied nodes as a field of their own.

For a rehearsal, put A>B back to 0 and change D>F from 1 to 2 instead. Work out by hand what happens to f(D), f(B), f(C) and f(A), and how many optimal routes survive, before you let the lab tell you. The answer is still 8 and there is now exactly one route; the third rehearsal below is the one that goes the other way.

The harder rehearsal: add an arc from A straight to F and give it a cost of 9, then 8, then 7. At 9 nothing changes; at 8 the set of optimal routes grows by one; at 7 every other route stops being optimal and the answer becomes a single arc. Three regimes, one number, and the recursion never needed to be told that the arc skipped two stages.

Common mistakes

1
Recording the first argmin and calling it the argminThis is the defect that survives every check. The value is right, the route printed is a genuinely optimal route, and the page looks finished — it has simply stopped saying that there are others. On the network in the lab it turns three correct answers into one, and a reader who later needs a second-best route, or has a tie-break criterion the model did not include, has been quietly deprived of the thing they needed.
2
Trying to fill the table forwardsA forward pass computes, for each node, the cheapest way to reach it. That is a perfectly good quantity and it is not this one, and the two coincide only because a shortest path is reversible. The moment the arcs mean something asymmetric — an action taken, an amount spent — the forward quantity answers a different question. Fill backwards and the definition keeps you honest.
3
Believing the saving is about this networkSix values against four paths is not a saving and the lab says so. The reason to care is the shape: add a stage with k nodes and the paths multiply by something like k while the values grow by k. At the sizes on this page you can check the recursion by hand against the enumeration, which is the only reason the enumeration is there.

Check your understanding

✓
Finish when you can fill a small staged network backwards, list every optimal route, and say which nodes have ties and whether those ties matter.

You should be able to write f at each node from the last stage back, record every argmin rather than one, reconstruct the routes by walking forward, price each of them again from the arcs, and explain why changing a single arc cost can change the number of optimal routes without changing the answer.

Note: Everything here was solved on a network somebody else had already drawn: the stages were given, so the hardest modelling decision on this course had already been made. The next lesson, “The State Is What Is Left”, takes that away. There is no network in it until you build one, the nodes have to be invented, and the whole difficulty moves from the arithmetic to the question of what a node should mean.

Kept in this browser only, for you. Nothing is sent anywhere and nothing checks it. Carry it to another device.

Feedback and recommendations

Something to change about this lesson? Write it down, tick it off when it is handled, and export the list when you want to act on it. Kept in this browser only: nothing is sent anywhere, and nobody is notified.

No recommendations yet for this lesson.