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 27: Line 27:
 
If no negative-weight edges are present, which is often the case, the final loop may be omitted altogether from the algorithm, since it will never be useful. If negative-weight edges are present, then the final loop will <i>always</i> detect the presence of a negative-weight cycle. Suppose a negative-weight cycle exists. Then, choose any vertex <i>k</i> on this cycle. At the termination of the main loop, <i>dist[k][k]</i> will be negative, since the algorithm will inevitably have found the shortest path from <i>k</i> back to itself using each vertex in the graph at most once (the reason for this is explained in the previous section), which is, of course, of negative weight. (The presence of negative-weight cycles implies the presence of negative-weight simple cycles, as all non-simple cycles can be decomposed into simple cycles.) The algorithm then prints out an appropriate error message.
 
If no negative-weight edges are present, which is often the case, the final loop may be omitted altogether from the algorithm, since it will never be useful. If negative-weight edges are present, then the final loop will <i>always</i> detect the presence of a negative-weight cycle. Suppose a negative-weight cycle exists. Then, choose any vertex <i>k</i> on this cycle. At the termination of the main loop, <i>dist[k][k]</i> will be negative, since the algorithm will inevitably have found the shortest path from <i>k</i> back to itself using each vertex in the graph at most once (the reason for this is explained in the previous section), which is, of course, of negative weight. (The presence of negative-weight cycles implies the presence of negative-weight simple cycles, as all non-simple cycles can be decomposed into simple cycles.) The algorithm then prints out an appropriate error message.
  
==Warshall's algorithm==
+
==Special case: Warshall's algorithm==
A special case of the Floyd–Warshall algorithm is ''Warshall's algorithm'', which tests only for reachability, hence computing the [[transitive closure]] of a graph. If two vertices are linked by an edge, we assign the edge any finite weight (such as 0 or 1), otherwise we assign it an infinite weight. If, at the end, we find the distance from one vertex to another to be finite, then they are connected, since a path existed using only finite-weight edges (that is, ones that actually exist); otherwise, if no such path exists, the entry in the matrix will be infinity. Notice that we can replace "finite" by 1, "infinite" by 0, addition with logical AND, and <code>min</code> with logical OR, and preserve existing logic. This yields the following implementation of Warshall's algorithm:
+
A special case of the Floyd–Warshall algorithm is ''Warshall's algorithm'', which tests only for reachability. If two vertices are linked by an edge, we assign the edge any finite weight (such as 0 or 1), otherwise we assign it an infinite weight. If, at the end, we find the distance from one vertex to another to be finite, then they are connected, since a path existed using only finite-weight edges (that is, ones that actually exist); otherwise, if no such path exists, the entry in the matrix will be infinity. Notice that we can replace "finite" by 1, "infinite" by 0, addition with logical AND, and <code>min</code> with logical OR, and preserve existing logic. This yields the following implementation of Warshall's algorithm:
 
<pre>
 
<pre>
 
input adj
 
input adj
Line 42: Line 42:
  
 
[[Category:Algorithms]]
 
[[Category:Algorithms]]
[[Category:Graph theory]]
 

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)