Graph Theory37 sections · 1633 units
Open in Course

Time Complexity

(Cubic in vertices)

Floyd-Warshall has three nested loops, each running nn times. The innermost loop body is O(1)O(1). Total time: O(n3)O(n^3). This is slower than single-source Dijkstra (O(nlogn+m)O(n \log n + m)), but faster than running Dijkstra nn times on dense graphs.

For sparse graphs with many queries, Floyd-Warshall is often simpler. The simplicity of the code makes it a practical choice when n500n \leq 500 or so. Beyond that, consider Johnson's algorithm.

Space complexity is O(n2)O(n^2) for the data structures used.