New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add Bellman-Ford algorithm for shortest paths #8714
Comments
comment:1
Here is an implementation by David Joyner:
And some examples:
and
|
This comment has been minimized.
This comment has been minimized.
Dependencies: #12806 |
comment:4
Hello, I have checked the networkx implementation of the Bellman-Ford algorithm (See here) and we can propose a better implementation. This is a first implementation that can certainly be improved. Its advantage is that in the best case the time complexity is in O(|V|+|E|) and in the worst case, it is O(|V|.|E|). It uses a set to maintain the set of active vertices, that is vertices for which a change has been performed during previous round.
The implementation can be adapted to graphs and digraphs. Let me know if you think it is a good idea to write this patch. Best, |
comment:5
I have a better implementation, but I don't know exactly
Well, some minor details ;-) |
comment:6
Well, it depends on whether you want to optimize it in C a bit or not.. In this case the choice is made for you as you would need a Cython file. You could also use the "distance_all_pairs" file. I would say that the best name would be.... no name ? A hidden function. Then function would then be accessed through the usual methods of graphs/digraphs, like "distance", "shortest path", "distances all pairs", with a special flag or just automatically if there happen to be negative weights on the edges... What do you think ? It would be weird to create a new function for that if we have many already, especially if you wonder what it should return -- in this case it would have to return what the function calling it expects... We should be able to talk about in in a few days Nathann |
comment:7
At first I can add it to generic_graph.py, and let to others to option to improve the implementation in C. I will propose a patch over the week end. |
comment:8
Hello! Is there anything new with this patch? BF would be a very useful algorithm to have in Sage! |
comment:9
I don't have a lot of free time these days and it takes some time to write the documentation and doctests. I will try to commit soon. |
Attachment: trac_8714.patch.gz |
comment:10
I have uploaded the first part of the patch: the hidden bellman-ford function. You can already try it (and reviewed it) via It remains the long/boring part to call that function from other functions, add optional arguments and tests, etc. I don't have time to do it now, so anyone is welcome to contribute. I set the patch to needs review, but it should be needs work. David. |
Author: David Coudert |
comment:11
Well, then.. Nathann |
This comment has been minimized.
This comment has been minimized.
comment:13
Attachment: trac_8714.2.patch.gz Finally, this patch is ready to be reviewed! The method is now transparently called when negative weights are detected. I have also added an optional parameter that could be used e.g. in patch #13380. apply: trac_8714.2.patch |
Changed branch from u/ncohen/8714 to u/cheuberg/8714 |
comment:24
I had a short look at this patch and corrected a few obvious typos. Replying to @nathanncohen:
If there is a negative loop somewhere, New commits:
|
Branch pushed to git repo; I updated commit sha1. New commits:
|
comment:26
Thank you for pushing this patch. I had no time to do it in the last months, and I still don't know how to use git... I had a quick look at proposed changes. typo: algorith -> algorithm
I have some doubt about the recursive function Thanks. |
comment:27
Replying to @nathanncohen:
Apparently, the function However: is there any advantage of using the Bellman-Ford algorithm (as compared to Dijkstra) in the undirected case? If all edges have non-negative weight, Dijkstra should be more efficient. If there is an edge with negative weight (and this edge is reachable from the start vertex), then Bellman-Ford will detect it as a negative circuit (just take this edge in both directions repeatedly) and fail anyway. There is a method for computing shortest paths in undirected graphs with conservative weights (negative edge weights allowed, but no undirected cycle (different vertices!) of negative weight), cf. Corollary 12.13 in Korte and Vygen, Combinatorial Optimization (5th edition). But this has nothing to do with Bellman-Ford. My proposal would be to remove the code for undirected graphs in this proposed patch, which would increase readability IMHO, remove the problem with this |
comment:30
I fixed two issues with the code. This includes a simple doctest for the (previously broken for negative edge weights) function shortest_paths. IMHO, doctests should be included for the other |
comment:32
Bellman-Ford has now been implemented in #18931. As discussed in #18931 comment 15, the performance has not yet been compared with the code here. However, the branch attached to this ticket no longer merges with develop and very much work would be needed. Moreover, this ticket has not had any activity for 15 months. My suggestion is to close this ticket here as a duplicate. |
Reviewer: David Coudert |
comment:33
Actually I tried this patch a few days ago and I fully agree that it has to be fully rewritten. Moreover, to be competitive with respect to #18931, the algorithm has to be cythonized. |
I'm using #698 as a wish list of items to add to the graph theory module of Sage. The purpose of this ticket is to implement the Bellman-Ford algorithm for finding shortest paths in a weighted graph
G
that may have negative weights. IfG
doesn't have negative weights, Dijkstra's algorithm can be used. However, ifG
has negative weights, we fall back on the Bellman-Ford algorithm. The Bellman-Ford algorithm is able to handle graphs with negative weights, but not graphs that have negative-weight cycles. See also the function BellmanFord in Mathematica's Combinatorica package. See this graph theory book for an algorithmic presentation of the Bellman-Ford algorithm.See also the graph theory roadmap.
APPLY:
Depends on #12806
CC: @wdjoyner @dkrenn @dcoudert @nathanncohen
Component: graph theory
Author: David Coudert
Branch/Commit: u/cheuberg/8714 @
2c58a9b
Reviewer: David Coudert
Issue created by migration from https://trac.sagemath.org/ticket/8714
The text was updated successfully, but these errors were encountered: