Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Source code:
package
common.bellmanford;
import java.util.LinkedList;
import java.util.Stack;
/**
* Simple test
*/
public static void main(String[] args) {
final EdgeWeightedDigraph graph = new
EdgeWeightedDigraph(3);
graph.addEdge(new DirectedEdge(0, 1,
2));
graph.addEdge(new DirectedEdge(1, 2, -
1));
graph.addEdge(new DirectedEdge(2, 1, -
3));
System.out.println(bf.negativeCycle());
}
/**
* Computes a shortest paths tree from
<tt>s</tt> to every other vertex in
* the edge-weighted digraph <tt>G</tt>.
* @param G the acyclic digraph
* @param s the source vertex
* @throws IllegalArgumentException unless
0 ≤ <tt>s</tt> ≤ <tt>V</tt> - 1
*/
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 LinkedList<Integer>();
queue.add(s);
onQueue[s] = true;
while (!queue.isEmpty() &&
!hasNegativeCycle()) {
int v = queue.remove();
onQueue[v] = false;
relax(G, v);
}
/**
* Is there a negative cycle reachable from
the source vertex <tt>s</tt>?
* @return <tt>true</tt> if there is a
negative cycle reachable from the
* source vertex <tt>s</tt>, and
<tt>false</tt> otherwise
*/
public boolean hasNegativeCycle() {
return cycle != null;
}
/**
* Returns a negative cycle reachable from
the source vertex <tt>s</tt>, or <tt>null</tt>
* if there is no such cycle.
* @return a negative cycle reachable from
the soruce vertex <tt>s</tt>
* as an iterable of edges, and
<tt>null</tt> if there is no such cycle
*/
public Iterable<DirectedEdge>
negativeCycle() {
return cycle;
}
/**
* Returns the length of a shortest path
from the source vertex <tt>s</tt> to vertex
<tt>v</tt>.
* @param v the destination vertex
* @return the length of a shortest path
from the source vertex <tt>s</tt> to vertex
<tt>v</tt>;
* <tt>Double.POSITIVE_INFINITY</tt> if
no such path
* @throws UnsupportedOperationException if
there is a negative cost cycle reachable
* from the source vertex <tt>s</tt>
*/
public double distTo(int v) {
if (hasNegativeCycle())
throw new
UnsupportedOperationException("Negative cost
cycle exists");
return distTo[v];
}
/**
* Is there a path from the source
<tt>s</tt> to vertex <tt>v</tt>?
* @param v the destination vertex
* @return <tt>true</tt> if there is a path
from the source vertex
* <tt>s</tt> to vertex <tt>v</tt>, and
<tt>false</tt> otherwise
*/
public boolean hasPathTo(int v) {
return distTo[v] <
Double.POSITIVE_INFINITY;
}
/**
* Returns a shortest path from the source
<tt>s</tt> to vertex <tt>v</tt>.
* @param v the destination vertex
* @return a shortest path from the source
<tt>s</tt> to vertex <tt>v</tt>
* as an iterable of edges, and
<tt>null</tt> if no such path
* @throws UnsupportedOperationException if
there is a negative cost cycle reachable
* from the source vertex <tt>s</tt>
*/
public Iterable<DirectedEdge> pathTo(int v)
{
if (hasNegativeCycle())
throw new
UnsupportedOperationException("Negative cost
cycle exists");
if (!hasPathTo(v)) return null;
Stack<DirectedEdge> path = new
Stack<DirectedEdge>();
for (DirectedEdge e = edgeTo[v]; e !=
null; e = edgeTo[e.from()]) {
path.push(e);
}
return path;
}
}