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.
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.
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.
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.
or make one
race these, up to four
Choose two to four racers.
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.
No tick the race offers is counted by every one of these racers, so one clock cannot time them. Choose others.
Every lane starts at A. The place to reach is D.
breadth-first search
done in 20 looksdijkstra's shortest paths
done in 9 looksa* search
done in 6 looksbellman-ford shortest paths
done in 40 lookswhat 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
- 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 (A alone, then B and E, then B and F, and on) and A* . 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 search | dijkstra's shortest paths | a* search | bellman-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 D | 20 | 9 | 6 | 40 |
| a line of 1,000 places | 1,998 | 999 | 999 | 3,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.
| shape | breadth-first search found | how long that way is | the other three found |
|---|---|---|---|
| a town at random | 4 roads | 69 long | 69 long |
| one long line | 7 roads | 70 long | 70 long |
| two rows and rungs | 3 roads | 30 long | 30 long |
| every road the grid allows | 3 roads | 35 long | 35 long |
| a shorter way round | 3 roads | 270 long | 50 long |
Their looks on the map the race makes in each shape.
| breadth-first search | dijkstra's shortest paths | a* search | bellman-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 H | 18 | 6 | 6 | 31 |
| one long lineA-B 10, B-C 10, C-D 10, D-H 10, G-H 10, F-G 10, E-F 10; A E | 14 | 7 | 7 | 47 |
| 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 D | 20 | 8 | 6 | 40 |
| 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 H | 26 | 13 | 12 | 52 |
| 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 D | 16 | 6 | 6 | 48 |