Loading algorithms…
Loading visualizer…
All-pairs shortest paths via dynamic programming — handles negative weights and detects negative cycles.
Floyd-Warshall runs once and computes shortest paths between every pair of vertices in O(V³). Running Dijkstra from each source would be O(V · (V+E) log V) and cannot handle negative edges. Running Bellman-Ford from each source would be O(V² · E). Compare Bellman-Ford →
Floyd-Warshall is a dynamic programming algorithm that computes the shortest paths between all pairs of vertices in a weighted graph in a single run — handling negative edge weights (but not negative cycles).
dist[i][i] = 0, direct edges get their weight, and all others are ∞.k (in turn), check every pair (i, j): if going through k is cheaper, update dist[i][j].next[i][j] matrix pointing to the next hop so any path can be traced in O(V).dist[i][i] < 0, a negative cycle exists.Define dist[k][i][j] as the shortest path from i to j using only vertices 0…k as intermediates. The recurrence is:
Since k can only shrink distances, the current matrix in-place update is safe — you never read a cell before it has been correctly set for the current k.
⚠️ Negative cycles: If any vertex ends up with dist[v][v] < 0 after the main loop, the graph contains a negative cycle and all shortest-path values involving that vertex are undefined.
Floyd-Warshall solves all-pairs. Dijkstra / Bellman-Ford solve single-source and must be run V times for all-pairs.
Floyd-Warshall and Bellman-Ford handle them. Dijkstra requires non-negative weights. Compare Bellman-Ford →
Floyd-Warshall is O(V³) — best when the graph is dense or you need all pairs. On sparse graphs, repeated Dijkstra is faster: O(V · E · log V).
Step through the O(V³) relaxation loop · Watch the matrix update in real time · Reconstruct any path instantly