Graph Theory37 sections · 1633 units
Open in Course

Naive Approach

(Running Dijkstra n times)

The naive approach to all-pairs shortest paths is to run Dijkstra from every vertex. If you run Dijkstra nn times, the total time is O(n2logn+nm)O(n^2 \log n + nm). For dense graphs where mn2m \approx n^2, this becomes O(n3logn)O(n^3 \log n).

Floyd-Warshall gives you O(n3)O(n^3) with no logarithmic factor and much simpler code. It also works with negative edges. The code is about 1010 lines compared to Dijkstra's priority queue machinery.

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