0% found this document useful (0 votes)
75 views

Bellman Ford Algorithm

Uploaded by

Chandu Gowda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Bellman Ford Algorithm

Uploaded by

Chandu Gowda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

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.

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.

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.

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.

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

Step 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

o Therefore, Dist[B] = 5

1st Relaxation

Step 3: During 2nd Relaxation:

 Current Distance of D > (Distance of B) + (Weight of B to D) i.e. Infinity > 5 + 2

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

Step 4: During 3rd Relaxation:

 Current Distance of F > (Distance of D ) + (Weight of D to F) i.e. Infinity > 7 + 2

o Dist[F] = 9

 Current Distance of E > (Distance of C ) + (Weight of C to E) i.e. Infinity > 6 + 1

o Dist[E] = 7

3rd Relaxation
Step 5: During 4th Relaxation:

 Current Distance of D > (Distance of E) + (Weight of E to D) i.e. 7 > 7 + (-1)

o Dist[D] = 6

 Current Distance of E > (Distance of F ) + (Weight of F to E) i.e. 7 > 9 + (-3)

o Dist[E] = 6

4th Relaxation

Step 6: During 5th Relaxation:

 Current Distance of F > (Distance of D) + (Weight of D to F) i.e. 9 > 6 + 2

o Dist[F] = 8

 Current Distance of D > (Distance of E ) + (Weight of E to D) i.e. 6 > 6 + (-1)

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.

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)

o Dist[E]=5

 Current Distance of F > (Distance of D ) + (Weight of D to F) i.e. 8 > 5 + 2

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

Result: A negative cycle (D->F->E) exists in the graph.

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)

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.

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.

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;
}

You might also like