Bellman Ford Algorithm using Dynamic Programming

This is a C++ Program that Solves Bellman Ford Algorithm using Dynamic Programming technique.

Problem Description

Given a weighted graph and a source vertex. Find the shortest path from the source to all the vertices. The graph may contain edges with negative value.

Problem Solution

This is a single source shortest path problem. We know that this problem can be solved using Djikstra’s Algorithm. But if the graph has negative weight edges, Djikstra’s Algo cannot be applied there. For that we have Bellman Ford Algorithm.

However, if the graph has negative weight loop, it is reported. This is because due to a negative weight cycle, the shortest distance for the vertices in the loop will keep on decreasing infinitely.

We create a distance array dist[]. dist[i]=minimum distance of vertex i from source vertex.

dist[src]=0

Now, compute the dist values for all the vertices in bottom up fashion for V-1 passes(V=no. of vertices). After this we have the min. dist values in the table for all vertices. At this point we will check for negative weight cycle. We iterate the calculation of dist values one more time, and if any value is reduced, we will know that there is a negative loop in the graph.

advertisement
advertisement
Expected Input and Output

Case-1:

 
number of vertices=5
number of edges=6
 
Information of all edges(source, destination, weight)
1 2 2
2 3 3
3 4 4
4 5 5
 
Source vertex=1
 
Expected Result: 
vectex    Min. distance from source
1         0
2         2
3         5
4         9
5         14

Case-2:

 
number of vertices=5
number of edges=7
 
Information of all edges(source, destination, weight)
 
1 2 17
2 3 25
3 4 1
4 2 -24
2 5 15
1 4 40
5 3 9
 
Source vertex=1
 
Expected Result: 
vectex    Min. distance from source
1         0
2         16
3         40
4         40
5         31

Case-3:

 
number of vertices=5
number of edges=6
 
Information of all edges(source, destination, weight)
1 2 6 
2 5 1
2 4 5
1 4 4
3 2 -4
5 3 2
 
Source vertex=1
 
Expected Result: There is a negative weight loop in the graph
Program/Source Code

Here is source code of the C++ Program for Bellman Ford Algorithm. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1.  
  2. //Bellman Ford Algorithm
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5.  
  6. //every edge has 3 values associated with it source, destination and weight
  7. struct edge
  8. {
  9.     int s,d,w;
  10. };
  11.  
  12. int bellmanFord(int n, int e, int src, vector<edge> &edge, vector<int> &dist)
  13. {
  14.     int i,j;
  15.     int s,d,w;
  16.  
  17.     i=src;
  18.  
  19.     dist[i-1]=0;
  20.  
  21.     //n-1 passes
  22.     for(i=1;i<n;i++)
  23.     {
  24.         //for each edge
  25.         for(j=0;j<e;j++)
  26.         {
  27.             s=edge[j].s;
  28.             d=edge[j].d;
  29.             w=edge[j].w;
  30.  
  31.             //if we can get a smaller value of dist[d] using this edge, replace it
  32.             if(dist[s]!=INT_MAX && dist[s]+w<dist[d])
  33.             {
  34.                 dist[d]=dist[s]+w;
  35.             }
  36.         }
  37.     }
  38.  
  39.     //this loop is to detect a negative loop
  40.     for(j=0;j<e;j++)
  41.     {
  42.         s=edge[i].s;
  43.         d=edge[i].d;
  44.         w=edge[i].w;
  45.  
  46.         if(dist[s]!=INT_MAX && dist[s]+w<dist[d])
  47.         {
  48.             //we have caught a negative loop
  49.             return 0;
  50.         }
  51.     }
  52.  
  53.     //all okay signal
  54.     //indicating no negative loop
  55.     return 1;
  56. }
  57.  
  58. int main()
  59. {
  60.     int i;
  61.     int n,e;
  62.     int s,d,w;
  63.     int src;
  64.  
  65.     cout<<"Enter the number of vertices ";
  66.     cin>>n;
  67.  
  68.     cout<<"Enter the number of edges ";
  69.     cin>>e;
  70.  
  71.     vector<edge> edge(e);
  72.  
  73.     cout<<"Enter the src, dest and weight of each edge"<<endl;
  74.     for(i=0;i<e;i++)
  75.     {
  76.         cin>>s>>d>>edge[i].w;
  77.         edge[i].s=s-1;
  78.         edge[i].d=d-1;
  79.     }
  80.  
  81.     cout<<"Enter the source vertex ";
  82.     cin>>src;
  83.  
  84.  
  85.     //create a vector of size n(for n vertices) and initialize the value of each element to infinity
  86.     //dist[i]=the minimum distance of vertex i from source vertex
  87.     vector<int> dist(n,INT_MAX);
  88.  
  89.     i=bellmanFord(n,e,src,edge,dist);
  90.  
  91.     cout<<endl;
  92.  
  93.     if(i)
  94.     {
  95.         cout<<"vectex      Min. distance from source"<<endl;
  96.         for(i=0;i<n;i++)
  97.         {
  98.             cout<<i+1<<"         "<<dist[i]<<endl;
  99.         }
  100.     }
  101.  
  102.     else
  103.     {
  104.         //negative loop
  105.         cout<<"There is a negative weight loop in the graph "<<endl;
  106.     }
  107.  
  108.     return 0;
  109. }
Program Explanation

The information of all the edges are stored in the nodes of a structure edge. In the main function, we take the inputs for number of vertices, number of edges and information of edge(source, destination and weight).

advertisement

Then the function BellmanFord() is invoked which will calculate the shortest path for all vertices from the source. If a negative weight cycle is found, it is reported. Otherwise the distance array is displayed.

Runtime Test Cases
 
Case-1:
$ g++ bellman_ford.cpp
$ ./a.out
Enter the number of vertices 5
Enter the number of edges 4
Enter the src, dest and weight of each edge
1 2 2
2 3 3
3 4 4
4 5 5
Enter the source vertex 1
 
vectex    Min. distance from source
1         0
2         2
3         5
4         9
5         14
 
Case-2:
$ ./a.out
Enter the number of vertices 5
Enter the number of edges 7
Enter the src, dest and weight of each edge
1 2 17
2 3 25
3 4 1
4 2 -24
2 5 15
1 4 40
5 3 9
Enter the source vertex 1
 
vectex    Min. distance from source
1         0
2         16
3         40
4         40
5         31
 
Case-3:
$ ./a.out
Enter the number of vertices 5
Enter the number of edges 6
Enter the src, dest and weight of each edge
1 2 6 
2 5 1
2 4 5
1 4 4
3 2 -4
5 3 2
Enter the source vertex 1
 
There is a negative weight loop in the graph

Sanfoundry Global Education & Learning Series – Dynamic Programming Problems.
To practice all Dynamic Programming Problems, here is complete set of 100+ Problems and Solutions.

advertisement

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.