Skip to content

Commit 3113074

Browse files
authored
Add additional properties to the NextWeightFnParams ...
1 parent 6ea85cc commit 3113074

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

src/algorithms/shortestPath/getPath.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ export function getPath<Node, LinkProps>(
4040

4141
nodeList.push(node);
4242
const edgeWeight = graph.getEdgeWeight(currentNode, node)
43-
totalWeight = nextWeightFn({edgeWeight, currentPathWeight: totalWeight, hop: hop, sourceGraph: graph});
43+
totalWeight = nextWeightFn({
44+
edgeWeight, currentPathWeight: totalWeight,
45+
hop: hop, graph: graph, path: tracks,
46+
previousNode: node, currentNode: currentNode
47+
});
4448
node = currentNode;
4549
hop++;
4650
}

src/algorithms/shortestPath/shortestPath.spec.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,20 @@ describe("Dijkstra's Shortest Path Algorithm", function () {
110110
describe('addWeightFunction', () => {
111111
it('should return edgeWeight if currentPathWeight is undefined', () => {
112112
const graph = new Graph();
113-
const params = { edgeWeight: 5, currentPathWeight: undefined, hop: 1, sourceGraph: graph };
113+
const params = {
114+
edgeWeight: 5, currentPathWeight: undefined, hop: 1,
115+
graph: graph, path: { d: new Map(), p: new Map(), q: new Set() },
116+
previousNode: 'a', currentNode: 'b'
117+
};
114118
expect(addWeightFunction(params)).toBe(5);
115119
});
116120

117121
it('should return the sum of edgeWeight and currentPathWeight', () => {
118122
const graph = new Graph()
119-
const params = { edgeWeight: 5, currentPathWeight: 10, hop: 1, sourceGraph: graph };
123+
const params = { edgeWeight: 5, currentPathWeight: 10, hop: 1,
124+
graph: graph, path: { d: new Map(), p: new Map(), q: new Set() },
125+
previousNode: 'a', currentNode: 'b'
126+
};
120127
expect(addWeightFunction(params)).toBe(15);
121128
});
122129
});
@@ -156,8 +163,22 @@ describe('shortestPath with custom weight functions', () => {
156163
const graph = new Graph().addEdge('a', 'b', 1).addEdge('b', 'c', 2);
157164
shortestPath(graph, 'a', 'c', customWeightFn);
158165

159-
expect(customWeightFn).toHaveBeenCalledWith({ edgeWeight: 2, currentPathWeight: undefined, hop: 1, sourceGraph: graph });
160-
expect(customWeightFn).toHaveBeenCalledWith({ edgeWeight: 1, currentPathWeight: 2, hop: 2, sourceGraph: graph });
166+
expect(customWeightFn).toHaveBeenCalledWith({ edgeWeight: 2, currentPathWeight: undefined, hop: 1,
167+
graph: graph, currentNode: 'b', previousNode: 'c',
168+
path: {
169+
d: new Map([['a', 0], ['b', 1], ['c', 3]]),
170+
p: new Map([['b', 'a'], ['c', 'b']]),
171+
q: new Set(),
172+
},
173+
});
174+
expect(customWeightFn).toHaveBeenCalledWith({ edgeWeight: 1, currentPathWeight: 2, hop: 2,
175+
graph: graph, currentNode: 'a', previousNode: 'b',
176+
path: {
177+
d: new Map([['a', 0], ['b', 1], ['c', 3]]),
178+
p: new Map([['b', 'a'], ['c', 'b']]),
179+
q: new Set(),
180+
}
181+
});
161182
});
162183

163184
it('should compute shortest path with a custom weight function in a graph with multiple paths', () => {

src/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { TraversingTracks } from './algorithms/shortestPath/types.js';
12
import { Graph } from './Graph.js';
23

34
export type EdgeWeight = number;
@@ -25,5 +26,8 @@ export type NextWeightFnParams<Node = unknown, LinkProps = unknown> = {
2526
edgeWeight: EdgeWeight;
2627
currentPathWeight: EdgeWeight | undefined;
2728
hop: number;
28-
sourceGraph: Graph<Node, LinkProps>;
29+
graph: Graph<Node, LinkProps>;
30+
path: TraversingTracks<NoInfer<Node>>;
31+
previousNode: NoInfer<Node>;
32+
currentNode: NoInfer<Node>;
2933
};

0 commit comments

Comments
 (0)