Skip to content
Free public beta until December 4, 2026: every course is open, no card needed. After that, the first chapter of each course stays free and full access is a one-time $39 ($19 for up to 100 founding members chosen for helpful feedback). Your progress is kept. How the beta works

Which shortest-path algorithm do I run?

The right algorithm is decided entirely by what the edge weights are allowed to be.

Unweighted (every edge costs the same)? BFS already solves it. Weighted but every weight is non-negative? Dijkstra's algorithm. Some weight is negative? Dijkstra can be fooled -- Bellman-Ford is required. Need every pair of vertices' distances at once, not just from one source? Floyd-Warshall, regardless of weight sign.

Unweighted -> BFS

Non-negative weights -> Dijkstra

Finds the shortest route from one starting vertex to every other vertex in a graph with non-negative edge weights, always settling the closest unvisited vertex next.

Negative weights -> Bellman-Ford

Finds shortest paths from one vertex to all others even when some edge weights are negative, by relaxing every edge repeatedly.

All pairs at once -> Floyd-Warshall

Computes the shortest distance between every pair of vertices at once, by considering each vertex in turn as a possible shortcut.

The decision rule itself

Graph settings

Calculator