Skip to content
OneKitly

The Shortest Path Uses the Most Edges

Published 12/3/2026 · 3 min read · Developer tools

Daniel Okonkwo

Daniel Okonkwo — Front-end developer and tech writer at OneKitly

Web performance · File formats

Checked against 2 sources

View profile →
In short

Take the seven edges A–B 4, A–C 2, B–C 1, B–D 5, C–D 8, C–E 10 and D–E 2, undirected. The cheapest route from A to E is A–C–B–D–E at a cost of 10, and it crosses four edges. The direct-looking A–C–E crosses two and costs 12; A–B–D–E crosses three and costs 11. Even A to B is cheaper the long way round: the direct edge costs 4, while going through C costs 2 + 1 = 3. Dijkstra never counts edges, so a route that looks like a detour on a drawing is simply the answer, and any code that filters candidates by hop count before comparing weights will miss it.

Advertisement

In a seven-edge graph, A to E costs 10 by a four-edge route and 12 by a two-edge one. Counting hops is a different problem from counting cost.

Reading the same seven lines as one-way streets changes the answer

Switch the graph to directed and the winning route disappears, because B–C now runs only from B to C and the trick of reaching B through C is gone. A to E becomes 11 by A–B–D–E, A to B becomes 4 by the direct edge instead of 3, and A to D rises from 8 to 9. Nothing in the edge list changed; only the reading did. This is the most common bug in routing code that was written against undirected test data: the algorithm is correct, the graph is not, and the symptom is a cost that is slightly too high rather than a crash.

One negative edge and the whole method stops working

Dijkstra works by settling the nearest unsettled node and never revisiting it, which is only sound if adding an edge can never lower a total. Give one edge a negative weight and a node settled early can turn out to have been reachable more cheaply later, and the algorithm will happily return a wrong number without any warning. The fix is not a patch but a different algorithm: Bellman–Ford relaxes every edge V − 1 times and detects negative cycles, at the cost of running in O(V·E) instead of O(E log V). Costs that can be negative — rebates, currency arbitrage, elevation regained on a descent — are the signal that you have left Dijkstra's domain.

Edges
Every route from A to E, cheapest first
RouteEdgesCost
A–C–B–D–E410
A–B–D–E311
A–C–E212
A–B–C–E315
Dijkstra shortest path calculatorEnter a weighted graph as edges ("A, B, 4" per line) and a source node: Dijkstra's algorithm returns the shortest distance and the exact path from the source to every reachable vertex. Works for directed or undirected graphs, accepts many edge formats, and flags unreachable vertices — ideal for routing, networks and pathfinding.Try the tool →
Advertisement

Frequently asked questions

Does the algorithm return every shortest path or just one?
One, because it stores a single predecessor per node. When two routes tie, the one that arrived first in the queue wins, and the loser is overwritten without trace — so two implementations can print different paths of identical cost and both be right. If you need all of them, keep a list of predecessors instead of one, and append whenever a candidate ties the current best rather than only when it beats it.
How do map applications route so much faster than this?
They do not run plain Dijkstra on the road network. A* adds a lower-bound estimate of the remaining distance so the search leans towards the destination; contraction hierarchies precompute shortcuts so a continental query touches a few thousand nodes instead of tens of millions. Both still return an exact shortest path — the speed comes from pruning what cannot possibly win, not from accepting an approximation. Plain Dijkstra remains the right choice when the graph is small, changes constantly, or has no useful geometry to estimate from.

Articles you may find interesting

All guides →

Related tools

Sources

Spotted a mistake in this article?