Skip to content

Commit e3304b7

Browse files
committed
🎨
+ Enhance Dijkstra typing
1 parent ce127ea commit e3304b7

File tree

2 files changed

+26
-31
lines changed

2 files changed

+26
-31
lines changed

src/main.ts

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,32 @@ export interface DijkstraOptions {
1313
* @param s Source node
1414
* @returns First, the distances from source node to considered node. Secondly, an array that traces downwards the shortest path from considered node to source node.
1515
*/
16-
export function Dijkstra<N extends node, G extends WeightedGraph<N>>(
17-
G: G,
18-
[s]: [N],
19-
): [Map<N, number>, Map<N, N>];
16+
export function Dijkstra<
17+
N extends node,
18+
G extends WeightedGraph<N> = WeightedGraph<N>,
19+
>(G: G, [s]: [N]): [Map<N, number>, Map<N, N>];
2020
/**
2121
* @description Generate the shortest paths from source node `s` to every nodes in graph `G`, considering options `O`.
2222
* @param G Source graph
2323
* @param s Source node
2424
* @param O Options for Dijkstra computing
2525
* @returns First, the distances from source node to considered node. Secondly, an array that traces downwards the shortest path from considered node to source node.
2626
*/
27-
export function Dijkstra<N extends node, G extends WeightedGraph<N>>(
28-
G: G,
29-
[s]: [N],
30-
O: DijkstraOptions,
31-
): [Map<N, number>, Map<N, N>];
27+
export function Dijkstra<
28+
N extends node,
29+
G extends WeightedGraph<N> = WeightedGraph<N>,
30+
>(G: G, [s]: [N], O: DijkstraOptions): [Map<N, number>, Map<N, N>];
3231
/**
3332
* @description Computes the shortest path from source node `s` to target node `t` on graph `G`.
3433
* @param G Source graph
3534
* @param s Source node
3635
* @param t Target node
3736
* @returns The shortest path from s to t.
3837
*/
39-
export function Dijkstra<N extends node, G extends WeightedGraph<N>>(
40-
G: G,
41-
[s, t]: [N, N],
42-
): path<N>;
38+
export function Dijkstra<
39+
N extends node,
40+
G extends WeightedGraph<N> = WeightedGraph<N>,
41+
>(G: G, [s, t]: [N, N]): path<N>;
4342
/**
4443
* @description Computes the shortest path from source node `s` to target node `t` on graph `G`, considering options `O`.
4544
* @param G Source graph
@@ -48,12 +47,14 @@ export function Dijkstra<N extends node, G extends WeightedGraph<N>>(
4847
* @param O Options for Dijkstra computing
4948
* @returns The shortest path from s to t.
5049
*/
51-
export function Dijkstra<N extends node, G extends WeightedGraph<N>>(
52-
G: G,
53-
[s, t]: [N, N],
54-
O: DijkstraOptions,
55-
): path<N>;
56-
export function Dijkstra<N extends node, G extends WeightedGraph<N>>(
50+
export function Dijkstra<
51+
N extends node,
52+
G extends WeightedGraph<N> = WeightedGraph<N>,
53+
>(G: G, [s, t]: [N, N], O: DijkstraOptions): path<N>;
54+
export function Dijkstra<
55+
N extends node,
56+
G extends WeightedGraph<N> = WeightedGraph<N>,
57+
>(
5758
G: G,
5859
[s, t]: [N, N] | [N],
5960
O?: DijkstraOptions,
@@ -71,24 +72,22 @@ export function Dijkstra<N extends node, G extends WeightedGraph<N>>(
7172
}
7273

7374
while (!Q.isEmpty()) {
74-
7575
const min = Q.extractMinimum()!; // Can't be null otherwise Q is empty
76-
76+
7777
QMapping.set(min.value!, null);
7878

7979
if (t !== undefined && min.value === t) break;
8080

81-
8281
for (const v of G.neighborsIterator(min.value!) ?? []) {
8382
/**@description New alternative distance found from min, from a + (a,b) instead of b */
84-
83+
8584
const alt = min.key + G.weight(min.value!, v);
8685

8786
if (O && alt > O.maxCumulWeight) continue;
8887

8988
if (alt < (dist.get(v) ?? Infinity)) {
9089
dist.set(v, alt);
91-
90+
9291
prev.set(v, min.value!);
9392
const vINode = QMapping.get(v);
9493
if (vINode != null) Q.decreaseKey(vINode, alt);

src/utils/Graph.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,15 +240,11 @@ export class WeightedGraph<N extends node = node> extends Graph<N> {
240240

241241
weight(n1: N, n2: N): number {
242242
if (!this.arc(n1, n2)) throw new Error("Invalid nodes");
243-
243+
244244
return this.weights.get(`${n1}-${n2}`)!;
245245
}
246246
}
247247

248-
export type KeyOfMap<M extends Map<unknown, unknown>> = M extends Map<
249-
infer K,
250-
unknown
251-
>
252-
? K
253-
: never;
248+
export type KeyOfMap<M extends Map<unknown, unknown>> =
249+
M extends Map<infer K, unknown> ? K : never;
254250
export type unpackGraphNode<G> = G extends Graph ? KeyOfMap<G["adj"]> : never;

0 commit comments

Comments
 (0)