Fewest stops or lowest cost
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.
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.
Enrichment · STEM ExplorationsOpen to everyone · no sign-in
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.
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.
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.