Johnson Algorithm - Negative cycle check
Johnson Algorithm - Negative cycle check
Original Graph & Modified GraphI am using the BellmanFord algorithm to check the negative cycle for Johnson's algorithm.
The issue is after I add the extra node added with zero weights, the modified graph reports a negative cycle, while the original graph without the extra edge does not have any negative cycle.
My question is whether this can happen? If yes, how to detect negative cycle in Johnson's algorithm?
See the image for the sample graph. The modified graph reports a negative cycle from node 1 to node 2
– shibu thomas
Aug 24 at 8:47
1 Answer
1
If your graph has no negative cycle or no cycle at all, adding a node and connecting it with a zero weight edge to the original graph will not add a cycle, especially not a negative cycle.
But I'm not sure if this is not a misunderstanding. If you just add a zero weight edge to a graph connecting two already existing nodes, this can add a negative cycle.
Just imaging a tree with only negative edge weights. It has no cycles. But if you add a zero weight edge, you close a cycle and since the edge weights are all negative the cycle will also have negative length.
I think in case of Johnson's algorithm you add out-edges from a new node to all existing nodes. So this cannot close any cycles because you can never come back to the new node.
Please see the image. The modified graph now reports a -ve cycle from node 1 to node 2
– shibu thomas
Aug 24 at 8:48
I cannot see a negative cycle in the image. The minimal distance for node 2 is not correct. It should be -1 instead of 0. Since you connect the new node to all other node with zero weight edges, all distances can be at most zero. Have you stopped bellman ford too early? you need an extra round since you have added one node.
– AbcAeffchen
Aug 24 at 9:09
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Are you sure the added edges are directed from the new node to the existing nodes in the graph only and have weight 0? If so, are you sure that Bellman Ford is checking for negative weight cycles only? Maybe it detects 0-weight cycles as well.
– HSK
Aug 23 at 4:21