"bellman ford algorithm" Code Answer's

You're definitely familiar with the best coding language C++ that developers use to develop their projects and they get all their queries like "bellman ford algorithm" answered properly. Developers are finding an appropriate answer about bellman ford algorithm related to the C++ coding language. By visiting this online portal developers get answers concerning C++ codes question like bellman ford algorithm. Enter your desired code related query in the search bar and get every piece of information about C++ code related question on bellman ford algorithm. 

bellman ford code in c++

By Magnificent MarkhorMagnificent Markhor on Aug 26, 2020
#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];
  
  /* initializing array 'dis' with 999. 999 denotes infinite distance */
  for(i=0;i<nv;i++)
  {
    dis[i]=999;
  }
    
  /* distance of source vertex from source vertex is o */
  dis[src_graph]=0;
  
  /* relaxing all the edges nv - 1 times */
  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;
      }  
    }
    
  }
  
  /* checking if negative cycle is present */
  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;  
  
  /* if you enter no of vertices: 5 then vertices will be 1,2,3,4,5. so while giving input enter source and destination vertex accordingly */
  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;
}

Add Comment

1

bellman ford algorithm cp algorithm

By Careful CardinalCareful Cardinal on Jun 18, 2020
struct edge
{
    int a, b, cost;
};

int n, m, v;
vector<edge> e;
const int INF = 1000000000;

void solve()
{
    vector<int> d (n, INF);
    d[v] = 0;
    for (int i=0; i<n-1; ++i)
        for (int j=0; j<m; ++j)
            if (d[e[j].a] < INF)
                d[e[j].b] = min (d[e[j].b], d[e[j].a] + e[j].cost);
    // display d, for example, on the screen
}

Source: cp-algorithms.com

Add Comment

2

bellman ford algorithm

By Wrong WhaleWrong Whale on Apr 26, 2021
#include <vector>
#include <iostream>
using namespace std;

#define INF 99999;

struct Edge {
	int u, v, w;
};

int V; 							// Number of verticies.
vector<vector<Edge>> adjList;	// Adjacency list.
vector<vector<int>> adjMatrix;	// Adjacency matrix.

// With adjacency list.
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")
    
    // contains the shortest dist between src and each vertex.
  	return dist;
}

// With adjacency matrix.
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")
    
    // contains the shortest dist between src and each vertex.
  	return dist;
}

Add Comment

1

All those coders who are working on the C++ based application and are stuck on bellman ford algorithm can get a collection of related answers to their query. Programmers need to enter their query on bellman ford algorithm related to C++ code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about bellman ford algorithm for the programmers working on C++ code while coding their module. Coders are also allowed to rectify already present answers of bellman ford algorithm while working on the C++ language code. Developers can add up suggestions if they deem fit any other answer relating to "bellman ford algorithm". Visit this developer's friendly online web community, CodeProZone, and get your queries like bellman ford algorithm resolved professionally and stay updated to the latest C++ updates. 

C++ answers related to "bellman ford algorithm"

View All C++ queries

C++ queries related to "bellman ford algorithm"

Browse Other Code Languages

CodeProZone