/****************************************************************************** * Compilation: javac AcyclicSP.java * Execution: java AcyclicSP V E * Dependencies: EdgeWeightedDigraph.java DirectedEdge.java Topological.java * Data files: https://algs4.cs.princeton.edu/44sp/tinyEWDAG.txt * * Computes shortest paths in an edge-weighted acyclic digraph. * * % java AcyclicSP tinyEWDAG.txt 5 * 5 to 0 (0.73) 5->4 0.35 4->0 0.38 * 5 to 1 (0.32) 5->1 0.32 * 5 to 2 (0.62) 5->7 0.28 7->2 0.34 * 5 to 3 (0.61) 5->1 0.32 1->3 0.29 * 5 to 4 (0.35) 5->4 0.35 * 5 to 5 (0.00) * 5 to 6 (1.13) 5->1 0.32 1->3 0.29 3->6 0.52 * 5 to 7 (0.28) 5->7 0.28 * ******************************************************************************/ /** * The {@code AcyclicSP} class represents a data type for solving the * single-source shortest paths problem in edge-weighted directed acyclic * graphs (DAGs). The edge weights can be positive, negative, or zero. *
* This implementation uses a topological-sort based algorithm. * The constructor takes Θ(V + E) time in the * worst case, where V is the number of vertices and * E is the number of edges. * Each instance method takes Θ(1) time. * It uses Θ(V) extra space (not including the * edge-weighted digraph). *
* This correctly computes shortest paths if all arithmetic performed is * without floating-point rounding error or arithmetic overflow. * This is the case if all edge weights are integers and if none of the * intermediate results exceeds 252. Since all intermediate * results are sums of edge weights, they are bounded by V C, * where V is the number of vertices and C is the maximum * absolute value of any edge weight. *
* For additional documentation,
* see Section 4.4 of
* Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class AcyclicSP {
private double[] distTo; // distTo[v] = distance of shortest s->v path
private DirectedEdge[] edgeTo; // edgeTo[v] = last edge on shortest s->v path
/**
* Computes a shortest paths tree from {@code s} to every other vertex in
* the directed acyclic graph {@code G}.
* @param G the acyclic digraph
* @param s the source vertex
* @throws IllegalArgumentException if the digraph is not acyclic
* @throws IllegalArgumentException unless {@code 0 <= s < V}
*/
public AcyclicSP(EdgeWeightedDigraph G, int s) {
distTo = new double[G.V()];
edgeTo = new DirectedEdge[G.V()];
validateVertex(s);
for (int v = 0; v < G.V(); v++)
distTo[v] = Double.POSITIVE_INFINITY;
distTo[s] = 0.0;
// visit vertices in topological order
Topological topological = new Topological(G);
if (!topological.hasOrder())
throw new IllegalArgumentException("Digraph is not acyclic.");
for (int v : topological.order()) {
for (DirectedEdge e : G.adj(v))
relax(e);
}
}
// relax edge e
private void relax(DirectedEdge e) {
int v = e.from(), w = e.to();
if (distTo[w] > distTo[v] + e.weight()) {
distTo[w] = distTo[v] + e.weight();
edgeTo[w] = e;
}
}
/**
* Returns the length of a shortest path from the source vertex {@code s} to vertex {@code v}.
* @param v the destination vertex
* @return the length of a shortest path from the source vertex {@code s} to vertex {@code v};
* {@code Double.POSITIVE_INFINITY} if no such path
* @throws IllegalArgumentException unless {@code 0 <= v < V}
*/
public double distTo(int v) {
validateVertex(v);
return distTo[v];
}
/**
* Is there a path from the source vertex {@code s} to vertex {@code v}?
* @param v the destination vertex
* @return {@code true} if there is a path from the source vertex
* {@code s} to vertex {@code v}, and {@code false} otherwise
* @throws IllegalArgumentException unless {@code 0 <= v < V}
*/
public boolean hasPathTo(int v) {
validateVertex(v);
return distTo[v] < Double.POSITIVE_INFINITY;
}
/**
* Returns a shortest path from the source vertex {@code s} to vertex {@code v}.
* @param v the destination vertex
* @return a shortest path from the source vertex {@code s} to vertex {@code v}
* as an iterable of edges, and {@code null} if no such path
* @throws IllegalArgumentException unless {@code 0 <= v < V}
*/
public Iterable