-
Notifications
You must be signed in to change notification settings - Fork 0
/
Detecting Negative Weight Cycle (Bellman Ford).cpp
75 lines (59 loc) · 1.62 KB
/
Detecting Negative Weight Cycle (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
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--)
{
int V,e,i,j,x,y,w,flag=1;
cin>>V>>e;
vector<pair<int,int>> adj[V];
for(i=0;i<e;i++)
{
cin>>x>>y>>w;
adj[x].push_back({y,w});
}
int dist[V];
dist[0]=0;
for(i=1;i<V;i++)
dist[i]=INT_MAX;
for(i=0;i<V-1;i++) //Single source shortest path algorithm
{
for(j=0;j<V;j++)
{
for(auto k:adj[j])
{
int u=j;
int v=k.first;
int w=k.second;
if(dist[u]!=INT_MAX && dist[u] + w < dist[v])
{
dist[v]=dist[u]+w;
}
}
}
}
for(j=0;j<V;j++) //Detecting Negative Weight Cycle
{
for(auto k:adj[j])
{
int u=j;
int v=k.first;
int w=k.second;
if(dist[u]!=INT_MAX && dist[u] + w < dist[v])
{
flag=0;
break;
}
}
}
if(flag==0)
cout<<"1"<<endl;
else
cout<<"0"<<endl;
}
return 0; //Worst case time complexity O(n3) n=no. of vertex
} //Best case time complexity O(n2)
If Graph is DAG then we can reduce time complexity
1.Obtain topological Sort of the DAG using dfs and store it in array
2.Now apply only one pass of Bellman Ford by vertex order obtained in step 1