-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAcyclicSP.java
More file actions
35 lines (30 loc) · 1.03 KB
/
Copy pathAcyclicSP.java
File metadata and controls
35 lines (30 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class AcyclicSP {
/* Get shortest path in an edge weighted digraph */
private DirectedEdge[] edgeTo;
private double[] distTo;
/* Constructor */
public AcyclicSP(EdgeWeightedDigraph G, int s) {
// initialise arrays
this.edgeTo = new DirectedEdge[G.V()];
this.distTo = new double[G.V()];
// initialise all initial distances to infinity
for (int v = 0; v < G.V(); v++)
distTo[v] = Double.POSITIVE_INFINITY;
distTo[s] = 0.0;
// consider all vertices in topologically sorted order
Topological topological = new Topological(G);
for (int v : topological.order())
for (DirectedEdge e : G.adj(v))
relax(e);
}
/* Helper: Relax a DirectedEdge */
private void relax(DirectedEdge e) {
// get vertices
int v = e.from(), w = e.to();
// relax
if (distTo[w] > distTo[v] + e.weight()) {
distTo[w] = distTo[v] + e.weight();
edgeTo[w] = e;
}
}
}