implementation of bellman ford algorithm code example
Example 1: bellman ford code in c++
#include<iostream>
#define MAX 10
using namespace std;
typedef struct edge
{
int src;
int dest;
int wt;
}edge;
void bellman_ford(int nv,edge e[],int src_graph,int ne)
{
int u,v,weight,i,j=0;
int dis[MAX];
for(i=0;i<nv;i++)
{
dis[i]=999;
}
dis[src_graph]=0;
for(i=0;i<nv-1;i++)
{
for(j=0;j<ne;j++)
{
u=e[j].src;
v=e[j].dest;
weight=e[j].wt;
if(dis[u]!=999 && dis[u]+weight < dis[v])
{
dis[v]=dis[u]+weight;
}
}
}
for(j=0;j<ne;j++)
{
u=e[j].src;
v=e[j].dest;
weight=e[j].wt;
if(dis[u]+weight < dis[v])
{
cout<<"\n\nNEGATIVE CYCLE PRESENT..!!\n";
return;
}
}
cout<<"\nVertex"<<" Distance from source";
for(i=1;i<=nv;i++)
{
cout<<"\n"<<i<<"\t"<<dis[i];
}
}
int main()
{
int nv,ne,src_graph;
edge e[MAX];
cout<<"Enter the number of vertices: ";
cin>>nv;
printf("Enter the source vertex of the graph: ");
cin>>src_graph;
cout<<"\nEnter no. of edges: ";
cin>>ne;
for(int i=0;i<ne;i++)
{
cout<<"\nFor edge "<<i+1<<"=>";
cout<<"\nEnter source vertex :";
cin>>e[i].src;
cout<<"Enter destination vertex :";
cin>>e[i].dest;
cout<<"Enter weight :";
cin>>e[i].wt;
}
bellman_ford(nv,e,src_graph,ne);
return 0;
}
Example 2: bellman ford algorithm
#include <vector>
#include <iostream>
using namespace std;
#define INF 99999;
struct Edge {
int u, v, w;
};
int V;
vector<vector<Edge>> adjList;
vector<vector<int>> adjMatrix;
int* shortestPath(int src) {
int dist[V];
for (int u = 0; u < V; ++u)
dist[u] = INF;
dist[src] = 0;
for (int i = 0; i < V - 1; ++i)
for (int u = 0; u < V; ++u)
for (Edge e : adjList[u])
if (dist[e.u] + e.w < dist[e.v])
dist[e.v] = dist[e.u] + e.w;
for (int u = 0; u < V; ++u)
for (Edge e : adjList[u])
if (dist[e.u] + e.w < dist[e.v])
throw std::runtime_error("negative loop detected")
return dist;
}
int* shortestPath(int src) {
int dist[V];
for (int u = 0; u < V; ++u)
dist[u] = INF;
dist[src] = 0;
for (int i = 0; i < V - 1; ++i)
for (int u = 0; u < V; ++u)
for (int v = 0; v < V; ++v)
if (dist[u] + adjMatrix[u][v] < dist[v])
dist[v] = dist[u] + adjMatrix[u][v];
for (int u = 0; u < V; ++u)
for (int v = 0; v < V; ++v)
if (dist[u] + adjMatrix[u][v] < dist[v])
throw std::runtime_error("negative loop detected")
return dist;
}