-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bellman-ford.cpp
92 lines (73 loc) · 1.78 KB
/
bellman-ford.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//Bellman Ford Algorithm
#include<bits/stdc++.h>
using namespace std;
bool bellman_ford(vector<tuple<int,int,int>>graph,int V,int s,vector<int>&dist)
{
//no. of edges
int n = graph.size();
for(int i=0;i<V;i++)
{
dist.push_back(INT_MAX);
}
dist[s] = 0;
//V-1 times relaxation of all edges is done
for(int i=0;i<V-1;i++)
{
for(int j=0;j<n;j++)
{
int src = get<0>(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<n;i++)
{
int src = get<0>(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<tuple<int,int,int>>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)
vector<int>dist;
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<dist.size();i++)
{
cout<<i<<"\t\t\t"<<dist[i]<<"\n";
}
}
return 0;
}