From 3b43753fc9080c73bef0b19bf0417ea8c891bc55 Mon Sep 17 00:00:00 2001 From: Shyamal Vaderia Date: Thu, 27 Jun 2024 11:21:59 -0400 Subject: [PATCH 1/2] Added more information in finding negative cycle using Bellman-Ford The information is taken from the [Bellman-Ford](https://cp-algorithms.com/graph/bellman_ford.html) page. I felt it should be present here as well to make this article self-sufficient. Also modified the code to remove the redundant check since `d` is initialised to `0` instead of `INF` so the check is not required. --- src/graph/finding-negative-cycle-in-graph.md | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/graph/finding-negative-cycle-in-graph.md b/src/graph/finding-negative-cycle-in-graph.md index 1c981ab99..479b491e7 100644 --- a/src/graph/finding-negative-cycle-in-graph.md +++ b/src/graph/finding-negative-cycle-in-graph.md @@ -19,6 +19,9 @@ Bellman-Ford algorithm allows you to check whether there exists a cycle of negat The details of the algorithm are described in the article on the [Bellman-Ford](bellman_ford.md) algorithm. Here we'll describe only its application to this problem. +The standard implementation of Bellman-Ford looks for a negative cycle reachable from some starting vertex $v$ ; however, the algorithm can be modified to just looking for any negative cycle in the graph. +For this we need to put all the distance  $d[i]$  to zero and not infinity — as if we are looking for the shortest path from all vertices simultaneously; the validity of the detection of a negative cycle is not affected. + Do $N$ iterations of Bellman-Ford algorithm. If there were no changes on the last iteration, there is no cycle of negative weight in the graph. Otherwise take a vertex the distance to which has changed, and go from it via its ancestors until a cycle is found. This cycle will be the desired cycle of negative weight. ### Implementation @@ -40,13 +43,11 @@ void solve() for (int i = 0; i < n; ++i) { x = -1; for (Edge e : edges) { - if(d[e.a] < INF){ - if (d[e.a] + e.cost < d[e.b]) { - d[e.b] = max(-INF, d[e.a] + e.cost); - p[e.b] = e.a; - x = e.b; - } - } + if (d[e.a] + e.cost < d[e.b]) { + d[e.b] = max(-INF, d[e.a] + e.cost); + p[e.b] = e.a; + x = e.b; + } } } From e6903c9d5073ca7397fd18374707c5b2ab3b9bf0 Mon Sep 17 00:00:00 2001 From: Oleksandr Kulkov Date: Thu, 11 Jul 2024 16:22:19 +0200 Subject: [PATCH 2/2] fix spacing --- src/graph/finding-negative-cycle-in-graph.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/graph/finding-negative-cycle-in-graph.md b/src/graph/finding-negative-cycle-in-graph.md index 479b491e7..63d5c464f 100644 --- a/src/graph/finding-negative-cycle-in-graph.md +++ b/src/graph/finding-negative-cycle-in-graph.md @@ -43,11 +43,11 @@ void solve() 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] = max(-INF, d[e.a] + e.cost); - p[e.b] = e.a; - x = e.b; - } + if (d[e.a] + e.cost < d[e.b]) { + d[e.b] = max(-INF, d[e.a] + e.cost); + p[e.b] = e.a; + x = e.b; + } } }