-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiSP.java
68 lines (60 loc) · 2.08 KB
/
MultiSP.java
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
public class MultiSP
{
private DirectedEdge[] edgeTo;
private double[] distTo;
private Topological top;
private Iterable<Integer> source;
private Iterable<Integer> dests;
private int closest = -1;
public MultiSP(EdgeWeightedDigraph g, Iterable<Integer> s,
Iterable<Integer> d) {
source = s;
dests = d;
top = new Topological(g);
edgeTo = new DirectedEdge[g.V()];
distTo = new double[g.V()];
// for (int v : top.order())
// StdOut.printf("%d\t", v);
// StdOut.printf("\n");
for (int i = 0; i < g.V(); i++)
distTo[i] = Double.POSITIVE_INFINITY;
for (int i : source) distTo[i] = 0;
// distTo[s] = 0;
for (int v : top.order()) relax(g, v);
}
private void relax(EdgeWeightedDigraph g, int v) {
// StdOut.printf("relaxing vertex %d with dist %f\n", v, distTo[v]);
for (DirectedEdge e : g.adj(v)) {
int w = e.to();
// StdOut.printf("considering %d -> %d: dist %f, weight %f, new %f...",
// v, w, distTo[w], e.weight(), distTo[v] + e.weight());
if (distTo[w] > distTo[v] + e.weight()) {
// StdOut.printf("eligible\n",
// distTo[w], distTo[v], e.weight());
distTo[w] = distTo[v] + e.weight();
edgeTo[w] = e;
}
// else
// StdOut.printf("not eligible\n");
}
}
public int shortestDest() {
double dist = Double.POSITIVE_INFINITY;
for (int i : dests) {
if (distTo[i] < dist) {
dist = distTo[i];
closest = i;
}
}
return closest;
}
public Iterable<DirectedEdge> pathTo(int v) {
if (closest < 0) shortestDest();
Stack<DirectedEdge> path = new Stack<DirectedEdge>();
for (DirectedEdge e = edgeTo[v]; e != null; e = edgeTo[e.from()])
path.push(e);
return path;
}
public static void main(String[] args) {
}
}