Shortest path (class P)
Dijkstra and breadth-first search on the same graph — and they disagree.
What this demo checks
Both of these are class P: the work grows politely with the size of the graph, so they finish essentially instantly at this scale and would still finish on a graph of a million nodes.
They are also solving DIFFERENT problems, which is easy to miss. Dijkstra finds the cheapest route by edge weight. Breadth-first search finds the route with the fewest hops and ignores weight entirely.
Try A to H. There is a single direct edge A–H with weight 25. BFS takes it — one hop, job done. Dijkstra refuses it and walks the long way round for a total cost of 16. Neither is wrong; they are answering different questions.
- 1.Pick a start node and an end node
- 2.Choose Dijkstra or breadth-first search
- 3.Press Run and watch nodes light up in the order the algorithm settles them
What the code is doing under the hood
Dijkstra keeps a priority queue of the cheapest known cost to each node, always settling the cheapest unsettled node next. BFS keeps a plain FIFO queue, which is what makes it explore in rings of equal hop count.
Swapping the queue is the entire difference between the two algorithms. Everything else — the visited set, rebuilding the path from predecessors — is the same code.
Run this demo to see the code that executed.