Skip to content

find the shortest route

Find the shortest way from one place to another along roads with lengths. Race four methods on the same map.

A look checks one road from one place. From A, the road to B is 12 long, so B can get the number 12, the length of its way from A so far. In the race below each method has a lane. At each tick of the clock, every lane still running makes one more look. Fewest looks wins.

All four race on one map of two-way roads. Each picture shows one of them on the starting map, from A to D: the places it finished with shaded, and the way it found drawn heavy.

Breadth-first search ignores lengths: every road counts as 1. It takes places in the order it found them, and looks along every road from each one. The number it writes at each place is the fewest roads from the start, not the shortest way.

12101210141010101010ABCDEFGH
Nothing tells it to stop at D, so it takes all 8 places and looks along all 10 roads from both ends: 20 looks. Its numbers: A 0, B and E 1, C and F 2, D and G 3, H 4. D is 3 roads away. That way is also 38 long, the shortest, by luck of this map.

Again and again, Dijkstra's method settles the place nearest the start that is not settled yet, which makes its number final. Then it looks along that place's roads to places not settled yet, and keeps any shorter way. It stops once the place to reach is settled.

12101210141010101010ABCDEFGH
It settles A 0, E 10, B 12, F 20, C 24 and G 30, each nearer than D, then D at 38. H, at 40, is never settled. 9 looks: 2, 1, 2, 1, 2 and 1, from A, E, B, F, C and G in turn.

A* (say A star) does the same, but settles the place with the smallest estimate: the way so far plus the straight line left. A straight line counts 10 a step across or down, however the picture spaces them. Across and down together, it goes by Pythagoras, rounded up. A to D is 30. E to D, 30 across and 10 up, is the square root of 900 + 100, about 31.6, so 32. Every road here is at least as long as its straight line, and going by other places cannot beat a straight line. A way along roads is a whole number, so a way at least 31.6 long is at least 32: rounding up never takes the line past it. So the line left never says too much, and A* still finds the shortest way.

12101210141010101010ABCDEFGH
A's estimate is 0 + 30, so in A*'s lane A shows 30, not 0. From A, B's 12 + 20 = 32 beats E's 10 + 32 = 42. From B, C's 24 + 10 = 34 beats F's 22 + 23 = 45 and E's waiting 42. From C, D's 38 + 0 = 38 beats G's 34 + 15 = 49, F's 45 and E's 42. Any other way leaves A, B and C through E, F or G, so it is at least 42, 45 or 49: none beats 38. 2 looks from each of A, B and C: 6, and the same answer.

Bellman-Ford reads only one-way roads, so each road here goes in as two, one each way. Round after round it goes through them in letter order: A to B, A to E, B to A, B to C and on. It looks along each one that leaves a place with a number by then, until a round changes nothing.

12101210141010101010ABCDEFGH
10 roads, 2 ways each, is 20 a round, as the place each one-way road leaves has its number by that road's turn. Round 1: the other 7 places get a number, and F, G and H are made shorter: 22 to 20, 34 to 30, 48 to 40. Round 2: nothing changes, so it stops. 40 looks, and D is 38, the same answer.

or make one

race these, up to four

one tick is

Without scripts the race cannot run, so each lane shows where its racer ends.

Tick 40: no lane has made more than 40 looks. Breadth-first search, dijkstra's shortest paths, a* search and bellman-ford shortest paths have finished.

Every lane starts at A. The place to reach is D.

breadth-first search

done in 20 looks
A0B1C2D3E1F2G3H4

dijkstra's shortest paths

done in 9 looks
12101210101010141010A0B12C24D38E10F20G30H40

a* search

done in 6 looks
12101210101010141010A30B32C34D38E42F45G49H

bellman-ford shortest paths

done in 40 looks
12101210101010141010A0B12C24D38E10F20G30H40

what the marks mean

  • a place or road being looked at or compared
  • a place given a new number, and the road that gave it
  • a place whose number is final, and the road to it
  • the place a lane starts from
  • a road passed by
  • any other place or road
  • how much of its race a lane has run

which suits this map

On this map of 8 places, a* search made the fewest looks: 6, against 9 for dijkstra's shortest paths, 20 for breadth-first search and 40 for bellman-ford shortest paths.

What each lane found for D: breadth-first search 3 roads, dijkstra's shortest paths 38 long, a* search 38 long and bellman-ford shortest paths 38 long.

each racer's own page

The same racers on one line of two-way roads, each 10 long, in letter order: looks up, how many places across, both scales in powers of ten. Each point is counted on that line.
102104101102103104105106107101102103104101102103104105106107
  • breadth-first search
  • dijkstra's shortest paths
  • a* search
  • bellman-ford shortest paths

the starting racers, then on one line

On the starting map A* makes the fewest looks. On one line of 1,000 places, Dijkstra's method and A* tie at 999. Each only ever has the next place to choose, and looks along a road only to a place not settled yet, so each looks along every road once. Breadth-first search looks along each road from both ends: 1,998. Bellman-Ford does that twice, in 2 rounds: 3,996. Looks are not all the work, though. To choose each next place, Dijkstra's method and A* weigh up every place that has a number and is not settled yet. On the starting map Dijkstra's method weighs up 1+2+2+2+2+2+2=131 + 2 + 2 + 2 + 2 + 2 + 2 = 13 (A alone, then B and E, then B and F, and on) and A* 1+2+3+4=101 + 2 + 3 + 4 = 10. Breadth-first search and Bellman-Ford weigh up none. Dijkstra's method and A* also read a road again from its other end, only to skip it. Against letter order, Bellman-Ford reads a road from a place with no number yet, and skips it. Neither counts as a look.

First, their looks on the starting map, run once. Then their looks on one line of 1,000 places, one road after another, each 10 long, in letter order: the same map for all four. Letter order suits Bellman-Ford, as one round carries a number the whole way. Started from the line's other end, a round carries a number only one road further, and looks from one more place, which adds 2 looks. That is 1 + 3 + 5 + ... + 1,997 = 998,001 looks, then 1,998 that change nothing: 999,999. Bellman-Ford's own page counts lines of one-way roads, so its 1,998 there is half of the 3,996 here. The fewest in each row is in bold.

breadth-first searchdijkstra's shortest pathsa* searchbellman-ford shortest paths
the starting mapA-B 12, A-E 10, B-C 12, E-F 10, B-F 10, F-G 10, C-G 10, C-D 14, G-H 10, D-H 10; A D209640
a line of 1,000 places1,9989999993,996

the starting racers on a map of 8 places in each shape

By looks, on every shape below: A* fewest or tied with Dijkstra's method, then breadth-first search, then Bellman-Ford. Shape moves the gaps, and on a shorter way round breadth-first search's way stops being the shortest. Bellman-Ford's rounds hang on letter order. One long line runs A, B, C, D, H, then back through G, F and E. H to G comes after D to H in letter order, so round 1 reaches G. G to F came earlier in that round, so round 2 reaches only F. F to E came earlier again, so round 3 reaches only E, and round 4 changes nothing. Round 1 looks from A, B, C, D and H: 1 + 2 + 2 + 2 + 2 = 9. G adds 2, F adds 2 and E adds 1: 9 + 11 + 13 + 14 = 47 looks. Of the four, only Bellman-Ford can take a road below 0, which pays back, and only if that road runs one way. Taken both ways, such a road is a loop: at 5 below 0, there and back is 10 below 0, and twice round is 20 below 0. So there is no shortest way. The box above takes none, so that strength never shows here. Use breadth-first search when every road counts the same, and Dijkstra's method when lengths differ. Use A* when you can also work out a straight line that no road is shorter than, and Bellman-Ford when a one-way road goes below 0.

9090901010101010ABCDEFGH
A shorter way round. Along the top, 3 roads of 90 make 270. Round by the bottom, 5 roads of 10 make 50, drawn heavy. Breadth-first search answers 3 roads, the fewest, and that way is not the shortest.
10101010101010101010ABCDEFGH
Two rows joined by rungs, every road 10, D straight ahead. A* settles only the top row, shaded: 6 looks. Dijkstra's method also settles E at 10 and F at 20, no further than C at 20. G ties D at 30, and a tie goes by letter, so D comes first: 8 looks.
shapebreadth-first search foundhow long that way isthe other three found
a town at random4 roads69 long69 long
one long line7 roads70 long70 long
two rows and rungs3 roads30 long30 long
every road the grid allows3 roads35 long35 long
a shorter way round3 roads270 long50 long
The same maps as the table below. On one long line and on two rows and rungs every road is 10. So the fewest roads are also the shortest way, and its length is 10 times its roads. Where lengths differ, breadth-first search answers another question. Here its way comes out longer only on a shorter way round.

Their looks on the map the race makes in each shape.

breadth-first searchdijkstra's shortest pathsa* searchbellman-ford shortest paths
a town at randomA-B 14, A-E 17, C-D 33, C-G 22, C-H 16, D-H 18, E-F 25, F-G 12, G-H 15; A H186631
one long lineA-B 10, B-C 10, C-D 10, D-H 10, G-H 10, F-G 10, E-F 10; A E147747
two rows and rungsA-B 10, B-C 10, C-D 10, E-F 10, F-G 10, G-H 10, A-E 10, B-F 10, C-G 10, D-H 10; A D208640
every road the grid allowsA-B 10, B-C 10, C-D 10, E-F 10, F-G 10, G-H 10, A-E 10, B-F 10, C-G 10, D-H 10, A-F 15, B-G 15, D-G 15; A H26131252
a shorter way roundA-B 90, B-C 90, C-D 90, A-E 10, E-F 10, F-G 10, G-H 10, D-H 10; A D166648