Editing Floyd–Warshall algorithm

Jump to: navigation, search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 1: Line 1:
 
The '''Floyd–Warshall algorithm''' finds [[Shortest_path#All-pairs_shortest_paths|all-pairs shortest paths]] in a directed, weighted graph which contains no negative-weight cycles. That is, unlike [[Dijkstra's algorithm]], it is guaranteed to correctly compute shortest paths even when some edge weights are negative. (Note however that it is still a requirement that no negative-weight ''cycle'' occurs; finding shortest paths in such a graph becomes either meaningless if non-simple paths are allowed, or computationally difficult when they are not.) With a running time of <math>\mathcal{O}(V^3)</math>, Floyd–Warshall is asymptotically optimal in dense graphs. It is outperformed by [[Dijkstra's algorithm]] or the [[Bellman–Ford algorithm]] in the [[Shortest_path#Single-source_shortest_paths|single-source shortest paths]] problem (with running times of <math>\mathcal{O}(V^2)</math> and <math>\mathcal{O}(VE)</math>, respectively); in the all-pairs shortest paths problem in a sparse graph, it is outperformed by repeated application of Dijkstra's algorithm and by [[Johnson's algorithm]]. Nevertheless, in small graphs (fewer than about 300 vertices), Floyd–Warshall is often the algorithm of choice, because it computes all-pairs shortest paths, handles negative weights on edges correctly while detecting negative-weight cycles, and is very easy to implement.
 
The '''Floyd–Warshall algorithm''' finds [[Shortest_path#All-pairs_shortest_paths|all-pairs shortest paths]] in a directed, weighted graph which contains no negative-weight cycles. That is, unlike [[Dijkstra's algorithm]], it is guaranteed to correctly compute shortest paths even when some edge weights are negative. (Note however that it is still a requirement that no negative-weight ''cycle'' occurs; finding shortest paths in such a graph becomes either meaningless if non-simple paths are allowed, or computationally difficult when they are not.) With a running time of <math>\mathcal{O}(V^3)</math>, Floyd–Warshall is asymptotically optimal in dense graphs. It is outperformed by [[Dijkstra's algorithm]] or the [[Bellman–Ford algorithm]] in the [[Shortest_path#Single-source_shortest_paths|single-source shortest paths]] problem (with running times of <math>\mathcal{O}(V^2)</math> and <math>\mathcal{O}(VE)</math>, respectively); in the all-pairs shortest paths problem in a sparse graph, it is outperformed by repeated application of Dijkstra's algorithm and by [[Johnson's algorithm]]. Nevertheless, in small graphs (fewer than about 300 vertices), Floyd–Warshall is often the algorithm of choice, because it computes all-pairs shortest paths, handles negative weights on edges correctly while detecting negative-weight cycles, and is very easy to implement.
 
==The algorithm==
 
<pre>
 
input adj
 
for each k ∈ V(G)
 
    for each i ∈ V(G)
 
          for each j ∈ V(G)
 
              adj[i][j]=min(adj[i][j],adj[i][k]+adj[k][j])
 
for each k ∈ V(G)
 
    if adj[k][k] < 0
 
          error "Graph contains a negative-weight cycle"
 
</pre>
 
At the successful conclusion of the algorithm, the adjacency matrix <i>adj</i> will have been transformed into a shortest-paths matrix. If the distance from each vertex to itself is initially set as infinite, this matrix will also tell us the length of the shortest path of nonzero length from each vertex back to itself.
 
  
 
==Theory of the algorithm==
 
==Theory of the algorithm==

Please note that all contributions to PEGWiki are considered to be released under the Attribution 3.0 Unported (see PEGWiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)