-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDijkstraSP.java
More file actions
41 lines (36 loc) · 1.28 KB
/
Copy pathDijkstraSP.java
File metadata and controls
41 lines (36 loc) · 1.28 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
36
37
38
39
40
41
public class DijkstraSP {
/* Find the shortest path from a source vertex to all
* other vertices in an Edge weighted Digraph */
private DirectedEdge[] edgeTo;
private double[] distTo;
private IndexMinPQ<Double> pq;
/* Constructor */
public void DijkstraSP(EdgeWeightedDigraph G, int s) {
this.edgeTo = new DirectedEdge[G.V()];
this.distTo = new double[G.V()];
this.pq = new IndexMinPQ<Double>(G.V());
// initialise initial distances
for (int v = 0; v < G.V(); v++)
distTo[v] = Double.POSITIVE_INFINITY;
distTo[s] = 0.0;
pq.insert(s, 0.0);
// PQ takes care of taking vertices in order of distance
while (!pq.isEmpty()) {
int v = pq.delMin();
// relax all outgoing edges from v
for (DirectedEdge e : G.adj(v))
relax(e);
}
}
/* Helper: Relax an edge */
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;
// update the changes in the PQ also
if (pq.contains(w)) pq.decreaseKey(w, distTo[w]);
else pq.insert(w, distTo[w]);
}
}
}