I know this is a bit of a reach, but I am following Princeton's course on Algorithm. I am trying to use Bellman Ford algorithm to detect negative cycle in edge weighted directed graph.
There is a negative cycle reachable from the source if and only if the queue is
nonempty after the Vth pass through all the edges. Moreover, the subgraph of
edges in our edgeTo[] array must contain a negative cycle.
The complete code implementation are available at : BellmanFordSP.java and EdgeWeightedDirectedCycle.java. Specifically, I'm stuck at this point :
public class BellmanFordSP
{
private double[] distTo; // distTo[v] = distance of shortest s->v path
private DirectedEdge[] edgeTo; // edgeTo[v] = last edge on shortest s->v path
private boolean[] onQueue; // onQueue[v] = is v currently on the queue?
private Queue<Integer> queue; // queue of vertices to relax
private int cost; // number of calls to relax()
private Iterable<DirectedEdge> cycle;// negative cycle (or null if no such cycle)
// Computes a shortest paths tree from s to every other vertex
public BellmanFordSP(EdgeWeightedDigraph G, int s)
{
distTo = new double[G.V()];
edgeTo = new DirectedEdge[G.V()];
onQueue = new boolean[G.V()];
for (int v = 0; v < G.V(); v++)
distTo[v] = Double.POSITIVE_INFINITY;
distTo[s] = 0.0;
// Bellman-Ford algorithm
queue = new Queue<Integer>();
queue.enqueue(s);
onQueue[s] = true;
while (!queue.isEmpty() && !hasNegativeCycle())
{
int v = queue.dequeue();
onQueue[v] = false;
relax(G, v);
}
}
// relax vertex v and put other endpoints on queue if changed
// G.V() gives number of vertices in G
// G.adj(v) returns an Iterable of edges emanating from vertex v.
private void relax(EdgeWeightedDigraph G, int v)
{
for (DirectedEdge e : G.adj(v))
{
int w = e.to();
if (distTo[w] > distTo[v] + e.weight())
{
distTo[w] = distTo[v] + e.weight();
edgeTo[w] = e;
if (!onQueue[w])
{
queue.enqueue(w);
onQueue[w] = true;
}
}
if (cost++ % G.V() == 0) // <-- what does this check do ?
findNegativeCycle();
}
}
// Is there a negative cycle reachable from the source vertex s?
public boolean hasNegativeCycle()
{
return cycle != null;
}
// Returns a negative cycle reachable from the source vertex s
public Iterable<DirectedEdge> negativeCycle()
{
return cycle;
}
// by finding a cycle in predecessor graph
private void findNegativeCycle()
{
int V = edgeTo.length;
EdgeWeightedDigraph spt = new EdgeWeightedDigraph(V);
for (int v = 0; v < V; v++)
if (edgeTo[v] != null)
spt.addEdge(edgeTo[v]);
EdgeWeightedDirectedCycle finder = new EdgeWeightedDirectedCycle(spt);
cycle = finder.cycle();
}
What does this condition signify : cost++ % G.V() == 0
.
Why are we checking for negative cycles on this specific condition only ?