Skip to content

Commit 752c2c1

Browse files
committed
✨ introduce a better interface for Edge
1 parent c67b845 commit 752c2c1

File tree

5 files changed

+23
-11
lines changed

5 files changed

+23
-11
lines changed

src/graph/dijkstra.bench.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ const edges = generateSampleData(N, EDGE_PROB);
3939

4040
let result: any;
4141
// warmup
42-
const spa = createSPA(edges);
42+
const spa = createSPA(
43+
edges.map((e) => ({ from: e[0], to: e[1], distance: e[2] })),
44+
);
4345
for (let i = 0; i < ITERS; i++) {
4446
const a = Math.floor(Math.random() * N);
4547
const b = Math.floor(Math.random() * N);

src/graph/dijkstra.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ const INF = 1e9;
55

66
const createSPAlgoUndirected = (edges: any[]) => {
77
return new DijkstraShortestPath(
8-
edges,
8+
edges.flatMap((e) => [
9+
{ from: e[0], to: e[1], distance: e[2] },
10+
{ from: e[1], to: e[0], distance: e[2] },
11+
]),
912
() => new mnemonist.Heap<HeapNode>((a, b) => a.distance - b.distance),
1013
INF,
1114
);
1215
};
1316

1417
const createSPAlgoDirected = (edges: any[]) => {
1518
return new DijkstraShortestPath(
16-
edges,
19+
edges.map((e) => ({ from: e[0], to: e[1], distance: e[2] })),
1720
() => new mnemonist.Heap<HeapNode>((a, b) => a.distance - b.distance),
1821
INF,
1922
);

src/graph/dijkstra.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
Edge,
2+
EdgeDef,
33
PriorityQueue,
44
ShortestPath,
55
ShortestPathAlgo,
@@ -12,7 +12,7 @@ export class HeapNode {
1212
) {}
1313
}
1414

15-
export class DijkstraShortestPath<T, E extends Edge<T>>
15+
export class DijkstraShortestPath<T, E extends EdgeDef<T>>
1616
implements ShortestPathAlgo<T, E>
1717
{
1818
private readonly nodeToIndex = new Map<T, number>();
@@ -24,7 +24,7 @@ export class DijkstraShortestPath<T, E extends Edge<T>>
2424
public priorityQueueFactory: () => PriorityQueue<HeapNode>,
2525
public readonly INF = 1e9,
2626
) {
27-
const nodeSet = new Set(edges.flatMap((e) => [e[0], e[1]]));
27+
const nodeSet = new Set(edges.flatMap((e) => [e.from, e.to]));
2828
let index = 0;
2929
for (const node of nodeSet) {
3030
this.nodes[index] = node;
@@ -33,11 +33,11 @@ export class DijkstraShortestPath<T, E extends Edge<T>>
3333
index++;
3434
}
3535
for (const edge of edges) {
36-
const fromId = this.nodeToIndex.get(edge[0]);
36+
const fromId = this.nodeToIndex.get(edge.from);
3737
if (fromId == null) {
3838
throw new Error();
3939
}
40-
const toId = this.nodeToIndex.get(edge[1]);
40+
const toId = this.nodeToIndex.get(edge.to);
4141
if (toId == null) {
4242
throw new Error();
4343
}
@@ -51,7 +51,7 @@ export class DijkstraShortestPath<T, E extends Edge<T>>
5151
calculate(
5252
srcNode: T,
5353
dstNode: T,
54-
distFn: (edge: E) => number = (e) => e[2],
54+
distFn: (edge: E) => number = (e) => e.distance,
5555
): ShortestPath<T> {
5656
const src = this.nodeToIndex.get(srcNode)!;
5757
if (src == null) {
@@ -94,7 +94,7 @@ export class DijkstraShortestPath<T, E extends Edge<T>>
9494
}
9595
if (dist < 0) {
9696
throw new Error(
97-
`Negative distance ${dist} for edge ${neighbor.edge[0]} -> ${neighbor.edge[1]}`,
97+
`Negative distance ${dist} for edge ${neighbor.edge.from} -> ${neighbor.edge.to}`,
9898
);
9999
}
100100
const candidate = distances[node] + dist;

src/graph/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export {
55
} from "./floyd-warshal";
66
export {
77
Edge,
8+
EdgeDef,
89
TransitiveClosure,
910
INF,
1011
PriorityQueue,

src/graph/interfaces.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
// [parent, child, distance]
55
export type Edge<T> = [T, T, number];
66

7+
export interface EdgeDef<T> {
8+
from: T;
9+
to: T;
10+
distance: number;
11+
}
12+
713
export type TransitiveClosure<T> = {
814
parent: T;
915
child: T;
@@ -24,7 +30,7 @@ export interface ShortestPath<T> {
2430
distance: number;
2531
}
2632

27-
export interface ShortestPathAlgo<T, E extends Edge<T>> {
33+
export interface ShortestPathAlgo<T, E extends EdgeDef<T>> {
2834
calculate(
2935
srcNode: T,
3036
dstNode: T,

0 commit comments

Comments
 (0)