|
| 1 | +// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:collection'; |
| 6 | + |
| 7 | +/// Dijkstra's algorithm for single source shortest path. |
| 8 | +/// |
| 9 | +/// Adopted from https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm#Pseudocode |
| 10 | +/// |
| 11 | +/// Note that this is not an optimal implementation in that it uses a |
| 12 | +/// (Splay) Tree as the priority queue which takes O(log n) time to (fake) a |
| 13 | +/// decrease of priority whereas e.g. a fibonacci heap would have done it in |
| 14 | +/// (amortized) O(1). |
| 15 | +class DijkstrasAlgorithm<E> { |
| 16 | + Map<GraphNode<E>, int> dist = new Map<GraphNode<E>, int>(); |
| 17 | + Map<GraphNode<E>, GraphNode<E>> prev = new Map<GraphNode<E>, GraphNode<E>>(); |
| 18 | + |
| 19 | + DijkstrasAlgorithm(Iterable<GraphNode<E>> graphNodes, GraphNode<E> source, |
| 20 | + int Function(E, E) comparator, int Function(E, E) distance) { |
| 21 | + SplayTreeSet<GraphNode<E>> q = new SplayTreeSet<GraphNode<E>>((a, b) { |
| 22 | + int distA = dist[a]; |
| 23 | + int distB = dist[b]; |
| 24 | + |
| 25 | + int when0() { |
| 26 | + if (identical(a, b)) return 0; |
| 27 | + int result = comparator(a.node, b.node); |
| 28 | + if (result == 0) { |
| 29 | + throw "The nodes ${b.node} and ${a.node} are not the same but " |
| 30 | + "compares to the same. That's not allowed!"; |
| 31 | + } |
| 32 | + return result; |
| 33 | + } |
| 34 | + |
| 35 | + if (distA != null && distB == null) return -1; |
| 36 | + if (distA == null && distB != null) return 1; |
| 37 | + if (distA == null && distB == null) { |
| 38 | + return when0(); |
| 39 | + } |
| 40 | + if (distA < distB) return -1; |
| 41 | + if (distA > distB) return 1; |
| 42 | + return when0(); |
| 43 | + }); |
| 44 | + |
| 45 | + dist[source] = 0; |
| 46 | + int index = 0; |
| 47 | + for (GraphNode<E> g in graphNodes) { |
| 48 | + // dist and prev not set, we see "null" as "infinity" and "undefined". |
| 49 | + if (!q.add(g)) { |
| 50 | + throw "Couldn't add ${g.node} (index $index)."; |
| 51 | + } |
| 52 | + index++; |
| 53 | + } |
| 54 | + |
| 55 | + while (q.isNotEmpty) { |
| 56 | + GraphNode<E> u = q.first; |
| 57 | + int distToU = dist[u]; |
| 58 | + if (distToU == null) { |
| 59 | + // No path to any of the remaining ${q.length} nodes. |
| 60 | + break; |
| 61 | + } |
| 62 | + q.remove(u); |
| 63 | + for (GraphNode<E> v in u.outgoing) { |
| 64 | + // Wikipedia says "only v that are still in Q" but it shouldn't matter |
| 65 | + // --- the length via u would be longer. |
| 66 | + int distanceUToV = distance(u.node, v.node); |
| 67 | + if (distanceUToV < 0) throw "Got negative distance. That's not allowed"; |
| 68 | + int alt = distToU + distanceUToV; |
| 69 | + int distToV = dist[v]; |
| 70 | + if (distToV == null || alt < distToV) { |
| 71 | + // Decrease length (decrease priority in priority queue). |
| 72 | + q.remove(v); |
| 73 | + dist[v] = alt; |
| 74 | + prev[v] = u; |
| 75 | + q.add(v); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + List<E> getPathFromTarget(GraphNode<E> source, GraphNode<E> target) { |
| 82 | + List<E> path = new List<E>(); |
| 83 | + GraphNode<E> u = target; |
| 84 | + while (u == source || prev[u] != null) { |
| 85 | + path.add(u.node); |
| 86 | + u = prev[u]; |
| 87 | + } |
| 88 | + return path.reversed.toList(); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +class GraphNode<E> { |
| 93 | + final E node; |
| 94 | + final Set<GraphNode<E>> outgoing = new Set<GraphNode<E>>(); |
| 95 | + final Set<GraphNode<E>> incoming = new Set<GraphNode<E>>(); |
| 96 | + |
| 97 | + GraphNode(this.node); |
| 98 | + |
| 99 | + void addOutgoing(GraphNode<E> other) { |
| 100 | + if (outgoing.add(other)) { |
| 101 | + other.incoming.add(this); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + String toString() { |
| 106 | + return "GraphNode[$node]"; |
| 107 | + } |
| 108 | +} |
0 commit comments