I was thinking of an algorithm for finding a negative weight cycle in a directed graph. The problem is that we have a graph G (V, E), we need to find an effective algorithm for finding a cycle with a negative weight. I understand the algorithm in this pdf
In short, the algorithm applies the Bellman Ford algorithm by iterating | V | -1 times, doing relaxation. After that, he checks if there is an edge that can even be softened more, then there is a negative weight cycle, and we can trace it back with parent pointers, and everything goes well, we find a negative weight cycle.
However, I was thinking of another algorithm for using deep search (DFS) in the graph, tracking the sum of distances so far covered, I mark all nodes white at the beginning and turn them gray when I search for the path and mark them black when they are finished, so I know that I find a loop if and only if I find a visited node and it is gray (in my path), and not black, which was already completed by Depth-First search, and therefore for my algorithm, if I get to gray node that has already been visited, I check that it will update (new distance), and if it is less than before, I know that I have a negative weight cycle and you can trace it.
Is my algorithm wrong? If so, can you find a counterexample? If you do not help me prove it?
thanks
Saher ahwal
source share