Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dijkstra vs. Floyd-Warshall: Finding optimal route on all node pairs

I am reading up on Dijkstra's algorithm and the Floyd-Warshall algorithm. I understand that Dijkstra's finds the optimal route from one node to all other nodes and Floyd-Warshall finds the optimal route for all node pairings.

My question is would Dijkstra's algorithm be more efficient than Floyd's if I run it on every single node in order to find the optimal route between all pairings.

Dijkstra's runtime is O(E + VlogV) where Floyd's is O(V3). If Dijkstra's fails, what would its runtime be in this case? Thanks!

like image 320
pyt Avatar asked Nov 18 '10 07:11

pyt


People also ask

Is Floyd-Warshall always better than Dijkstra?

Unlike Dijkstra's algorithm, Floyd Warshall can be implemented in a distributed system, making it suitable for data structures such as Graph of Graphs (Used in Maps). Lastly Floyd Warshall works for negative edge but no negative cycle, whereas Dijkstra's algorithm don't work for negative edges.

What is the difference between Floyd-Warshall and Dijkstra algorithm?

The biggest difference is that Floyd's algorithm finds the shortest path between all vertices and Dijkstra's algorithm finds the shortest path between a single vertex and all other vertices.

Is it possible to find all pairs of shortest paths using Dijkstra's algorithm?

If we apply Dijkstra's Single Source shortest path algorithm for every vertex, considering every vertex as the source, we can find all pair shortest paths in O(V*VLogV) time.

In what circumstances might we want to use Dijkstra's algorithm to compute all pairs shortest paths over the Floyd-Warshall algorithm?

If you E = O(V) , then running Dijkstra for all nodes if better both in theory and in practice. Basically, run Dijkstra from all nodes if you expect to have about as many edges as you have nodes, and run Floyd if you expect to have almost complete graphs.


Video Answer


1 Answers

As others have pointed out, Floyd-Warshall runs in time O(n3) and running a Dijkstra's search from each node to each other node, assuming you're using a Fibonacci heap to back your Dijkstra's implementation, takes O(mn + n2 log n). However, you cannot always safely run Dijkstra's on an arbitrary graph because Dijkstra's algorithm does not work with negative edge weights.

There is a truly remarkable algorithm called Johnson's algorithm that is a slight modification to running Dijkstra's algorithm from each node that allows that approach to work even if the graph contains negative edges (as long as there aren't any negative cycles). The algorithm works by first running Bellman-Ford on the graph to transform it to a graph with no negative edges, then using Dijkstra's algorithm starting at each vertex. Because Bellman-Ford runs in time O(mn), the overall asymptotic runtime is still O(mn + n2 log n), so if m = o(n2) (note that this is little-o of n), this approach is asymptotically faster than using Floyd-Warshall.

The one catch here is that this assumes that you have Dijkstra's algorithm backed by a Fibonacci heap. If you don't have Fibonacci heap available and aren't willing to put in the 72 hours necessary to build, debug, and test one, then you can still use a binary heap for Dijkstra's algorithm; it just increases the runtime to O(m log n), so this version of Johnson's algorithm runs in O(mn log n). This is no longer always asymptotically faster than Floyd-Warshall, because if m = Ω(n2) then Floyd-Warshall runs in O(n3) while Johnson's algorithm runs in O(n3 log n). However, for sparse graphs, where m = o(n2 / log n), this implementation of Johnson's algorithm is still asymptotically better than Floyd-Warshall

In short:

  • With a Fibonacci heap, Johnson's algorithm is always asymptotically at least as good as Floyd-Warshall, though it's harder to code up.
  • With a binary heap, Johnson's algorithm is usually asymptotically at least as good as Floyd-Warshall, but is not a good option when dealing with large, dense graphs.

Hope this helps!

like image 71
templatetypedef Avatar answered Sep 29 '22 22:09

templatetypedef