Graph Theory37 sections · 1633 units
Open in Course

When to Use Floyd-Warshall

(vs running Dijkstra n times)

Use Floyd-Warshall when you need shortest paths between all pairs of vertices. If you only need paths from one source, use Dijkstra or Bellman-Ford instead. They are faster for single-source queries. If you need to answer many distance queries and can afford O(n3)O(n^3) preprocessing, Floyd-Warshall is ideal.

It also handles negative edges (but not negative cycles). The algorithm is simple enough to implement in a contest in under 22 minutes.