Graph Theory37 sections · 1633 units
Open in Course

When Floyd-Warshall Wins

(All-pairs, small graphs)

Floyd-Warshall computes distances between every pair of nodes in O(N3)O(N^3). Use it when N ≤ 400400 and you need all-pairs distances. If N = 10001000, O(N3)=109O(N^3) = 10^9 operations.

Too slow. Run Dijkstra N times instead if weights are non-negative. Floyd-Warshall also works with negative edges and detects negative cycles. Check the diagonal after running it: if dist[i][i] < 0 for any ii, a negative cycle exists that passes through node ii.