Skip to content

Commit 874971c

Browse files
committed
fix: function argument of type Graph now accept Graph with any LinkProps
1 parent 74a26d8 commit 874971c

7 files changed

Lines changed: 31 additions & 20 deletions

File tree

src/Graph.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ export class Graph<Node = string, LinkProps = never> {
157157
*/
158158
removeEdge(source: Node, target: Node): this {
159159
this.edges.get(source)?.delete(target);
160+
this.edgeProperties.get(source)?.delete(target);
160161

161162
return this;
162163
}

src/algorithms/lowestCommonAncestors/lowestCommonAncestors.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import { NoInfer } from '../../types.js';
77
* Inspired by https://github.com/relaxedws/lca/blob/master/src/LowestCommonAncestor.php code
88
* but uses depth search instead of breadth. Also uses some optimizations.
99
*/
10-
export function lowestCommonAncestors<Node>(
11-
graph: Graph<Node>,
10+
export function lowestCommonAncestors<Node, LinkProps>(
11+
graph: Graph<Node, LinkProps>,
1212
node1: NoInfer<Node>,
1313
node2: NoInfer<Node>,
1414
): Node[] {
@@ -22,8 +22,8 @@ export function lowestCommonAncestors<Node>(
2222
return lcas;
2323
}
2424

25-
function CA1Visit<Node>(
26-
graph: Graph<Node>,
25+
function CA1Visit<Node, LinkProps>(
26+
graph: Graph<Node, LinkProps>,
2727
node1Ancestors: Node[],
2828
lcas: Node[],
2929
visited: Set<Node>,
@@ -45,8 +45,8 @@ function CA1Visit<Node>(
4545
}
4646
}
4747

48-
function CA2Visit<Node>(
49-
graph: Graph<Node>,
48+
function CA2Visit<Node, LinkProps>(
49+
graph: Graph<Node, LinkProps>,
5050
node1Ancestors: Node[],
5151
lcas: Node[],
5252
visited: Set<Node>,

src/algorithms/shortestPath/dijkstra.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { extractMin } from './extractMin.js';
44
import { relax } from './relax.js';
55
import { TraversingTracks } from './types.js';
66

7-
export function dijkstra<Node>(
8-
graph: Graph<Node>,
7+
export function dijkstra<Node, LinkProps>(
8+
graph: Graph<Node, LinkProps>,
99
tracks: TraversingTracks<NoInfer<Node>>,
1010
source: NoInfer<Node>,
1111
destination: NoInfer<Node>,

src/algorithms/shortestPath/getPath.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import { Graph } from '../../Graph.js';
77
* Assembles the shortest path by traversing the
88
* predecessor subgraph from destination to source.
99
*/
10-
export function getPath<Node>(
11-
graph: Graph<Node>,
10+
export function getPath<Node, LinkProps>(
11+
graph: Graph<Node, LinkProps>,
1212
tracks: TraversingTracks<NoInfer<Node>>,
1313
source: NoInfer<Node>,
1414
destination: NoInfer<Node>,

src/algorithms/shortestPath/relax.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { NoInfer } from '../../types.js';
44
import { TraversingTracks } from './types.js';
55

66
export function relax<Node>(
7-
graph: Graph<Node>,
7+
graph: Graph<Node, any>,
88
tracks: TraversingTracks<NoInfer<Node>>,
99
source: NoInfer<Node>,
1010
target: NoInfer<Node>,

src/algorithms/shortestPath/shortestPath.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import { TraversingTracks } from './types.js';
99
* Cormen et al. "Introduction to Algorithms" 3rd Ed. p. 658
1010
* Variable and function names correspond to names in the book.
1111
*/
12-
export function shortestPath<Node>(
13-
graph: Graph<Node>,
12+
export function shortestPath<Node, LinkProps>(
13+
graph: Graph<Node, LinkProps>,
1414
source: NoInfer<Node>,
1515
destination: NoInfer<Node>,
1616
): {

src/algorithms/shortestPath/shortestPaths.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import type { NoInfer } from '../../types.js';
33
import { Graph } from '../../Graph.js';
44
import { shortestPath } from './shortestPath.js';
55

6-
export function shortestPaths<Node>(
7-
graph: Graph<Node>,
6+
export function shortestPaths<Node, LinkProps>(
7+
graph: Graph<Node, LinkProps>,
88
source: NoInfer<Node>,
99
destination: NoInfer<Node>,
1010
) {
@@ -13,19 +13,29 @@ export function shortestPaths<Node>(
1313
const paths = [path];
1414
const pathWeight = path.weight;
1515

16-
const removedEdges: { u: Node; v: Node; weight: number }[] = [];
16+
const removedEdges: Array<{ u: Node; v: Node; weight: number; props: LinkProps }> = [];
1717

1818
while (path.weight) {
1919
const u = path.nodes[0];
2020
const v = path.nodes[1];
2121

2222
if (graph.hasEdge(u, v)) {
23-
removedEdges.push({ u, v, weight: graph.getEdgeWeight(u, v) });
23+
removedEdges.push({
24+
u,
25+
v,
26+
weight: graph.getEdgeWeight(u, v),
27+
props: graph.getEdgeProperties(u, v),
28+
});
2429
graph.removeEdge(u, v);
2530
}
2631

2732
if (graph.hasEdge(v, u)) {
28-
removedEdges.push({ u: v, v: u, weight: graph.getEdgeWeight(v, u) });
33+
removedEdges.push({
34+
u: v,
35+
v: u,
36+
weight: graph.getEdgeWeight(v, u),
37+
props: graph.getEdgeProperties(u, v),
38+
});
2939
graph.removeEdge(v, u);
3040
}
3141

@@ -38,8 +48,8 @@ export function shortestPaths<Node>(
3848
}
3949
}
4050

41-
for (const { u, v, weight } of removedEdges) {
42-
graph.addEdge(u, v, weight);
51+
for (const { u, v, weight, props } of removedEdges) {
52+
graph.addEdge(u, v, ...([weight, props] as never));
4353
}
4454

4555
return paths;

0 commit comments

Comments
 (0)