We are given a directed graph. We need compute whether the graph has negative cycle or not. A negative cycle is one in which the overall sum of the cycle comes negative.
Negative weights are found in various applications of graphs. For example, instead of paying cost for a path, we may get some advantage if we follow the path.
Input : 4 4 0 1 1 1 2 -1 2 3 -1 3 0 -1 Output : Yes The graph contains a negative cycle.
The idea is to use Bellman Ford Algorithm.
Below is algorithm find if there is a negative weight cycle reachable from given source.
1) Initialize distances from source to all vertices as infinite and distance to source itself as 0. Create an array dist of size |V| with all values as infinite except dist[src] where src is source vertex.
2) This step calculates shortest distances. Do following |V|-1 times where |V| is the number of vertices in given graph.
…..a) Do following for each edge u-v
………………If dist[v] > dist[u] + weight of edge uv, then update dist[v]
………………….dist[v] = dist[u] + weight of edge uv
3) This step reports if there is a negative weight cycle in graph. Do following for each edge u-v
……If dist[v] > dist[u] + weight of edge uv, then “Graph contains negative weight cycle”
The idea of step 3 is, step 2 guarantees shortest distances if graph doesn’t contain negative weight cycle. If we iterate through all edges one more time and get a shorter path for any vertex, then there is a negative weight cycle.
How does it work?
As discussed in Bellman Ford algorithm, for a given source, it first calculates the shortest distances which have at-most one edge in the path. Then, it calculates shortest paths with at-nost 2 edges, and so on. After the i-th iteration of outer loop, the shortest paths with at most i edges are calculated. There can be maximum |V| – 1 edges in any simple path, that is why the outer loop runs |v| – 1 times. If there is a negative weight cycle, then one more iteration would give a shorted path.
How to handle disconnected graph (If cycle is not reachable from source)?
The above algorithm and program might not work if the given graph is disconnected. It works when all vertices are reachable from source vertex 0.
To handle disconnected graph, we can repeat the process for vertices for which distance is infinite.
This article is contributed by kartik. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Detect cycle in the graph using degrees of nodes of graph
- Detect cycle in an undirected graph
- Detect Cycle in a Directed Graph using BFS
- Detect cycle in an undirected graph using BFS
- Detect Cycle in a Directed Graph
- Detect Cycle in a directed graph using colors
- Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph)
- Bellman–Ford Algorithm | DP-23
- Bellman Ford Algorithm (Simple Implementation)
- Detecting negative cycle using Floyd Warshall
- Coloring a Cycle Graph
- Degree of a Cycle Graph
- Check if there is a cycle with odd weight sum in an undirected graph
- Shortest cycle in an undirected unweighted graph
- Number of single cycle components in an undirected graph