I was exploring the algorithm for finding negative edge cycles in a Directed Graph using bellman-ford algorithm and came across this implementation in cp-algorithms.
struct Edge {
int a, b, cost;
};
int n, m;
vector<Edge> edges;
const int INF = 1000000000;
void solve()
{
vector<int> d(n);
vector<int> p(n, -1);
int x;
for (int i = 0; i < n; ++i) {
x = -1;
for (Edge e : edges) {
if (d[e.a] + e.cost < d[e.b]) {
d[e.b] = d[e.a] + e.cost;
p[e.b] = e.a;
x = e.b;
}
}
}
if (x == -1) {
cout << "No negative cycle found.";
} else {
for (int i = 0; i < n; ++i)
x = p[x];
vector<int> cycle;
for (int v = x;; v = p[v]) {
cycle.push_back(v);
if (v == x && cycle.size() > 1)
break;
}
reverse(cycle.begin(), cycle.end());
cout << "Negative cycle: ";
for (int v : cycle)
cout << v << ' ';
cout << endl;
}
}
Can someone tell me, what is the significance of this for-loop?
for (int i = 0; i < n; ++i)
x = p[x];
Ig, the algorithm could print the negative cycle correctly even without it, or am I missing something here?
Thanks in advance!