-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstra.ts
More file actions
131 lines (119 loc) · 3.28 KB
/
dijkstra.ts
File metadata and controls
131 lines (119 loc) · 3.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import {
EdgeDef,
PriorityQueue,
ShortestPath,
ShortestPathAlgo,
} from "./interfaces";
export class HeapNode {
constructor(
public node: number,
public distance: number,
) {}
}
export class DijkstraShortestPath<T, E extends EdgeDef<T>>
implements ShortestPathAlgo<T, E>
{
private readonly nodeToIndex = new Map<T, number>();
private readonly nodes: T[] = [];
private readonly adjacencyList: { edge: E; node: number }[][] = [];
constructor(
public edges: E[],
public priorityQueueFactory: () => PriorityQueue<HeapNode>,
public readonly INF = 1e9,
) {
const nodeSet = new Set(edges.flatMap((e) => [e.from, e.to]));
let index = 0;
for (const node of nodeSet) {
this.nodes[index] = node;
this.nodeToIndex.set(node, index);
this.adjacencyList[index] = [];
index++;
}
for (const edge of edges) {
const fromId = this.nodeToIndex.get(edge.from);
if (fromId == null) {
throw new Error();
}
const toId = this.nodeToIndex.get(edge.to);
if (toId == null) {
throw new Error();
}
this.adjacencyList[fromId].push({
edge,
node: toId,
});
}
}
calculate(
srcNode: T,
dstNode: T,
distFn: (edge: E) => number = (e) => e.distance,
): ShortestPath<T> {
const src = this.nodeToIndex.get(srcNode)!;
if (src == null) {
throw new Error(`Invalid source node ${srcNode}`);
}
const dst = this.nodeToIndex.get(dstNode)!;
if (dst == null) {
throw new Error(`Invalid destination node ${dstNode}`);
}
const nodesPQ = this.priorityQueueFactory();
const distances: number[] = [];
const previous: number[] = [];
for (let node = 0; node < this.adjacencyList.length; node++) {
distances[node] = node === src ? 0 : this.INF;
nodesPQ.push(new HeapNode(node, distances[node]));
previous[node] = -1;
}
let node = -1;
// as long as there is something to visit
while (nodesPQ.size > 0) {
node = nodesPQ.pop()!.node;
if (node === dst) {
break;
}
if (distances[node] === this.INF) {
// not reachable
continue;
}
for (const neighbor of this.adjacencyList[node]) {
// calculate new distance to neighboring node
const dist = distFn(neighbor.edge);
if (dist === this.INF) {
// essentially disconnected
continue;
}
if (dist < 0) {
throw new Error(
`Negative distance ${dist} for edge ${neighbor.edge.from} -> ${neighbor.edge.to}`,
);
}
const candidate = distances[node] + dist;
if (candidate < distances[neighbor.node]) {
distances[neighbor.node] = candidate;
previous[neighbor.node] = node;
nodesPQ.push(new HeapNode(neighbor.node, candidate));
}
}
}
if (distances[dst] === this.INF) {
return {
path: [],
distance: this.INF,
};
}
return {
path: this.buildPathTo(dst, previous),
distance: distances[dst],
};
}
private buildPathTo(node: number, previous: number[]) {
const path: T[] = [];
while (node !== -1) {
path.push(this.nodes[node]);
node = previous[node]!;
}
path.reverse();
return path;
}
}