All pages 8 of 8

The graph

A map of stations and lines, where a ripple from one station finds the shortest way to all the others.

ABCDEFGHtwo routes meet
In life: Two routes can reach the same station.
ABCDEFGH
The way from A to H is A C F H: 3 hops, found in 8 steps.

Counted: 8 steps

Work the machine

The steps

  1. Start at A and light it.
  2. B is 1 hop from A.
  3. C is 1 hop from A.
  4. D is 1 hop from A.
  5. E is 2 hops from A, reached from B.
  6. F is 2 hops from A, reached from C.
  7. G is 2 hops from A, reached from D.
  8. H is 3 hops from A, reached from F.
  9. Follow the way back: H from F, F from C, C from A.
  10. The way from A to H is A C F H: 3 hops, found in 8 steps.
The machine, at the end of the worked example. Press a button to make a move and watch it, one step at a time.
find: every one8 stepsadd: about the same, whatever the size1 stepremove: about the same, whatever the size1 step
The costs, with 8 things inside, counted by running the machine above: one box a step.

What it is

Picture a metro map. Stations are joined by lines, and a station can have many lines. There is no top and no bottom, and there may be more than one way between two stations.

This is called a graph. The stations are called nodes and the lines are called edges.

How a move works

Here eight stations, A to H, are joined by nine lines. To ripple out from A, light A first. Then light each station one line away: B, C and D. Then each station one line from those: E, F and G. Then H.

Each station is lit once, so the ripple from A takes 8 steps. It also finds the shortest way to each station. To get from A to H, follow the ripple back: H was reached from F, F from C, and C from A. The way is A, C, F, H, which is 3 hops.

Drawing a new line takes 1 step, and so does rubbing one out.

Where you meet it

Maps, flights, friends on a social app and links between web pages are all graphs. A sat nav ripples out over the roads to find your way home.

Watch out for

A map is not a tree. Two routes can reach the same station, and a loop can lead you back to where you started. So a ripple must keep track of where it has been, or it goes round in circles for ever.