Fewest stops or lowest cost

Three algorithms,
one cheapest route

Describe a weighted network, pick a start and a target, and see what breadth-first search, Dijkstra's algorithm and Bellman-Ford each return, what they cost, how many edges they examine and where each one goes wrong.

Enter your figures and read the result

One edge per line: from, to, weight. The worked example has a cheapest route that is not the one with the fewest edges. Saved on this device only.

Loading Shortest Path Algorithm Comparator. If nothing appears, turn on JavaScript.

What you get

Enrichment · STEM ExplorationsOpen to everyone · no sign-in

What you finish with
A worked-out result for your own entry (a headline figure, tiles, tables and a drawing where one helps), as a printout, a spreadsheet (.csv) or a saved file.
Who it is for
Students in computer science and discrete mathematics, and teachers who want to show why weights change which route is shortest and why Dijkstra needs non-negative weights.
Format
Browser tool for a phone, tablet or computer: type or paste, the result updates as you type. Print, spreadsheet (.csv), save to a file and open it again.
Access
Open to everyone with the link: no account, no sign-in, nothing to buy and nothing to install. Everything you type stays in this browser; nothing is sent to Caveat.
Where it lives
The STEM Explorations space in Caveat Enrichment.
Support
Questions about access, an order or a correction: team@caveatacademic.com.

How to use it

  1. Load the worked example to see how it reads, or start with your own entry.
  2. Type or paste into the fields; the result updates as you type.
  3. Read the headline, the tiles and the tables.
  4. Try a second case from the buttons above the fields.
  5. Print the result, download a spreadsheet or save the entry to a file.

Find the route

How it works

The worked example from the edge list to the comparison of routes.

Why the fewest edges can cost more

From A to E the route A, B, E has two edges but costs 4 + 7 = 11. The route A, C, B, D, E has four edges and costs 1 + 2 + 1 + 3 = 7. Breadth-first search only counts edges, so it cannot see the cheaper route.

Negative weights

Dijkstra settles a node once it is the nearest, which only works if later edges cannot make a path shorter. A negative weight breaks that, so Bellman-Ford is the safer choice. If a loop adds up to a negative total, no shortest route exists at all.

Sources

  1. Cormen, T. H., Leiserson, C. E., Rivest, R. L., & Stein, C. Introduction to Algorithms, 4th ed., MIT Press, chapters 20 and 22: breadth-first search, Bellman-Ford and Dijkstra's algorithm.
  2. Dijkstra, E. W. (1959). A note on two problems in connexion with graphs. Numerische Mathematik, 1, 269-271.

The worked examples are invented for illustration. What you type is saved only in this browser and is never sent to Caveat. Support: team@caveatacademic.com.

Written with AI, then checked before it is published. How we checkFound a mistake? Report it and we fix it, or refund you.Cancel anytime.