-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathDijkstraAlgorithm.java
More file actions
52 lines (43 loc) · 2.24 KB
/
DijkstraAlgorithm.java
File metadata and controls
52 lines (43 loc) · 2.24 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
42
43
44
45
46
47
48
49
50
51
52
package org.psjava.algo;
import org.psjava.algo.graph.shortestpath.SingleSourceShortestPathCalcStatus;
import org.psjava.ds.AddToMapWithSameValue;
import org.psjava.ds.graph.DirectedEdge;
import org.psjava.ds.graph.Graph;
import org.psjava.ds.heap.Heap;
import org.psjava.ds.heap.HeapFactory;
import org.psjava.ds.heap.HeapNode;
import org.psjava.ds.map.MutableMap;
import org.psjava.ds.map.MutableMapFactory;
import org.psjava.ds.numbersystrem.InfinitableAddableNumberSystem;
import java.util.Comparator;
import java.util.function.Function;
public class DijkstraAlgorithm {
public static SingleSourceShortestPathAlgorithm getInstance(final HeapFactory heapFactory, final MutableMapFactory mapFactory) {
return SingleSourceShortestPathAlgorithmFactory.create(new SingleSourceShortestPathAlgorithmMaterial() {
@Override
public <V, W, E extends DirectedEdge<V>> SingleSourceShortestPathCalcStatus<V, W, E> calc(Graph<V, E> graph, V start, Function<E, W> weightFunction, final InfinitableAddableNumberSystem<W> ns) {
final SingleSourceShortestPathCalcStatus<V, W, E> status = new SingleSourceShortestPathCalcStatus<V, W, E>();
AddToMapWithSameValue.add(status.distance, graph.getVertices(), ns.getInfinity());
status.distance.replace(start, ns.getZero());
Heap<V> heap = heapFactory.create(new Comparator<V>() {
@Override
public int compare(V v1, V v2) {
return ns.compare(status.distance.get(v1), status.distance.get(v2));
}
});
MutableMap<V, HeapNode<V>> node = mapFactory.create();
for (V v : graph.getVertices())
node.add(v, heap.insert(v));
while (!heap.isEmpty()) {
V current = heap.extractMinimum();
for (E edge : graph.getEdges(current)) {
boolean relaxed = Relax.relax(status.distance, status.previous, edge, weightFunction, ns);
if (relaxed)
node.get(edge.to()).decreaseKey(edge.to());
}
}
return status;
}
});
}
}