//Bellman Ford Algorithm #include using namespace std; bool bellman_ford(vector>graph,int V,int s,vector&dist) { //no. of edges int n = graph.size(); for(int i=0;i(graph[j]); int dest = get<1>(graph[j]); int weight = get<2>(graph[j]); if(dist[src]!=INT_MAX && dist[dest]>dist[src]+weight) { dist[dest] = dist[src]+weight; } } } //The algorithm works fine with negative edges but does not work with negative weight cycles //thus checking for any negative weight cycles... for(int i=0;i(graph[i]); int dest = get<1>(graph[i]); int weight = get<2>(graph[i]); if(dist[src]!=INT_MAX && dist[dest]>dist[src]+weight) { cout<<"Negative weight cycle detected..\n"; return false; } } return true; } int main() { //no. of vertices(V) & source(s) int V = 6, s=0; //graph containing all the weighted edges.. vector>graph; //inserting an edge with : //src = 0 //dest = 1 // weight = 10 graph.push_back(make_tuple(0,1,10)); graph.push_back(make_tuple(0,2,8)); graph.push_back(make_tuple(1,4,2)); graph.push_back(make_tuple(2,3,1)); graph.push_back(make_tuple(3,1,-4)); graph.push_back(make_tuple(3,4,-1)); graph.push_back(make_tuple(4,5,-2)); graph.push_back(make_tuple(5,1,1)); //vector to store distances of all vertices from source(s) vectordist; bool neg = bellman_ford(graph,V,s,dist); //if no negative weigt cycle is found... if(neg) { cout<<"vertex Dist from Source\n"; for(int i=0;i