|
| 1 | +// @deno-types="https://raw.githubusercontent.com/mourner/tinyqueue/v2.0.3/index.d.ts" |
| 2 | +import TinyQueue from "https://raw.githubusercontent.com/mourner/tinyqueue/v2.0.3/index.js"; |
| 3 | + |
| 4 | +/** |
| 5 | + * An edge. |
| 6 | + */ |
| 7 | +export interface IEdge { |
| 8 | + /** Destination node. */ |
| 9 | + readonly toNode: number; |
| 10 | + /** Weight of the path to the destination node. */ |
| 11 | + readonly weight: number; |
| 12 | +} |
| 13 | + |
| 14 | +interface IPath { |
| 15 | + readonly toNode: number; |
| 16 | + readonly weight: number; |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Implementation of Dijkstra's Shortest Path algorithm. This quickly finds the shortest path between a single point |
| 21 | + * and every other point it it connected to. |
| 22 | + * |
| 23 | + * Nodes are numbered from 0 to n-1. |
| 24 | + * |
| 25 | + * Adapted from https://medium.com/@adriennetjohnson/a-walkthrough-of-dijkstras-algorithm-in-javascript-e94b74192026 |
| 26 | + * This has been made much faster by treating nodes as an index rather than a string (name). We use `tinyqueue` |
| 27 | + * as our priority queue. All map-likes have been eliminated, but there are still object references. So this is |
| 28 | + * not as fast as possible, but it should be plenty fast and not too heavy on memory. |
| 29 | + */ |
| 30 | +export class DijkstraShortestPathSolver { |
| 31 | + private constructor( |
| 32 | + public readonly nodes: number, |
| 33 | + public readonly adjacencyList: IEdge[][], |
| 34 | + ) { |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Initialize a new empty solver with the number of nodes needed. |
| 39 | + * @param nodes The number of nodes in the graph. |
| 40 | + * @returns A new solver. |
| 41 | + */ |
| 42 | + static init(nodes: number): DijkstraShortestPathSolver { |
| 43 | + return new DijkstraShortestPathSolver( |
| 44 | + nodes, |
| 45 | + new Array(nodes).fill(null).map((_v) => new Array(0)), |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * A clone of this solver. |
| 51 | + * @returns A cloned solver. |
| 52 | + */ |
| 53 | + clone(): DijkstraShortestPathSolver { |
| 54 | + return new DijkstraShortestPathSolver( |
| 55 | + this.nodes, |
| 56 | + this.adjacencyList.map((a) => a.slice(0)), |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Add an edge (in one direction). |
| 62 | + * @param fromNode Starting node. |
| 63 | + * @param toNode Ending node. |
| 64 | + * @param weight Weight of the edge. Must be greater than 0. |
| 65 | + */ |
| 66 | + addEdge(fromNode: number, toNode: number, weight: number): void { |
| 67 | + if (weight < 0) { |
| 68 | + throw new RangeError("weight must be >= 0"); |
| 69 | + } |
| 70 | + |
| 71 | + if (fromNode < 0 || fromNode >= this.nodes) { |
| 72 | + throw new RangeError( |
| 73 | + `fromNode must be in range 0..${this.nodes - 1}: ${fromNode}`, |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + if (toNode < 0 || toNode >= this.nodes) { |
| 78 | + throw new RangeError( |
| 79 | + `toNode must be in range 0..${this.nodes - 1}: ${toNode}`, |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + this.adjacencyList[fromNode].push({ toNode, weight }); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Add an edge in both directions. |
| 88 | + * @param fromNode Starting node. |
| 89 | + * @param toNode Ending node. |
| 90 | + * @param weight Weight of the edge. Must be greater than 0. |
| 91 | + */ |
| 92 | + addBidirEdge(fromNode: number, toNode: number, weight: number): void { |
| 93 | + if (weight < 0) { |
| 94 | + throw new RangeError("weight must be >= 0"); |
| 95 | + } |
| 96 | + |
| 97 | + if (fromNode < 0 || fromNode >= this.nodes) { |
| 98 | + throw new RangeError( |
| 99 | + `fromNode must be in range 0..${this.nodes - 1}: ${fromNode}`, |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + if (toNode < 0 || toNode >= this.nodes) { |
| 104 | + throw new RangeError( |
| 105 | + `toNode must be in range 0..${this.nodes - 1}: ${toNode}`, |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + this.adjacencyList[fromNode].push({ toNode, weight }); |
| 110 | + this.adjacencyList[toNode].push({ toNode: fromNode, weight }); |
| 111 | + } |
| 112 | + |
| 113 | + setEdges(node: number, edges: IEdge[]): void { |
| 114 | + this.adjacencyList[node] = edges; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Calculate shortest paths for all nodes for the given start node. |
| 119 | + * @param startNode The start node. |
| 120 | + */ |
| 121 | + calculateFor(startNode: number): ShortestPaths { |
| 122 | + const weights: number[] = new Array(this.nodes).fill(Infinity); |
| 123 | + weights[startNode] = 0; |
| 124 | + |
| 125 | + const pq = new TinyQueue<IPath>( |
| 126 | + [{ toNode: startNode, weight: 0 }], |
| 127 | + (a, b) => a.weight - b.weight, |
| 128 | + ); |
| 129 | + |
| 130 | + const backtrace: number[] = new Array(this.nodes).fill(-1); |
| 131 | + |
| 132 | + while (pq.length !== 0) { |
| 133 | + const shortestStep = pq.pop(); |
| 134 | + if (shortestStep === undefined) { |
| 135 | + throw new Error("shortest-step undefined"); |
| 136 | + } |
| 137 | + const currentNode = shortestStep.toNode; |
| 138 | + |
| 139 | + this.adjacencyList[currentNode].forEach((neighbor) => { |
| 140 | + const weight = weights[currentNode] + neighbor.weight; |
| 141 | + |
| 142 | + if (weight < weights[neighbor.toNode]) { |
| 143 | + weights[neighbor.toNode] = weight; |
| 144 | + backtrace[neighbor.toNode] = currentNode; |
| 145 | + pq.push({ toNode: neighbor.toNode, weight: weight }); |
| 146 | + } |
| 147 | + }); |
| 148 | + } |
| 149 | + |
| 150 | + return new ShortestPaths(startNode, backtrace, weights); |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +/** |
| 155 | + * Shortest paths result. |
| 156 | + */ |
| 157 | +export class ShortestPaths { |
| 158 | + constructor( |
| 159 | + public readonly startNode: number, |
| 160 | + public readonly backtrace: number[], |
| 161 | + public readonly weights: number[], |
| 162 | + ) {} |
| 163 | + |
| 164 | + /** |
| 165 | + * Find the shortest path to the given end node. |
| 166 | + * @param endNode The end node. |
| 167 | + */ |
| 168 | + shortestPathTo(endNode: number): number[] { |
| 169 | + const path = [endNode]; |
| 170 | + let lastStep = endNode; |
| 171 | + |
| 172 | + while (lastStep != this.startNode) { |
| 173 | + path.unshift(this.backtrace[lastStep]); |
| 174 | + lastStep = this.backtrace[lastStep]; |
| 175 | + } |
| 176 | + |
| 177 | + return path; |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Total weight of the path from the start node to the given end node. |
| 182 | + * @param endNode The end node. |
| 183 | + */ |
| 184 | + totalWeight(endNode: number): number { |
| 185 | + return this.weights[endNode]; |
| 186 | + } |
| 187 | +} |
0 commit comments