# Finding negative cycles using the Bellman-Ford algorithm and a source node

I'm exploring the Bellman-Ford algorithm to detect and track negative cycles (a collection of ncycle that we can see in the implementation). I'm wondering if the algorithm finds the same collection regardless of the source node when we can reach any node from any other node.

• What do you mean by the "set" of negative cycles that Bellman-Ford finds? I suggest you edit your question to make it more precise. There might be a ton of negative cycles, and any particular execution of Bellman-Ford might reveal only one of those cycles.
– D.W.
Commented Apr 30 at 7:19
• @D.W. I added the algorithm I refer to.
– atos
Commented Apr 30 at 7:46
• I know what the Bellman-Ford algorithm is. That doesn't help. What is the motivation for this question? In what context did you encounter it? I don't understand what you mean by "a collection of ncycle" - perhaps you are referring to a specific implementation, but some context seems to be missing.
– D.W.
Commented Apr 30 at 7:47
• Have you checked the link? It describes said implementation (along with ncycle we get for every edge).
– atos
Commented Apr 30 at 9:25
• I believe there might be some confusion here. ncycle is simply a linked list (presumably, or any ordered set of elements) that contains all vertices of some negative-weight cycle in the graph (i.e. a cycle where the sum of all weights is below $0$). It is not the set of all negative-weight cycles in the graph. Commented Apr 30 at 10:54

Your question is unclear since it is not explicit what a "collection of ncycle" means. I believe you are asking whether the Wikipedia implementation of the Bellman-Ford algorithm finds the same negative-weight cycle (assuming there is one) regardless of which source vertex we pick in the final depth-first search (DFS) when the input graph is strongly connected (i.e. for any pair of vertices $$u,v$$, there exist directed paths from $$u$$ to $$v$$ and $$v$$ to $$u$$).
As you can observe, it is strongly connected; you can pick any pair of distinct vertices $$u,v$$ and find a path from $$u$$ to $$v$$. Yet, picking vertex 1 as a source for the final negative-weight cycle detection yields the cycle 1, 2, 3, while picking vertex 4 as a source yields 4, 5, 6.