Open In App

Detect a negative cycle in a Graph | (Bellman Ford)

Last Updated : 19 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a directed weighted graph, your task is to find whether the given graph contains any negative cycles that are reachable from the source vertex (e.g., node 0).

Note: A negative-weight cycle is a cycle in a graph whose edges sum to a negative value.

Example:

Input: V = 4, edges[][] = [[0, 3, 6], [1, 0, 4], [1, 2, 6], [3, 1, 2]]

Example1


Example2
Example 2

Output: false

Algorithm to Find Negative Cycle in a Directed Weighted Graph Using Bellman-Ford:

  • Initial