Bellman Ford Algorithm
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.
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.
A Bellman-Ford algorithm is also guaranteed to find the shortest path in a graph, similar
to Dijkstra’s algorithm. Although Bellman-Ford is slower than Dijkstra’s algorithm, it is
capable of handling graphs with negative edge weights, which makes it more versatile. The
shortest path cannot be found if there exists a negative cycle in the graph. If we continue to go
around the negative cycle an infinite number of times, then the cost of the path will continue to
decrease (even though the length of the path is increasing). As a result, Bellman-Ford is also
capable of detecting negative cycles, which is an important feature.
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.
By relaxing edges N-1 times, the Bellman-Ford algorithm ensures that the distance
estimates for all vertices have been updated to their optimal values, assuming the graph doesn’t
contain any negative-weight cycles reachable from the source vertex. If a graph contains a
negative-weight cycle reachable from the source vertex, the algorithm can detect it after N-1
iterations, since the negative cycle disrupts the shortest path lengths.
In summary, relaxing edges N-1 times in the Bellman-Ford algorithm guarantees that the
algorithm has explored all possible paths of length up to N-1, which is the maximum possible
length of a shortest path in a graph with N vertices. This allows the algorithm to correctly calculate
the shortest paths from the source vertex to all other vertices, given that there are no negative-
weight cycles.
Step 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 array
o Therefore, Dist[B] = 5
1st Relaxation
o Dist[D] = 7
Current Distance of C > (Distance of B) + (Weight of B to C) i.e. Infinity > 5 + 1
o Dist[C] = 6
2nd Relaxation
o Dist[F] = 9
o Dist[E] = 7
3rd Relaxation
Step 5: During 4th Relaxation:
o Dist[D] = 6
o Dist[E] = 6
4th Relaxation
o Dist[F] = 8
o Dist[D] = 5
Since the graph h 6 vertices, So during the 5th relaxation the shortest distance for all the
vertices should have been calculated.
5th Relaxation
Step 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.
o Dist[E]=5
o Dist[F]=7
Since, we observer changes in the Distance array Hence ,we can conclude the presence of a
negative cycle in the graph.
6th Relaxation
Bellman-Ford Algorithm
The Bellman-Ford algorithm uses relaxation to find single source shortest paths on directed
graphs that may contain negative weight edges. The algorithm will also detect if there are any
negative weight cycles (such that there is no solution).
BELLMAN-FORD(G,w,s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i = 1 to |G.V|-1
3. for each edge (u,v) ∈ G.E
4. RELAX(u,v,w)
5. for each edge (u,v) ∈ G.E
6. if v.d > u.d + w(u,v)
7. return FALSE
8. return TRUE
INITIALIZE-SINGLE-SOURCE(G,s)
1. for each vertex v ∈ G.V
2. v.d = ∞
3. v.pi = NIL
4. s.d = 0
RELAX(u,v,w)
1. if v.d > u.d + w(u,v)
2. v.d = u.d + w(u,v)
3. v.pi = u
Basically the algorithm works as follows:
1. Initialize d's, π's, and set s.d = 0 ⇒ O(V)
2. Loop |V|-1 times through all edges checking the relaxation condition to compute minimum
distances ⇒ (|V|-1) O(E) = O(VE)
3. Loop through all edges checking for negative weight cycles which occurs if any of the relaxation
conditions fail ⇒ O(E)
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.
C Implementation:
#include <stdio.h>
#include <stdlib.h>
int Bellman_Ford(int G[20][20] , int V, int E, int edge[20][2])
{
int i,u,v,k,distance[20],parent[20],S,flag=1;
for(i=0;i<V;i++)
distance[i] = 1000 , parent[i] = -1 ;
printf("Enter source: ");
scanf("%d",&S);
distance[S-1]=0 ;
for(i=0;i<V-1;i++)
{
for(k=0;k<E;k++)
{
u = edge[k][0] , v = edge[k][1] ;
if(distance[u]+G[u][v] < distance[v])
distance[v] = distance[u] + G[u][v] , parent[v]=u ;
}
}
for(k=0;k<E;k++)
{
u = edge[k][0] , v = edge[k][1] ;
if(distance[u]+G[u][v] < distance[v])
flag = 0 ;
}
if(flag)
for(i=0;i<V;i++)
printf("Vertex %d -> cost = %d parent =
%d\n",i+1,distance[i],parent[i]+1);
return flag;
}
int main()
{
int V,edge[20][2],G[20][20],i,j,k=0;
printf("BELLMAN FORD\n");
printf("Enter no. of vertices: ");
scanf("%d",&V);
printf("Enter graph in matrix form:\n");
for(i=0;i<V;i++)
for(j=0;j<V;j++)
{
scanf("%d",&G[i][j]);
if(G[i][j]!=0)
edge[k][0]=i,edge[k++][1]=j;
}
if(Bellman_Ford(G,V,k,edge))
printf("\nNo negative weight cycle\n");
else printf("\nNegative weight cycle exists\n");
return 0;
}