Skip to content

Add parameter for FW negative cycle detection - #1097

Open
kennyballou wants to merge 1 commit into
jgrapht:masterfrom
kennyballou:fw-add-negative-test
Open

kennyballou wants to merge 1 commit into
jgrapht:masterfrom
kennyballou:fw-add-negative-test

Conversation

@kennyballou

Copy link
Copy Markdown

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
for (int k = minDegreeTwo; k < n; k++) {
for (int i = minDegreeOne; i < n; i++) {
if (i == k) {
if (!detectNegativeCycles && i == k) {

Copy link
Copy Markdown

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
Copy Markdown
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 <[email protected]>
@kennyballou
kennyballou force-pushed the fw-add-negative-test branch from fb191c3 to 4157eb2 Compare June 30, 2021 17:52
@jkinable

Copy link
Copy Markdown
Collaborator

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

This branch has not been deployed

No deployments
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