Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As others have pointed out, Floyd-Warshall runs in time O(n<sup>3</sup>) 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 + n<sup>2</sup> 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.</p> <p>There is a truly remarkable algorithm called <strong><a href="http://en.wikipedia.org/wiki/Johnson%27s_algorithm" rel="noreferrer">Johnson's algorithm</a></strong> 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 <a href="http://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm" rel="noreferrer">Bellman-Ford</a> 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 + n<sup>2</sup> log n), so if m = o(n<sup>2</sup>) (note that this is <em>little-o</em> of n), this approach is asymptotically faster than using Floyd-Warshall.</p> <p>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 = &Omega;(n<sup>2</sup>) then Floyd-Warshall runs in O(n<sup>3</sup>) while Johnson's algorithm runs in O(n<sup>3</sup> log n). However, for sparse graphs, where m = o(n<sup>2</sup> / log n), this implementation of Johnson's algorithm is still asymptotically better than Floyd-Warshall</p> <p>In short:</p> <ul> <li>With a Fibonacci heap, Johnson's algorithm is always asymptotically at least as good as Floyd-Warshall, though it's harder to code up.</li> <li>With a binary heap, Johnson's algorithm is <em>usually</em> asymptotically at least as good as Floyd-Warshall, but is not a good option when dealing with large, dense graphs.</li> </ul> <p>Hope this helps!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload