The BellmanFord algorithm emulates the shortest paths from a single source vertex to all other vertices in a weighted digraph. It is slower than Dijkstra's algorithm for the same problem but more versatile because it can handle graphs with some edge weights that are negative numbers. Alfonso Shimbel proposed the algorithm in 1955, but it is now named after Richard Bellman and Lester Ford Jr., who brought it out in 1958 and 1956. In 1959, Edward F. Moore published a variation of the algorithm, sometimes referred to as the BellmanFordāMoore algorithm.
What Is the BellmanFord Algorithm?
Dynamic Programming is used in the BellmanFord algorithm. It begins with a starting vertex and calculates the distances between other vertices that a single edge can reach. It then searches for a path with two edges, and so on. The BellmanFord algorithm uses the bottomup approach.
Based on the "Principle of Relaxation," more accurate values gradually recovered an approximation to the proper distance until finally reaching the optimum solution.
Negative weight edges can generate negative weight cycles, which reduce the total path distance by returning to the same point.
Why Should You Be Cautious With Negative Weights?
When attempting to find the shortest path, negative weight cycles may produce an incorrect result.
Shortest path algorithms, such as Dijkstra's Algorithm that cannot detect such a cycle, may produce incorrect results because they may go through a negative weight cycle, reducing the path length.
After learning about the BellmanFord algorithm, you will look at how it works in this tutorial.
How Does the BellmanFord Algorithm Work?
The BellmanFord algorithm works by grossly underestimating the length of the path from the starting vertex to all other vertices. The algorithm then iteratively relaxes those estimates by discovering new ways that are shorter than the previously overestimated paths.
You can ensure that the result is optimized by repeating this process for all vertices.
Step 1: Make a list of all the graph's edges. This is simple if an adjacency list represents the graph.
Step 2: "V  1" is used to calculate the number of iterations. Because the shortest distance to an edge can be adjusted V  1 time at most, the number of iterations will increase the same number of vertices.
Step 3: Begin with an arbitrary vertex and a minimum distance of zero. Because you are exaggerating the actual distances, all other nodes should be assigned infinity.
For each edge uv, relax the path lengths for the vertices:
If distance[v] is greater than distance[u] + edge weight uv, then
distance[v] = distance[u] + edge weight uv
Step 4:If the new distance is less than the previous one, update the distance for each Edge in each iteration. The distance to each node is the total distance from the starting node to this specific node.
Step 5: To ensure that all possible paths are considered, you must consider alliterations. You will end up with the shortest distance if you do this.
Moving ahead with this tutorial on the BellmanFord algorithm, you will now learn the pseudocode for this algorithm.
Pseudocode of the BellmanFord Algorithm
Every Vertex's path distance must be maintained. That can be stored in a Vdimensional array, where V is the number of vertices.
Not only do you need to know the length of the shortest path, but you also need to be able to find it. To accomplish this, you must map each Vertex to the Vertex that most recently updated its path length.
When the algorithm is finished, you can find the path from the destination vertex to the source.
function bellmanFordAlgorithm(G, s) //G is the graph and s is the source vertex Ā Ā for each vertex V in G Ā Ā Ā Ā dist[V] < infinite // dist is distance Ā Ā Ā Ā Ā Ā prev[V] < NULL // prev is previous Ā Ā dist[s] < 0 Ā Ā for each vertex V in G Ā Ā Ā Ā for each edge (u,v) in G Ā Ā Ā Ā Ā Ā temporaryDist < dist[u] + edgeweight(u, v) Ā Ā Ā Ā Ā Ā if temporaryDist < dist[v] Ā Ā Ā Ā Ā Ā Ā Ā dist[v] < temporaryDist Ā Ā Ā Ā Ā Ā Ā Ā prev[v] < uĀ Ā Ā for each edge (U,V) in G Ā Ā Ā Ā If dist[U] + edgeweight(U, V) < dist[V} Ā Ā Ā Ā Ā Ā Error: Negative Cycle Exists Ā Ā return dist[], previ[] 
As you progress through this tutorial, you will see an example of the BellmanFord algorithm for a better learning experience.
An Example of BellmanFord Algorithm
Consider the weighted graph below.
 Choose path value 0 for the source vertex and infinity for all other vertices.
 If the new calculated path length is less than the previous path length, go to the source vertex's neighboring Edge and relax the path length of the adjacent Vertex.
 This procedure must be repeated V1 times, where V is the number of vertices in total. This happened because, in the worstcase scenario, any vertex's path length can be changed N times to an even shorter path length.
 As a result, after V1 iterations, you find your new path lengths and can determine in case the graph has a negative cycle or not.
You will now look at the time and space complexity of the BellmanFord algorithm after you have a better understanding of it.
The Complexity of BellmanFord Algorithm
Following is the time complexity of the bellman ford algorithmĀ
For all cases, the complexity of this algorithm will be determined by the number of edge comparisons.
if temporaryDist < dist[v] 
Edge relaxation differences depend on the graph and the sequence of looking in on edges in the graph.
The algorithm may need to undergo all repetitions while updating edges, but in many cases, the result is obtained in the first few iterations, so no updates are required.
dist[v] < temporaryDist 

WorstCase Time Complexity
When you come across a negative cycle in the graph, you can have a worstcase scenario.
As an example of a negative cycle, consider the following:
In a complete graph with edges between every pair of vertices, and assuming you found the shortest path in the first few iterations or repetitions but still go on with edge relaxation, you would have to relax E * (E  1) / 2 edges, (V  1) number of times.
The worstcase scenario in the case of a complete graph, the time complexity is as follows:
O(V2) = O(E V). O(V) = O (V3)

Average Case Time Complexity
You can reduce the worstcase running time by stopping the algorithm when no changes are made to the path values. As a result, there will be fewer iterations.
Another way to improve it is to ignore any vertex V with a distance value that has not changed since the last relaxation in subsequent iterations, reducing the number of edges that need to be relaxed and increasing the number of edges with correct values after each iteration. More information is available at the link at the bottom of this post.
Relaxation occurs V  1 time for every E the number of edges, so you multiply the two and get the average, which is the quadratic time complexity of O. (E V).

Best Case Time Complexity
If edge relaxation occurs from left to right in the above graph, the algorithm would only need to perform one relaxation iteration to find the shortest path, resulting in the time complexity of O(E) corresponding to the number of edges in the graph.
The following is the space complexity of the bellman ford algorithm:
The space complexity of the BellmanFord algorithm is O(V).
Following that, in this BellmanFord algorithm tutorial, you will look at some use cases of the BellmanFord algorithm.
Uses of BellmanFord Algorithm
There are several realworld applications for the BellmanFord algorithm, including:
 Identifying negative weight cycles
 In a chemical reaction, calculate the smallest possible heat gain/loss.
 Identifying the most efficient currency conversion method.
You will now peek at some applications of the BellmanFord algorithm in this tutorial.
Applications of the BellmanFord Algorithm
Following are the applications of the bellman ford algorithm:
 Examining a graph for the presence of negative weight cycles.
 Using negative weights, find the shortest path in a graph.
 Routing is a concept used in data networks.
Last but not least, you will need to perform practical demonstrations of the BellmanFord algorithm in the C programming language.
Ā Code Demonstration of BellmanFord Algorithm
#include<stdio.h> #include<stdlib.h> #include<conio.h> #include<string.h> #include<limits.h> struct Edges { Ā Ā Ā Ā // This structure is equal to an edge. Edge contains two endpoints. These edges are directed edges so they //contain source and destination and some weight. These 3 are elements in this structure Ā Ā Ā Ā int src, dest, wt; }; // a structure to represent a graph struct Graph { Ā Ā Ā Ā int Vertex, Edge; //Vertex is the number of vertices, and Edge is the number of edges Ā Ā Ā Ā struct Edges* edge; // This structure contains another structure that we have already created. }; struct Graph* designGraph(int Vertex, int Edge) { Ā Ā Ā Ā struct Graph* graph = (struct Graph*) malloc( sizeof(struct Graph)); //Allocating space to structure graph Ā Ā Ā Ā graph>Vertex = Vertex; //assigning values to structure elements that taken form user. Ā Ā Ā Ā graph>Edge = Edge; Ā Ā Ā Ā graph>edge = (struct Edges*) malloc( graph>Edge * sizeof( struct Edges ) ); //Creating "Edge" type structures inside "Graph" structure, the number of edge type structures are equal to number of edgesĀ Ā Ā Ā Ā return graph; } void Solution(int dist[], int n) { // This function prints the last solution Ā Ā Ā Ā printf("\nVertex\tDistance from Source Vertex\n"); Ā Ā Ā Ā int i; Ā Ā Ā Ā for (i = 0; i < n; ++i){ printf("%d \t\t %d\n", i, dist[i]); } } void BellmanFordalgorithm(struct Graph* graph, int src) { Ā Ā Ā Ā int Vertex = graph>Vertex; Ā Ā Ā Ā int Edge = graph>Edge; Ā Ā Ā Ā int Distance[Vertex]; Ā Ā Ā Ā int i,j; Ā Ā Ā Ā // This is the initial step that we know, and we initialize all distances to infinity except the source vertex. // We assign source distance as 0 Ā Ā Ā Ā for (i = 0; i < Vertex; i++) Ā Ā Ā Ā Ā Ā Ā Ā Distance[i] = INT_MAX; Ā Ā Ā Ā Distance[src] = 0; Ā Ā Ā Ā //The shortest path of graph that contain Vertex vertices, never contain "Veretx1" edges. So we do here "Vertex1" relaxations Ā Ā Ā Ā for (i = 1; i <= Vertex1; i++) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā for (j = 0; j < Edge; j++) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int u = graph>edge[j].src;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int v = graph>edge[j].dest; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int wt = graph>edge[j].wt; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (Distance[u] + wt < Distance[v]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Distance[v] = Distance[u] + wt; Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā Ā Ā Ā //, up to now, the shortest path found. But BellmanFordalgorithm checks for negative edge cycles. In this step, we check for that Ā Ā Ā Ā // shortest path if the graph doesn't contain any negative weight cycle in the graph. Ā Ā Ā Ā // If we get a shorter path, then there is a negative edge cycle. Ā Ā Ā Ā for (i = 0; i < Edge; i++) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā int u = graph>edge[i].src; Ā Ā Ā Ā Ā Ā Ā Ā int v = graph>edge[i].dest; Ā Ā Ā Ā Ā Ā Ā Ā int wt = graph>edge[i].wt; Ā Ā Ā Ā Ā Ā Ā Ā if (Distance[u] + wt < Distance[v]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā printf("This graph contains negative edge cycle\n"); Ā Ā Ā Ā }Ā Ā Ā Ā Ā Solution(Distance, Vertex); Ā Ā Ā Ā return; } int main() { Ā Ā Ā Ā int V,E,S; //V = no.of Vertices, E = no.of Edges, S is source vertex printf("Enter number of vertices\n"); Ā Ā Ā Ā scanf("%d",&V); printf("Enter number of edges\n"); Ā Ā Ā Ā scanf("%d",&E); printf("Enter the source vertex number\n"); scanf("%d",&S); Ā Ā Ā Ā struct Graph* graph = designGraph(V, E); //calling the function to allocate space to these many vertices and edges Ā Ā Ā Ā int i; Ā Ā Ā Ā for(i=0;i<E;i++){ Ā Ā Ā Ā Ā Ā Ā Ā printf("\nEnter edge %d properties Source, destination, weight respectively\n",i+1); Ā Ā Ā Ā Ā Ā Ā Ā scanf("%d",&graph>edge[i].src); Ā Ā Ā Ā Ā Ā Ā Ā scanf("%d",&graph>edge[i].dest); Ā Ā Ā Ā Ā Ā Ā Ā scanf("%d",&graph>edge[i].wt); Ā Ā Ā Ā } Ā Ā Ā Ā BellmanFordalgorithm(graph, S); //passing created graph and source vertex to BellmanFord Algorithm function Ā Ā Ā Ā return 0; } 
Output
Now that you have reached the end of the BellmanFord tutorial, you will go over everything youāve learned so far.
Advance your career as a MEAN stack developer with the Post Graduate Program In Full Stack Web Development. Enroll now!
Next Steps
In this BellmanFord algorithm tutorial, you looked at what the algorithm is and how it works. You studied and comprehended the BellmanFord algorithm stepbystep, using the example as a guide. You also learned C programming language code and the output for calculating the distance from the source vertex in a weighted graph. And you saw the time complexity for applying the algorithm and the applications and uses that you can put to use in your daily lives.
Assume you're looking for a more indepth study that goes beyond Mobile and Software Development and covers today's most indemand programming languages and skills. In that case, Simplilearn's softwaredevelopment course is the right choice for you. Explore this globally recognized Bootcamp program. Rest assured that completing it will be the best decision you can make to enter and advance in the mobile and software development professions.
Do you have any queries about this tutorial on BellmanFord Algorithm? Please leave them in the comments section at the bottom of this page if you do. Our experts will be happy to respond to your questions as earliest as possible!