(t_vertex source)
| 23 | Map<t_vertex, Pair<t_vertex, t_edge>> previous = new HashMap<>(); |
| 24 | |
| 25 | public void computePaths(t_vertex source) { |
| 26 | minDistance.put(source, 0f); |
| 27 | PriorityQueue<t_vertex> vertexQueue = new PriorityQueue<>((o1, o2) -> Float.compare(minDistance.computeIfAbsent(o1, (k) -> Float.POSITIVE_INFINITY), minDistance.computeIfAbsent(o2, (k) -> Float.POSITIVE_INFINITY))); |
| 28 | |
| 29 | vertexQueue.add(source); |
| 30 | |
| 31 | while (!vertexQueue.isEmpty()) { |
| 32 | t_vertex u = vertexQueue.poll(); |
| 33 | for (t_edge e : edgesFor.apply(u)) { |
| 34 | t_vertex v = output.apply(e); |
| 35 | double weight = length.apply(e).doubleValue(); |
| 36 | double min = minDistance.computeIfAbsent(u, (k) -> Float.POSITIVE_INFINITY); |
| 37 | double distanceThroughU = min + weight; |
| 38 | if (distanceThroughU < minDistance.computeIfAbsent(v, (k) -> Float.POSITIVE_INFINITY)) { |
| 39 | vertexQueue.remove(v); |
| 40 | minDistance.put(v, (float) distanceThroughU); |
| 41 | previous.put(v, new Pair<>(u, e)); |
| 42 | vertexQueue.add(v); |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | public List<Pair<t_vertex, t_edge>> getShortestPathTo(t_vertex target) { |
| 49 | if (!previous.containsKey(target)) return null; |
no test coverage detected