Basis Lab

Graphs and Finding a Path

When Some Links Cost More

Last timeFollowing One Path to the End

Weights break the ring walk immediately, and the repair is to serve the waiting line in order of cheapest known cost, which is one substitution and a proof.

The ring walk gives the wrong answer

Three towns. There is a road from the first to the third that takes two hours.

There is also a road from the first to the second taking ten minutes, and one

from the second to the third taking fifteen. The fewest links route is the

direct one, with a single link. The cheapest route is the other one, with two.

The ring walk will report the direct road, because it meets the third town at

distance one and never looks again. It is not malfunctioning; it is answering

the question it was built for, which is how few links, and that question has

stopped being the one anybody is asking.

FIG 1Two questions, two different answers
links usedtotal cost in minutesfound by the ring walk
direct road11201
through the second town2250
The ring walk finds the first row because it has fewer links. The marked cell is the answer anybody actually wanted. Once the links carry different costs, the two columns disagree, and no amount of care with the ring walk will fix it, because the ring walk never looks at the costs at all.

Serving the line by cost

The repair is one substitution. Keep a best known cost for every point,

starting at zero for the start and at infinity for everything else. Instead of

taking whichever point joined the line first, take whichever point in the line

has the smallest best known cost. Look at its neighbours, and for each one,

check whether going through this point is cheaper than the best route known so

far. If it is, record the lower cost and put the neighbour in the line.

Everything else is the ring walk. The waiting line is the only thing that

changed, and the structure that serves it is the one from the ordering course

that promises its smallest item and nothing else, which is precisely the

promise needed here.

FIG 2Cheapest first, from a, with costs on the links
stepstepcheapest in the lineits costimproved from itbest known costs
11a0b to 4, c to 1b 4, c 1
22c1b to 3, d to 7b 3, d 7
33b3d stays 7d 7
44d7e to 9e 9
55e9nothing leftall settled
5 steps
Notice step two. The route to b was already known at a cost of four, and going through c brings it down to three, so the cheaper figure replaces it. That improvement is the part the ring walk has no equivalent of, and it is why a point can be put in the line more than once before it is finally settled.

Why settling a point is safe

The method commits. When a point comes out of the line as the cheapest one, it

is declared settled and never improved again. That is a strong claim and it

needs an argument.

Suppose the cheapest point in the line is vv, at cost cc. Could some route

discovered later reach vv more cheaply? Any such route has to pass through

some point that is still in the line, because the route has to leave the

settled region at some point, and the first point it reaches outside is in the

line. Call that point uu. Since vv was the cheapest in the line, uu costs

at least cc. The route then continues from uu to vv along links, and if no

link can reduce a cost, the total is at least cc. So nothing cheaper exists.

The lesson stops here

3 more paragraphs to go

You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.

The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.

See the planThe contents

This is the reading half

Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.

The contents

The rest of this course

  1. 01Things and the Links Between Them
  2. 02Writing a Graph Downopening only
  3. 03Spreading Out One Ring at a Timeopening only
  4. 04Following One Path to the Endopening only
  5. 05When Some Links Cost Moreyou are here
  6. 06Doing Things in a Possible Orderopening only
  7. 07Connecting Everything Cheaplyopening only
  8. 08When the Graph Does Not Fitopening only

Read alongside