Talk:Floyd–Warshall algorithm

This is an old revision of this page, as edited by Justin W Smith (talk | contribs) at 17:03, 23 March 2010 (Undirected Graphs: ''Undirected'' just a special case. IMO, discussion of this (constant factor) optimization is not needed.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Latest comment: 15 years ago by Jwesley78 in topic Undirected Graphs
WikiProject iconMathematics B‑class Mid‑priority
WikiProject iconThis article is within the scope of WikiProject Mathematics, a collaborative effort to improve the coverage of mathematics on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.
BThis article has been rated as B-class on Wikipedia's content assessment scale.
MidThis article has been rated as Mid-priority on the project's priority scale.

WikiProject iconComputing Unassessed
WikiProject iconThis article is within the scope of WikiProject Computing, a collaborative effort to improve the coverage of computers, computing, and information technology on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.
???This article has not yet received a rating on Wikipedia's content assessment scale.
???This article has not yet received a rating on the project's importance scale.

More Explanation on Negative Cycles Needed

I think the "Behaviour with negative cycles" is not clear enough. When there is a negative cycle in the graph,does the output has any meaning? Can anyone explain it?Visame (talk) 17:28, 7 March 2008 (UTC)Reply

Basically, there is no solution to the shortest-path problem in a graph with negative cycles any machine can represent. You can think of it as the following: Assume you have a cycle C in a graph, and the overall sum of edge weights of this cycle is W. Furthermore, assume that this cycle contains a vertex V. If you now consider a path which goes like v_1, v_2, ..., V, ..., v_n, with an overall path weight P, then you can extend this path as follows: v_1, v_2, ..., V, C, V, ..., v_n. This works, since V is contained in the circle C, and thus, you can walk through the circle and end up at V again. Clearly, we can repeat this as much as we want, so v_1, ..., V, C, ..., C, V, ..., v_n, with as many C's as one likes all are still good paths through the graph. Now, consider the overall weight of these expanded paths. Without the circles, the overall weight of the path was P, but now, with one circle added to expand the graph, the new weight will be P + W, or, with K cycles added into the path, the new weight will be P + K*W. Now there are two cases: If W is positive (W > 0), then it clearly holds that P < P + W < P + 2*W < ..., thus, a correct shortest path algorithm will ignore them. However, if W is negative (W < 0), then it clearly holds that P > P + W > P + 2*W, ..., as you subtract more, and more and more from the path length. Thus, there exists no shortest path in this graph, as for whatever path you declare the shortest one, I can construct a shorter path by using P and adding enough repetitions of the circle into P. Now, Floyd-Warshall is one of the nicer algorithms to handle this, as Floyd-Warshall just stops after a certain number of iterations and outputting paths with a certain negative weight, because the number of edges on a path considered by the Floyd-Warshall-algorithm is bounded, thus, the output is wrong (even though, as the article states, negative cycles can be detected by checking the diagonal elements: dist[v][v] describes the weight of circles containing V, which is exactly what I described earlier). I hope this helps, and if it does, it can be added to the article (Probably with a nice image to make it really clear) --Tetha (talk) 14:02, 3 September 2009 (UTC)Reply

FW no longer in the c++ boost library

They currently only list the Johnson algorithm, no FW (presumably because it's slower). I think if I blindly removed the link from the page it would get rv'd quickly. 134.173.201.55 (talk) 09:08, 13 April 2009 (UTC)Reply

Notation style

WP:MOSMATH exists. Really, it does.

Contrast these:

 ,  ,  ,  

The above is in this article.

 

The second one is standard style.

Are there really some people who are not instantly offended by the first form? Why do people do things like that? Michael Hardy (talk) 14:02, 22 July 2009 (UTC)Reply

Haha, seriously? Well, as the person who wrote the first version, I'm very "offended" that you would rather complain than just fix the article. Not all of us are up to date on obscure Wikipedia style rules. :P 132.228.195.207 (talk) 16:12, 7 August 2009 (UTC)Reply

I've done some fixing of the article and I'll be back for more.

It doesn't take any familiarity with Wikipedia style conventions to be offended by what the first version above looks like. Michael Hardy (talk) 17:51, 9 November 2009 (UTC)Reply

Update Pseudocode/change to Java?

The pseudocode is not that readable/self-explanatory. Also, it does not include how to find the path between two vertices (though there's now a description below it).

How would it be to update the psuedocode, including a change to demonstrate finding the path? Alternatively, what about replaceing the psuedo code with Java code like what I posted a few edits ago? I think the Java code is easier for an average reader to read/understand than this psuedocode is (even without knowledge of Java), but I'm not too familiar with Wikipedia's pseudocode conventions. luv2run (talk) 01:56, 30 November 2009 (UTC)Reply

I'm the one who deleted your code. I think that it is too long and that pseudocode is better for this purpose. I don't know about any pseudocode conventions (and I couldn't find any either). What do you think is difficult to understand in that code?
I think that in theory, the task is usually given as “find the length of the shortest path”. In practice, the path itself is usually required as well. I think that because of this, the code should only find the length (as it currently does) with a note how to find the path, but I'm not that sure about this.
Also, as a note, your code isn't correct, because Java's int can't have a value of infinity. But this is of course very easy to fix and isn't an argument against it. Svick (talk) 02:32, 30 November 2009 (UTC)Reply

True, thanks. On the current version, I don't like the syntax much, especially the { }^2, and also just think the inner loop isn't as self-explanatory as it should be. I'd rather make it a little longer to show what it actually does, or comment more. Like in the java I had, naming a variable length_thru_k, then comparing it to the current path length.

The java code was a little long, I agree, but half of it was an example showing how to recover the path. I don't know if the explanation we have now is sufficient to explain how to recover the path or not. Hard to tell ... maybe I'll see if I can come up with something a little more compact in psuedocode, C, or Java, and post it here. I guess the problem is it seems redundant to have a separate section for finding the path, but insufficient to have a brief description. Thoughts on that?luv2run (talk) 02:47, 30 November 2009 (UTC)Reply

I agree that the pseudocode as presented isn't that great, but I greatly prefer pseudocode to java (or any other programming language). The whole point of having pseudocode is so that we can avoid the messiness of a particular programming language and state the algorithm as clearly as possible. --Robin (talk) 02:57, 30 November 2009 (UTC)Reply



How does this look?

 1 int path[][];
 2 /* path is dimension n by n, where n is the number of vertices.
 3    Initially, path[i][j] is the length of an edge from i to j, or infinity if there is no edge.
 4    path[i][i] is zero, the length from any vertex to itself.
 5    After running the algorithm, path[i][j] will have the length of the shortest path from i to j. */
 6 
 7 procedure FloydWarshall ()
 8    for k := 1 to n
 9       /* Pick whichever is shorter: the path we already have from i to j,
10          or the path from i to k, then from k to j (a path from i to j through k). */
11       for each pair of nodes (i,j)
12          int length_thru_k := path[i][k]+path[k][j];
13          if length_thru_k < path[i][j] then
14             path[i][j] := length_thru_k

Additionally, we could easily demonstrate remembering the path by adding the appropriate array and statement under the if. Like so (the second procedure, GetPath, maybe not necessary?)

 1 int path[][];
 2 /* path is dimension n by n, where n is the number of vertices.
 3    Initially, path[i][j] is the length of an edge from i to j, or infinity if there is no edge.
 4    path[i][i] is zero, the length from any vertex to itself.
 5    After running the algorithm, path[i][j] will have the length of the shortest path from i to j. */
 6 
 7 int goes_thru[][];
 8 /* Initally, goes_thru[i][j] is set to (for example) -1, indicating no intermediate vertices.
 9    When we find a path from i to j passing through k, we save it here by setting goes_thru[i][j]
10    equal to k. This will allow us to recover the path from i to j. */
11 
12 procedure FloydWarshall ()
13    for k := 1 to n
14       /* Pick whichever is shorter: the path we already have from i to j,
15          or the path from i to k, then from k to j (a path from i to j through k). */
16       for each pair of nodes (i,j)
17          int length_thru_k := path[i][k] + path[k][j];
18          if length_thru_k < path[i][j] then
19             path[i][j] := length_thru_k;
20             goes_thru[i][j] := k;
21
22 procedure GetPath (i,j)
23    if path[i][j] equals infinity then
24       return "no path";
25    int intermediate := goes_thru[i][j];
26    if intermediate equals -1 then
27       return " ";   /* there is an edge from i to j, with no vertices between */
28    else
29       return GetPath(i,intermediate) + intermediate + GetPath(intermediate,j);

Let me know what you think. Thanks guys. -luv2run (talk) 03:55, 30 November 2009 (UTC)Reply

Applications and generalizations

  • Transitive closure of directed graphs (Warshall's algorithm). In Warshall's original formulation of the algorithm, the graph is unweighted and represented by a Boolean adjacency matrix. Then the addition operation is replaced by logical conjunction (AND) and the minimum operation by logical disjunction (OR).

I think the AND and OR operations are mixed up here. AND would result in 0 for all pairs except 1,1 which is the only pair with a minimum of 1, and OR is the logical operator for addition. Didn't want to change the main page b/c I don't actually know for sure if this is correct but it struck me as wrong when I saw it. -rockychat3 —Preceding unsigned comment added by 209.94.128.120 (talk) 04:56, 1 December 2009 (UTC)Reply

  in position   in this variant of the algorithm means that we already know that there is a path between vertices   and  . If you want to combine paths   and   (where you would use addition of their lengths in the usual variant), you have to use AND (you can go from   to   through   if there is a path between   and   AND there is a path between   and  ). When you try to decide whether there is path through   OR “direct” (i.e. one we already know) path, you use OR. The difference stems from the fact that “no path” is represented as infinity in the usual algorithm, but as   in this variant. Svick (talk) 11:31, 1 December 2009 (UTC)Reply

Undirected Graphs

Weighted undirected graphs are equally capable of being solved by the Floyd-Warshall algorithm though some small changes are needed to gain further optimization. I've made the changes to have the article reflect that fact. AlexandreZ (talk) 16:13, 23 March 2010 (UTC)Reply

Undirected graphs are essentially a special case for directed graphs, especially sine F-W works only with the incidence matrix. You're right that one can optimize away half of the comparisons if you know beforehand that the graph is undirected. However, I think that this detail is not needed, nor is that optimization commonly used. Jwesley78 17:03, 23 March 2010 (UTC)Reply