-
Notifications
You must be signed in to change notification settings - Fork 327
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 a find_negative_cycle
algo based on Bellman-Ford
#434
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi. This algorithm was definitely missing, thanks for the contribution!
Do you think you could add some benchmarks for bellman_ford
and find_negative_cycle
?
Something akin to benches/floyd_warshall.rs.
Also, it would be a good idea now to move the two algorithms to a separate algo/bellman_ford.rs
.
@ABorgna how do i run the benchmarks?
|
Seems #395 broke the benchmarks. Let me fix it on master. |
@oxarbitrage I won't be able to make the fix until later today, but adding the |
I cherry picked your benchmarks in master, and seems there is a bit of a performance hit with the separation :/
Try Btw, I fixed the benchmark compilation in #435. |
Right, the The |
While reconstructing the path in find_negative_cycle
Following #434 (comment), Also, 54a3e25 didn't remove the extra node in the path for the case 2 so I fixed it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is ready to merge, if you are happy with the changes.
Thanks again for the work :)
It looks great, thank you for the last changes :) |
* add a `find_negative_cycle` algo based on Bellman-Ford * fix some comments * fix typo * remove the extra ancestor in path * create a new bellman_ford module * remove commented line * fix doc comment * add benchmark for bellman_ford algos * add inline to function * Include negative edge weights in benchmarks * Use a visit_map instead of a linear search While reconstructing the path in find_negative_cycle * Fix dupped edge in reconstructed path (and test) Co-authored-by: Agustin Borgna <agustinborgna@gmail.com>
* add a `find_negative_cycle` algo based on Bellman-Ford * fix some comments * fix typo * remove the extra ancestor in path * create a new bellman_ford module * remove commented line * fix doc comment * add benchmark for bellman_ford algos * add inline to function * Include negative edge weights in benchmarks * Use a visit_map instead of a linear search While reconstructing the path in find_negative_cycle * Fix dupped edge in reconstructed path (and test) Co-authored-by: Agustin Borgna <agustinborgna@gmail.com>
Had been using a custom extension of the
bellman_ford
algorithm from this library in order to find paths of negative cycles in directed graphs. I decided to give it a shot and try to introduce it as a library algo.Motivation
shortestpath
function do return negative cycles path (even if they do it as an error string).Details
The new algorithm is the first of 2 presented on this paper: https://blogs.asarkar.com/assets/docs/algorithms-curated/Negative-Weight%20Cycle%20Algorithms%20-%20Huang.pdf
As the
find_negative_cycle
algo introduced here is the same as the bellman-ford for the first part i moved steps 1 and 2 form the original into a common function that both algorithms can use.I am unsure if this PR haves enough quality to be included but i also don't know if it haves the enough use cases and acceptance (#195) to be considered as part of the library so i preferred to post it as it is and see what happens.
If it not get included i hope some people could find it useful.