Skip to content
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 parameter for FW negative cycle detection #1097

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kennyballou
Copy link

In b1f9e1e ("Implemented heuristics for Floyd-Warshall", Sat Jul 14
12:16:16 2018 +0300), some heuristics were added to improve the
performance of the Floyd-Warshall algorithm. However, as an unintended
consequence, the FW algorithm could no longer be used to detect negative
cycles.

Add a parameter to the algorithm constructor, maintaining the existing
behavior, which enables users to detect negative cycle detection.
Furthermore, add a method to check for negative cycles.

This resolves #1096.


@d-michail d-michail requested a review from jkinable June 29, 2021 10:00
@@ -305,11 +340,11 @@ private void lazyCalculateMatrix()
// run fw alg
for (int k = minDegreeTwo; k < n; k++) {
for (int i = minDegreeOne; i < n; i++) {
if (i == k) {
if (!detectNegativeCycles && i == k) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this change, the distance matrix, for your example graph, will become
[[-1, -1]
[-2, -2]]
Could you explain how is this correct?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally, when a graph does not contain a strictly negative cycle, no path through the graph will be "closer" than a self-loop on a node. This is the basis of the heuristic of skipping specific iterations. However, when a negative cycle is possible or a negative cycle does appear in the graph, there is a path that is not the self-loop that is "closer" than the self loop. The Floyd-Warshall algorithm can detect these negative cycles (when performed without the optimizing heuristic) and they appear in the diagonal of the weighted adjacency matrix.

I'm going to amend the PR with another test case for non-negative cycle graphs.

In b1f9e1e ("Implemented heuristics for Floyd-Warshall", Sat Jul 14
12:16:16 2018 +0300), some heuristics were added to improve the
performance of the Floyd-Warshall algorithm.  However, as an unintended
consequence, the FW algorithm could no longer be used to detect negative
cycles.

Add a parameter to the algorithm constructor, maintaining the existing
behavior, which enables users to detect negative cycle detection.
Furthermore, add a method to check for negative cycles.

This resolves jgrapht#1096.

Signed-off-by: Kenny Ballou <kb@devnulllabs.io>
@jkinable
Copy link
Collaborator

I've blocked some time in my calendar on Friday to work on my backlog of reviews...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Floyd Warshall Shortest Paths cannot detect negative cycles
3 participants