Skip to content

Commit 6ea85cc

Browse files
authored
Added graph object to the NextWeightFnParams ...
1 parent 0ba2777 commit 6ea85cc

File tree

4 files changed

+13
-8
lines changed

4 files changed

+13
-8
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ console.log(result.weight); // Prints the total weight of the path
201201
Calculates the weight based on the custom function.
202202

203203
```javascript
204-
import type { WeightParams } from '../../types.js';
205-
function multiplyWeightFunction(wp: WeightParams): number {
204+
import type { NextWeightFnParams } from '../../types.js';
205+
function multiplyWeightFunction(wp: NextWeightFnParams): number {
206206
if (wp.currentPathWeight === undefined) {
207207
return wp.edgeWeight;
208208
}

src/algorithms/shortestPath/getPath.ts

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

4141
nodeList.push(node);
4242
const edgeWeight = graph.getEdgeWeight(currentNode, node)
43-
totalWeight = nextWeightFn({edgeWeight, currentPathWeight: totalWeight, hop });
43+
totalWeight = nextWeightFn({edgeWeight, currentPathWeight: totalWeight, hop: hop, sourceGraph: graph});
4444
node = currentNode;
4545
hop++;
4646
}

src/algorithms/shortestPath/shortestPath.spec.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,14 @@ describe("Dijkstra's Shortest Path Algorithm", function () {
109109

110110
describe('addWeightFunction', () => {
111111
it('should return edgeWeight if currentPathWeight is undefined', () => {
112-
const params = { edgeWeight: 5, currentPathWeight: undefined, hop: 1 };
112+
const graph = new Graph();
113+
const params = { edgeWeight: 5, currentPathWeight: undefined, hop: 1, sourceGraph: graph };
113114
expect(addWeightFunction(params)).toBe(5);
114115
});
115116

116117
it('should return the sum of edgeWeight and currentPathWeight', () => {
117-
const params = { edgeWeight: 5, currentPathWeight: 10, hop: 1 };
118+
const graph = new Graph()
119+
const params = { edgeWeight: 5, currentPathWeight: 10, hop: 1, sourceGraph: graph };
118120
expect(addWeightFunction(params)).toBe(15);
119121
});
120122
});
@@ -154,8 +156,8 @@ describe('shortestPath with custom weight functions', () => {
154156
const graph = new Graph().addEdge('a', 'b', 1).addEdge('b', 'c', 2);
155157
shortestPath(graph, 'a', 'c', customWeightFn);
156158

157-
expect(customWeightFn).toHaveBeenCalledWith({ edgeWeight: 2, currentPathWeight: undefined, hop: 1 });
158-
expect(customWeightFn).toHaveBeenCalledWith({ edgeWeight: 1, currentPathWeight: 2, hop: 2 });
159+
expect(customWeightFn).toHaveBeenCalledWith({ edgeWeight: 2, currentPathWeight: undefined, hop: 1, sourceGraph: graph });
160+
expect(customWeightFn).toHaveBeenCalledWith({ edgeWeight: 1, currentPathWeight: 2, hop: 2, sourceGraph: graph });
159161
});
160162

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

src/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Graph } from './Graph.js';
2+
13
export type EdgeWeight = number;
24

35
export type Edge<NodeIdentity = unknown, Props = unknown> = {
@@ -19,8 +21,9 @@ export type SerializedInput<Node = unknown, LinkProps = unknown> = {
1921

2022
export type NoInfer<T> = [T][T extends any ? 0 : never];
2123

22-
export type NextWeightFnParams = {
24+
export type NextWeightFnParams<Node = unknown, LinkProps = unknown> = {
2325
edgeWeight: EdgeWeight;
2426
currentPathWeight: EdgeWeight | undefined;
2527
hop: number;
28+
sourceGraph: Graph<Node, LinkProps>;
2629
};

0 commit comments

Comments
 (0)