algorithmshortest-pathfloyd-warshall

add cycles with negative weights check in Floyd-Warshall


This is the pseudo-code for the Floyd-Warshall algorithm for shortest paths detection: enter image description here

I was wondering if it would be possible to add a check that will test if there are no cycles with negative weights?

I think it is possible, but I do not know how to check.


Solution

  • Take the sum of all negative weights in the graph.

    You have a negative cycle if and only if you find a path to a weight that is below that minimum.