1
+ /* Program to detect whether a directed graph has negative cycle or not
2
+
3
+ We are given a directed graph. We need to detect whether the graph has
4
+ a negative cycle or not. A negative cycle is one in which the overall
5
+ sum of the cycle becomes negative.
6
+
7
+ In this program, Bellman Ford algorithm is used to detect negative cycle
8
+ in a graph.
9
+
10
+ Bellman Ford algorithm is a algorithm which help us to find the shortest
11
+ path from a starting vertex to all other vertices of a weighted graph.
12
+
13
+ */
14
+
15
+
16
+ #include < iostream>
17
+ #include < vector>
18
+ #include < algorithm>
19
+ #include < climits>
20
+ #include < string>
21
+ using namespace std ;
22
+
23
+ // Define infinity as the maximum value of the integer
24
+ #define INF INT_MAX
25
+ #define ne 4
26
+
27
+ // Data structure to store a graph edge
28
+ struct Edge
29
+ {
30
+ // edge from 'source' to 'dest' having a weight equal to 'weight'
31
+ int source, dest, weight;
32
+ };
33
+
34
+ // This is a function to run the Bellman Ford algorithm
35
+ bool BellmanFord (vector<Edge> const &edges, int source)
36
+ {
37
+ // cost[] stores shortest path information
38
+ int cost[ne];
39
+
40
+ // Initially, all vertices except the source vertex weight infinity
41
+ fill_n (cost, ne, INF);
42
+ cost[source] = 0 ;
43
+
44
+ int u, v, w, k = ne;
45
+
46
+ // Relaxation step (it will run 'V-1' times)
47
+ while (--k)
48
+ {
49
+ for (Edge edge: edges)
50
+ {
51
+ // edge from 'u' to 'v' having weight 'w'
52
+ u = edge.source ;
53
+ v = edge.dest ;
54
+ w = edge.weight ;
55
+
56
+ // if the cost to destination 'u' can be
57
+ // shortened by taking edge 'u' -> 'v'
58
+ if (cost[u] != INF && cost[u] + w < cost[v])
59
+ {
60
+ // update cost to the new lower value
61
+ cost[v] = cost[u] + w;
62
+ }
63
+ }
64
+ }
65
+
66
+ // Run relaxation step once more for ne'th time to
67
+ // check for negative-weight cycles
68
+ vector<int > cycle;
69
+ for (Edge edge: edges)
70
+ {
71
+ // edge from 'u' to 'v' having weight 'w'
72
+ u = edge.source ;
73
+ v = edge.dest ;
74
+ w = edge.weight ;
75
+
76
+ // if the cost to destination 'u' can be
77
+ // shortened by taking edge 'u' -> 'v'
78
+
79
+ if (cost[u] != INF && cost[u] + w < cost[v]) {
80
+ return true ;
81
+ }
82
+ }
83
+
84
+ return false ;
85
+ }
86
+
87
+ int main ()
88
+ {
89
+
90
+ // given adjacency representation of the matrix
91
+ int adjMatrix[ne][ne] =
92
+ {
93
+
94
+ { 0 , INF, -2 , INF },
95
+ { 4 , 0 , -3 , INF },
96
+ { INF, INF, 0 , 2 },
97
+ { INF, -1 , INF, 0 }
98
+ };
99
+
100
+ // create a vector to store graph edges
101
+ vector<Edge> edges;
102
+
103
+ for (int v = 0 ; v < ne; v++)
104
+ {
105
+ for (int u = 0 ; u < ne; u++)
106
+ {
107
+ if (adjMatrix[v][u] && adjMatrix[v][u] != INF) {
108
+ edges.push_back ({v, u, adjMatrix[v][u]});
109
+ }
110
+ }
111
+ }
112
+
113
+
114
+ int i;
115
+ for (i = 0 ; i < ne; i++)
116
+ {
117
+ // run Bellman Ford algorithm from each vertex as the source
118
+ // and check for any negative-weight cycle
119
+ if (BellmanFord (edges, i))
120
+ {
121
+ cout << " Negative-weight cycle is found!" ;
122
+ break ;
123
+ }
124
+ }
125
+
126
+ // condition if the graph doesn't contain negative cycle
127
+ if (i == ne)
128
+ cout << " Negative-weight cycle doesn't exist." ;
129
+
130
+ return 0 ;
131
+ }
132
+
133
+ /*
134
+
135
+ Example Input -
136
+
137
+ Adjacency Matrix:
138
+
139
+ { 0, 1, INF, INF, INF},
140
+ {INF, 0, 2, INF, INF},
141
+ {INF, INF, 0, 3, INF},
142
+ {INF, INF, INF, 0, -3}
143
+ {INF, -3, INF, INF, 0}
144
+
145
+ Example Output -
146
+
147
+ Negative-weight cycle is found!
148
+
149
+ Example Input -
150
+
151
+ Adjacency Matrix:
152
+
153
+ { 0, 4, INF, 5},
154
+ {INF, 0, INF, INF},
155
+ {INF, -10, 0, INF},
156
+ {INF, INF, 3, 0}
157
+
158
+ Example Output -
159
+
160
+ Negative-weight cycle doesn't exist.*/
0 commit comments