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

  • Initialize distance array dist[] for each vertex ‘v‘ as dist[v] = INFINITY.
  • Assume any vertex (let’s say ‘0’) as source and assign dist = 0.
  • Relax all the edges(u,v,weight) N-1 times as per the below condition:
    • dist[v] = minimum(dist[v], distance[u] + weight)
  • Now, Relax all the edges one more time i.e. the Nth time and based on the below two cases we can detect the negative cycle:
    • Case 1 (Negative cycle exists): For any edge(u, v, weight), if dist[u] + weight < dist[v]
    • Case 2 (No Negative cycle) : case 1 fails for all the edges.

Bellman–Ford Algorithm

Imagine you have a map with different cities connected by roads, each road having a certain distance. The Bellman–Ford algorithm is like a guide that helps you find the shortest path from one city to all other cities, even if some roads have negative lengths. It’s like a GPS for computers, useful for figuring out the quickest way to get from one point to another in a network. In this article, we’ll take a closer look at how this algorithm works and why it’s so handy in solving everyday problems.

Table of Content

  • Bellman-Ford Algorithm
  • The idea behind Bellman Ford Algorithm
  • Principle of Relaxation of Edges for Bellman-Ford
  • Why Relaxing Edges N-1 times, gives us Single Source Shortest Path?
  • Why Does the Reduction of Distance in the N’th Relaxation Indicates the Existence of a Negative Cycle?
  • Working of Bellman-Ford Algorithm to Detect the Negative cycle in the graph
  • Algorithm to Find Negative Cycle in a Directed Weighted Graph Using Bellman-Ford
  • Handling Disconnected Graphs in the Algorithm
  • Complexity Analysis of Bellman-Ford Algorithm
  • Bellman Ford’s Algorithm Applications
  • Drawback of Bellman Ford’s Algorithm

Similar Reads

Bellman-Ford Algorithm

Bellman-Ford is a single source shortest path algorithm that determines the shortest path between a given source vertex and every other vertex in a graph. This algorithm can be used on both weighted and unweighted graphs....

The idea behind Bellman Ford Algorithm:

The Bellman-Ford algorithm’s primary principle is that it starts with a single source and calculates the distance to each node. The distance is initially unknown and assumed to be infinite, but as time goes on, the algorithm relaxes those paths by identifying a few shorter paths. Hence it is said that Bellman-Ford is based on “Principle of Relaxation“....

Principle of Relaxation of Edges for Bellman-Ford:

It states that for the graph having N vertices, all the edges should be relaxed N-1 times to compute the single source shortest path.In order to detect whether a negative cycle exists or not, relax all the edge one more time and if the shortest distance for any node reduces then we can say that a negative cycle exists. In short if we relax the edges N times, and there is any change in the shortest distance of any node between the N-1th and Nth relaxation than a negative cycle exists, otherwise not exist....

Why Relaxing Edges N-1 times, gives us Single Source Shortest Path?

In the worst-case scenario, a shortest path between two vertices can have at most N-1 edges, where N is the number of vertices. This is because a simple path in a graph with N vertices can have at most N-1 edges, as it’s impossible to form a closed loop without revisiting a vertex....

Why Does the Reduction of Distance in the N’th Relaxation Indicates the Existence of a Negative Cycle?

As previously discussed, achieving the single source shortest paths to all other nodes takes N-1 relaxations. If the N’th relaxation further reduces the shortest distance for any node, it implies that a certain edge with negative weight has been traversed once more. It is important to note that during the N-1 relaxations, we presumed that each vertex is traversed only once. However, the reduction of distance during the N’th relaxation indicates revisiting a vertex....

Working of Bellman-Ford Algorithm to Detect the Negative cycle in the graph:

Let’s suppose we have a graph which is given below and we want to find whether there exists a negative cycle or not using Bellman-Ford. Initial GraphStep 1: Initialize a distance array Dist[] to store the shortest distance for each vertex from the source vertex. Initially distance of source will be 0 and Distance of other vertices will be INFINITY. Initialize a distance arrayStep 2: Start relaxing the edges, during 1st Relaxation: Current Distance of B > (Distance of A) + (Weight of A to B) i.e. Infinity > 0 + 5 Therefore, Dist[B] = 51st RelaxationStep 3: During 2nd Relaxation:Current Distance of D > (Distance of B) + (Weight of B to D) i.e. Infinity > 5 + 2 Dist[D] = 7Current Distance of C > (Distance of B) + (Weight of B to C) i.e. Infinity > 5 + 1 Dist[C] = 62nd RelaxationStep 4: During 3rd Relaxation: Current Distance of F > (Distance of D ) + (Weight of D to F) i.e. Infinity > 7 + 2 Dist[F] = 9Current Distance of E > (Distance of C ) + (Weight of C to E) i.e. Infinity > 6 + 1 Dist[E] = 73rd RelaxationStep 5: During 4th Relaxation: Current Distance of D > (Distance of E) + (Weight of E to D) i.e. 7 > 7 + (-1) Dist[D] = 6Current Distance of E > (Distance of F ) + (Weight of F to E) i.e. 7 > 9 + (-3) Dist[E] = 64th RelaxationStep 6: During 5th Relaxation: Current Distance of F > (Distance of D) + (Weight of D to F) i.e. 9 > 6 + 2 Dist[F] = 8Current Distance of D > (Distance of E ) + (Weight of E to D) i.e. 6 > 6 + (-1) Dist[D] = 5Since the graph h 6 vertices, So during the 5th relaxation the shortest distance for all the vertices should have been calculated.5th RelaxationStep 7: Now the final relaxation i.e. the 6th relaxation should indicate the presence of negative cycle if there is any changes in the distance array of 5th relaxation. During the 6th relaxation, following changes can be seen: Current Distance of E > (Distance of F) + (Weight of F to E) i.e. 6 > 8 + (-3) Dist[E]=5Current Distance of F > (Distance of D ) + (Weight of D to F) i.e. 8 > 5 + 2 Dist[F]=7Since, we observer changes in the Distance array Hence ,we can conclude the presence of a negative cycle in the graph. 6th RelaxationResult: A negative cycle (D->F->E) exists in the graph....

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

Initialize distance array dist[] for each vertex ‘v‘ as dist[v] = INFINITY.Assume any vertex (let’s say ‘0’) as source and assign dist = 0.Relax all the edges(u,v,weight) N-1 times as per the below condition:dist[v] = minimum(dist[v], distance[u] + weight)Now, Relax all the edges one more time i.e. the Nth time and based on the below two cases we can detect the negative cycle:Case 1 (Negative cycle exists): For any edge(u, v, weight), if dist[u] + weight < dist[v]Case 2 (No Negative cycle) : case 1 fails for all the edges....

Handling Disconnected Graphs in the Algorithm:

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 graphs, we can repeat the above algorithm for vertices having distance = INFINITY, or simply for the vertices that are not visited....

Complexity Analysis of Bellman-Ford Algorithm:

Time Complexity when graph is connected: Best Case: O(E), when distance array after 1st and 2nd relaxation are same , we can simply stop further processingAverage Case: O(V*E)Worst Case: O(V*E)Time Complexity when graph is disconnected:All the cases: O(E*(V^2))Auxiliary Space: O(V), where V is the number of vertices in the graph....

Bellman Ford’s Algorithm Applications:

Network Routing: Bellman-Ford is used in computer networking to find the shortest paths in routing tables, helping data packets navigate efficiently across networks.GPS Navigation: GPS devices use Bellman-Ford to calculate the shortest or fastest routes between locations, aiding navigation apps and devices.Transportation and Logistics: Bellman-Ford’s algorithm can be applied to determine the optimal paths for vehicles in transportation and logistics, minimizing fuel consumption and travel time.Game Development: Bellman-Ford can be used to model movement and navigation within virtual worlds in game development, where different paths may have varying costs or obstacles.Robotics and Autonomous Vehicles: The algorithm aids in path planning for robots or autonomous vehicles, considering obstacles, terrain, and energy consumption....

Drawback of Bellman Ford’s Algorithm:

Bellman-Ford algorithm will fail if the graph contains any negative edge cycle....

Contact Us