Bellman
Bellman
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.
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.
Initial Graph
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.
1st Relaxation
Step 3: During 2nd Relaxation:
Current Distance of D > (Distance of B) + (Weight of B to D) i.e. Infinity > 5 + 2
Dist[D] = 7
Current Distance of C > (Distance of B) + (Weight of B to C) i.e. Infinity > 5 + 1
Dist[C] = 6
2nd Relaxation
3rd Relaxation
Step 5: During 4th Relaxation:
Current Distance of D > (Distance of E) + (Weight of E to D) i.e. 7 > 7 + (-1)
Dist[D] = 6
Current Distance of E > (Distance of F ) + (Weight of F to E) i.e. 7 > 9 + (-3)
Dist[E] = 6
4th Relaxation
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)
Dist[E]=5
Current Distance of F > (Distance of D ) + (Weight of D to F) i.e. 8 > 5 + 2
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
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.