From c8c56e7d68c57727526e9d251fd6ab9056186bdf Mon Sep 17 00:00:00 2001 From: Ravan Nannapaneni Date: Sun, 16 Feb 2025 21:42:29 +0800 Subject: [PATCH 1/5] Ravann weightfunction (#1) Created weightfunction to calculate weights of the path instead of hardcoded value ... --- src/algorithms/shortestPath/getPath.ts | 14 ++++++++++++-- src/algorithms/shortestPath/shortestPath.ts | 5 +++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/algorithms/shortestPath/getPath.ts b/src/algorithms/shortestPath/getPath.ts index 8ce82e9..4f2b9d5 100644 --- a/src/algorithms/shortestPath/getPath.ts +++ b/src/algorithms/shortestPath/getPath.ts @@ -3,6 +3,13 @@ import type { TraversingTracks } from './types.js'; import { Graph } from '../../Graph.js'; +export function addWeightFunction(edgeWeight: number, currentPathWeight: number | undefined, hop: number): number { + if (currentPathWeight === undefined) { + return edgeWeight; + } + return edgeWeight + currentPathWeight; +} + /** * Assembles the shortest path by traversing the * predecessor subgraph from destination to source. @@ -12,6 +19,7 @@ export function getPath( tracks: TraversingTracks>, source: NoInfer, destination: NoInfer, + weightFunction: (edgeWeight: number, currentPathWeight: number, hop: number) => number = addWeightFunction ): { nodes: [Node, Node, ...Node[]]; weight: number; @@ -19,15 +27,17 @@ export function getPath( const { p } = tracks; const nodeList: Node[] & { weight?: EdgeWeight } = []; - let totalWeight = 0; + let totalWeight = undefined as unknown as EdgeWeight; let node = destination; + let hop = 1; while (p.has(node)) { const currentNode = p.get(node)!; nodeList.push(node); - totalWeight += graph.getEdgeWeight(currentNode, node); + totalWeight = weightFunction(graph.getEdgeWeight(currentNode, node), totalWeight, hop); node = currentNode; + hop++; } if (node !== source) { diff --git a/src/algorithms/shortestPath/shortestPath.ts b/src/algorithms/shortestPath/shortestPath.ts index 3289dab..3aa4d63 100644 --- a/src/algorithms/shortestPath/shortestPath.ts +++ b/src/algorithms/shortestPath/shortestPath.ts @@ -1,7 +1,7 @@ import { Graph } from '../../Graph.js'; import { NoInfer } from '../../types.js'; import { dijkstra } from './dijkstra.js'; -import { getPath } from './getPath.js'; +import { getPath, addWeightFunction } from './getPath.js'; import { TraversingTracks } from './types.js'; /** @@ -13,6 +13,7 @@ export function shortestPath( graph: Graph, source: NoInfer, destination: NoInfer, + weightFunction: (edgeWeight: number, currentPathWeight: number, hop: number) => number = addWeightFunction ): { nodes: [Node, Node, ...Node[]]; weight: number; @@ -25,5 +26,5 @@ export function shortestPath( dijkstra(graph, tracks, source, destination); - return getPath(graph, tracks, source, destination); + return getPath(graph, tracks, source, destination, weightFunction); } From a26f3d582a4f83742345c00973a74e1655b71ca1 Mon Sep 17 00:00:00 2001 From: Ravan Nannapaneni Date: Tue, 18 Feb 2025 12:00:55 +0800 Subject: [PATCH 2/5] Changes based on PR comments ... (#2) * Added custom function document for shortestPath ... * Exporting WeightParams ... * Added WeightParams type ... * Changed weightFunction to use WeightParams ... * Changed the name of parameter ... * Added condition to check if the pathWeight is undefined ... --- README.md | 17 +++++++++++++++++ src/algorithms/shortestPath/getPath.ts | 20 ++++++++++++-------- src/algorithms/shortestPath/shortestPath.ts | 7 ++++--- src/algorithms/shortestPath/shortestPaths.ts | 2 +- src/index.ts | 2 +- src/types.ts | 6 ++++++ 6 files changed, 41 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 8929228..bbf8728 100644 --- a/README.md +++ b/README.md @@ -196,6 +196,23 @@ console.log(result.nodes); // Prints the array of nodes in the shortest path console.log(result.weight); // Prints the total weight of the path ``` +# shortestPath(graph, sourceNode, destinationNode, nextWeightFn) + +Calculates the weight based on the custom function. + +```javascript +import type { WeightParams } from '../../types.js'; +function multiplyWeightFunction(wp: WeightParams): number { + if (wp.currentPathWeight === undefined) { + return wp.edgeWeight; + } + return wp.edgeWeight * wp.currentPathWeight; +} +var result = shortestPath(graph, 'a', 'c', multiplyWeightFunction); +console.log(result.nodes); // Prints the array of nodes in the shortest path +console.log(result.weight); // Prints the total weight of the path +``` +

diff --git a/src/algorithms/shortestPath/getPath.ts b/src/algorithms/shortestPath/getPath.ts index 4f2b9d5..e8604cd 100644 --- a/src/algorithms/shortestPath/getPath.ts +++ b/src/algorithms/shortestPath/getPath.ts @@ -1,13 +1,17 @@ import type { EdgeWeight, NoInfer } from '../../types.js'; import type { TraversingTracks } from './types.js'; +import type { WeightParams } from '../../types.js'; import { Graph } from '../../Graph.js'; -export function addWeightFunction(edgeWeight: number, currentPathWeight: number | undefined, hop: number): number { - if (currentPathWeight === undefined) { - return edgeWeight; +/** + * Computes edge weight as the sum of all the edges in the path. + */ +export function addWeightFunction( wp: WeightParams): number { + if (wp.currentPathWeight === undefined) { + return wp.edgeWeight; } - return edgeWeight + currentPathWeight; + return wp.edgeWeight + wp.currentPathWeight; } /** @@ -19,15 +23,15 @@ export function getPath( tracks: TraversingTracks>, source: NoInfer, destination: NoInfer, - weightFunction: (edgeWeight: number, currentPathWeight: number, hop: number) => number = addWeightFunction + nextWeightFn: (params: WeightParams) => number = addWeightFunction ): { nodes: [Node, Node, ...Node[]]; - weight: number; + weight: number | undefined; } { const { p } = tracks; const nodeList: Node[] & { weight?: EdgeWeight } = []; - let totalWeight = undefined as unknown as EdgeWeight; + let totalWeight : EdgeWeight | undefined = undefined; let node = destination; let hop = 1; @@ -35,7 +39,7 @@ export function getPath( const currentNode = p.get(node)!; nodeList.push(node); - totalWeight = weightFunction(graph.getEdgeWeight(currentNode, node), totalWeight, hop); + totalWeight = nextWeightFn({ edgeWeight: graph.getEdgeWeight(currentNode, node), currentPathWeight: totalWeight, hop }); node = currentNode; hop++; } diff --git a/src/algorithms/shortestPath/shortestPath.ts b/src/algorithms/shortestPath/shortestPath.ts index 3aa4d63..3ab614a 100644 --- a/src/algorithms/shortestPath/shortestPath.ts +++ b/src/algorithms/shortestPath/shortestPath.ts @@ -3,6 +3,7 @@ import { NoInfer } from '../../types.js'; import { dijkstra } from './dijkstra.js'; import { getPath, addWeightFunction } from './getPath.js'; import { TraversingTracks } from './types.js'; +import type { WeightParams } from '../../types.js'; /** * Dijkstra's Shortest Path Algorithm. @@ -13,10 +14,10 @@ export function shortestPath( graph: Graph, source: NoInfer, destination: NoInfer, - weightFunction: (edgeWeight: number, currentPathWeight: number, hop: number) => number = addWeightFunction + nextWeightFn: (params: WeightParams) => number = addWeightFunction ): { nodes: [Node, Node, ...Node[]]; - weight: number; + weight: number | undefined; } { const tracks: TraversingTracks = { d: new Map(), @@ -26,5 +27,5 @@ export function shortestPath( dijkstra(graph, tracks, source, destination); - return getPath(graph, tracks, source, destination, weightFunction); + return getPath(graph, tracks, source, destination, nextWeightFn); } diff --git a/src/algorithms/shortestPath/shortestPaths.ts b/src/algorithms/shortestPath/shortestPaths.ts index 928b382..57a1281 100644 --- a/src/algorithms/shortestPath/shortestPaths.ts +++ b/src/algorithms/shortestPath/shortestPaths.ts @@ -41,7 +41,7 @@ export function shortestPaths( try { path = shortestPath(graph, source, destination); - if (!path.weight || pathWeight < path.weight) break; + if (!path.weight || !pathWeight || pathWeight < path.weight) break; paths.push(path); } catch (e) { break; diff --git a/src/index.ts b/src/index.ts index 817e98e..8d684a2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -export type { Edge, Serialized, SerializedInput, EdgeWeight } from './types.js'; +export type { Edge, Serialized, SerializedInput, EdgeWeight, WeightParams } from './types.js'; export { Graph } from './Graph.js'; export { CycleError } from './CycleError.js'; diff --git a/src/types.ts b/src/types.ts index bbce017..7d4ba83 100644 --- a/src/types.ts +++ b/src/types.ts @@ -18,3 +18,9 @@ export type SerializedInput = { }; export type NoInfer = [T][T extends any ? 0 : never]; + +export type WeightParams = { + edgeWeight: EdgeWeight; + currentPathWeight: EdgeWeight | undefined; + hop: number; +}; From 0ba2777a1d595aee0b9c7f33368f1dd394f0ceca Mon Sep 17 00:00:00 2001 From: Ravan Nannapaneni Date: Wed, 19 Feb 2025 14:06:32 +0800 Subject: [PATCH 3/5] Ravann patch 1 (#3) Updated weight function name to NextWeightFnParams ... Added new unit tests to test the weight function related functionality --- src/algorithms/shortestPath/getPath.ts | 9 ++- .../shortestPath/shortestPath.spec.ts | 76 ++++++++++++++++++- src/algorithms/shortestPath/shortestPath.ts | 4 +- src/index.ts | 2 +- src/types.ts | 2 +- 5 files changed, 84 insertions(+), 9 deletions(-) diff --git a/src/algorithms/shortestPath/getPath.ts b/src/algorithms/shortestPath/getPath.ts index e8604cd..24eb9e5 100644 --- a/src/algorithms/shortestPath/getPath.ts +++ b/src/algorithms/shortestPath/getPath.ts @@ -1,13 +1,13 @@ import type { EdgeWeight, NoInfer } from '../../types.js'; import type { TraversingTracks } from './types.js'; -import type { WeightParams } from '../../types.js'; +import type { NextWeightFnParams } from '../../types.js'; import { Graph } from '../../Graph.js'; /** * Computes edge weight as the sum of all the edges in the path. */ -export function addWeightFunction( wp: WeightParams): number { +export function addWeightFunction( wp: NextWeightFnParams): number { if (wp.currentPathWeight === undefined) { return wp.edgeWeight; } @@ -23,7 +23,7 @@ export function getPath( tracks: TraversingTracks>, source: NoInfer, destination: NoInfer, - nextWeightFn: (params: WeightParams) => number = addWeightFunction + nextWeightFn: (params: NextWeightFnParams) => number = addWeightFunction ): { nodes: [Node, Node, ...Node[]]; weight: number | undefined; @@ -39,7 +39,8 @@ export function getPath( const currentNode = p.get(node)!; nodeList.push(node); - totalWeight = nextWeightFn({ edgeWeight: graph.getEdgeWeight(currentNode, node), currentPathWeight: totalWeight, hop }); + const edgeWeight = graph.getEdgeWeight(currentNode, node) + totalWeight = nextWeightFn({edgeWeight, currentPathWeight: totalWeight, hop }); node = currentNode; hop++; } diff --git a/src/algorithms/shortestPath/shortestPath.spec.ts b/src/algorithms/shortestPath/shortestPath.spec.ts index c67492b..3ed212f 100644 --- a/src/algorithms/shortestPath/shortestPath.spec.ts +++ b/src/algorithms/shortestPath/shortestPath.spec.ts @@ -1,8 +1,10 @@ -import { describe, expect, it } from 'vitest'; +import { describe, expect, it, vi } from 'vitest'; import { Graph } from '../../Graph.js'; import { serializeGraph } from '../../utils/serializeGraph.js'; import { shortestPath } from './shortestPath.js'; import { shortestPaths } from './shortestPaths.js'; +import { addWeightFunction } from './getPath.js'; +import { NextWeightFnParams } from '../../types.js'; describe("Dijkstra's Shortest Path Algorithm", function () { it('Should compute shortest path on a single edge.', function () { @@ -104,3 +106,75 @@ describe("Dijkstra's Shortest Path Algorithm", function () { expect(postSerializedGraph.links).toContainEqual({ source: 'f', target: 'c' }); }); }); + +describe('addWeightFunction', () => { + it('should return edgeWeight if currentPathWeight is undefined', () => { + const params = { edgeWeight: 5, currentPathWeight: undefined, hop: 1 }; + expect(addWeightFunction(params)).toBe(5); + }); + + it('should return the sum of edgeWeight and currentPathWeight', () => { + const params = { edgeWeight: 5, currentPathWeight: 10, hop: 1 }; + expect(addWeightFunction(params)).toBe(15); + }); +}); + +describe('shortestPath with custom weight functions', () => { + it('should compute shortest path with default weight function (sum of weights)', () => { + const graph = new Graph().addEdge('a', 'b', 1).addEdge('b', 'c', 2); + expect(shortestPath(graph, 'a', 'c')).toEqual({ + nodes: ['a', 'b', 'c'], + weight: 3, + }); + }); + + it('should compute shortest path with a custom weight function', () => { + const customWeightFn = ({ edgeWeight, currentPathWeight, hop }: NextWeightFnParams) => { + if (currentPathWeight === undefined) { + return edgeWeight; + } + return currentPathWeight + edgeWeight ** hop; + }; + + const graph = new Graph().addEdge('a', 'b', 2).addEdge('b', 'c', 3); + expect(shortestPath(graph, 'a', 'c', customWeightFn)).toEqual({ + nodes: ['a', 'b', 'c'], + weight: 7, + }); + }); + + it('should pass correct parameters to custom weight function for a path with 3 nodes', () => { + const customWeightFn = vi.fn(({ edgeWeight, currentPathWeight, hop }: NextWeightFnParams) => { + if (currentPathWeight === undefined) { + return edgeWeight; + } + return currentPathWeight + edgeWeight ** hop; + }); + + const graph = new Graph().addEdge('a', 'b', 1).addEdge('b', 'c', 2); + shortestPath(graph, 'a', 'c', customWeightFn); + + expect(customWeightFn).toHaveBeenCalledWith({ edgeWeight: 2, currentPathWeight: undefined, hop: 1 }); + expect(customWeightFn).toHaveBeenCalledWith({ edgeWeight: 1, currentPathWeight: 2, hop: 2 }); + }); + + it('should compute shortest path with a custom weight function in a graph with multiple paths', () => { + const customWeightFn = ({ edgeWeight, currentPathWeight }: NextWeightFnParams) => { + if (currentPathWeight === undefined) { + return edgeWeight; + } + return edgeWeight + currentPathWeight; + }; + + const graph = new Graph() + .addEdge('a', 'b', 1) + .addEdge('b', 'c', 2) + .addEdge('a', 'd', 1) + .addEdge('d', 'c', 1); + + expect(shortestPath(graph, 'a', 'c', customWeightFn)).toEqual({ + nodes: ['a', 'd', 'c'], + weight: 2, + }); + }); +}); diff --git a/src/algorithms/shortestPath/shortestPath.ts b/src/algorithms/shortestPath/shortestPath.ts index 3ab614a..ba255ac 100644 --- a/src/algorithms/shortestPath/shortestPath.ts +++ b/src/algorithms/shortestPath/shortestPath.ts @@ -3,7 +3,7 @@ import { NoInfer } from '../../types.js'; import { dijkstra } from './dijkstra.js'; import { getPath, addWeightFunction } from './getPath.js'; import { TraversingTracks } from './types.js'; -import type { WeightParams } from '../../types.js'; +import type { NextWeightFnParams } from '../../types.js'; /** * Dijkstra's Shortest Path Algorithm. @@ -14,7 +14,7 @@ export function shortestPath( graph: Graph, source: NoInfer, destination: NoInfer, - nextWeightFn: (params: WeightParams) => number = addWeightFunction + nextWeightFn: (params: NextWeightFnParams) => number = addWeightFunction ): { nodes: [Node, Node, ...Node[]]; weight: number | undefined; diff --git a/src/index.ts b/src/index.ts index 8d684a2..a2419ab 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -export type { Edge, Serialized, SerializedInput, EdgeWeight, WeightParams } from './types.js'; +export type { Edge, Serialized, SerializedInput, EdgeWeight, NextWeightFnParams } from './types.js'; export { Graph } from './Graph.js'; export { CycleError } from './CycleError.js'; diff --git a/src/types.ts b/src/types.ts index 7d4ba83..91c6bb7 100644 --- a/src/types.ts +++ b/src/types.ts @@ -19,7 +19,7 @@ export type SerializedInput = { export type NoInfer = [T][T extends any ? 0 : never]; -export type WeightParams = { +export type NextWeightFnParams = { edgeWeight: EdgeWeight; currentPathWeight: EdgeWeight | undefined; hop: number; From 6ea85cc4a9d775d72310eb98d7b905defaabfd1f Mon Sep 17 00:00:00 2001 From: Ravan Nannapaneni Date: Wed, 19 Feb 2025 15:07:23 +0800 Subject: [PATCH 4/5] Added graph object to the NextWeightFnParams ... --- README.md | 4 ++-- src/algorithms/shortestPath/getPath.ts | 2 +- src/algorithms/shortestPath/shortestPath.spec.ts | 10 ++++++---- src/types.ts | 5 ++++- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index bbf8728..c0484b5 100644 --- a/README.md +++ b/README.md @@ -201,8 +201,8 @@ console.log(result.weight); // Prints the total weight of the path Calculates the weight based on the custom function. ```javascript -import type { WeightParams } from '../../types.js'; -function multiplyWeightFunction(wp: WeightParams): number { +import type { NextWeightFnParams } from '../../types.js'; +function multiplyWeightFunction(wp: NextWeightFnParams): number { if (wp.currentPathWeight === undefined) { return wp.edgeWeight; } diff --git a/src/algorithms/shortestPath/getPath.ts b/src/algorithms/shortestPath/getPath.ts index 24eb9e5..9dfd883 100644 --- a/src/algorithms/shortestPath/getPath.ts +++ b/src/algorithms/shortestPath/getPath.ts @@ -40,7 +40,7 @@ export function getPath( nodeList.push(node); const edgeWeight = graph.getEdgeWeight(currentNode, node) - totalWeight = nextWeightFn({edgeWeight, currentPathWeight: totalWeight, hop }); + totalWeight = nextWeightFn({edgeWeight, currentPathWeight: totalWeight, hop: hop, sourceGraph: graph}); node = currentNode; hop++; } diff --git a/src/algorithms/shortestPath/shortestPath.spec.ts b/src/algorithms/shortestPath/shortestPath.spec.ts index 3ed212f..1d85d4f 100644 --- a/src/algorithms/shortestPath/shortestPath.spec.ts +++ b/src/algorithms/shortestPath/shortestPath.spec.ts @@ -109,12 +109,14 @@ describe("Dijkstra's Shortest Path Algorithm", function () { describe('addWeightFunction', () => { it('should return edgeWeight if currentPathWeight is undefined', () => { - const params = { edgeWeight: 5, currentPathWeight: undefined, hop: 1 }; + const graph = new Graph(); + const params = { edgeWeight: 5, currentPathWeight: undefined, hop: 1, sourceGraph: graph }; expect(addWeightFunction(params)).toBe(5); }); it('should return the sum of edgeWeight and currentPathWeight', () => { - const params = { edgeWeight: 5, currentPathWeight: 10, hop: 1 }; + const graph = new Graph() + const params = { edgeWeight: 5, currentPathWeight: 10, hop: 1, sourceGraph: graph }; expect(addWeightFunction(params)).toBe(15); }); }); @@ -154,8 +156,8 @@ describe('shortestPath with custom weight functions', () => { const graph = new Graph().addEdge('a', 'b', 1).addEdge('b', 'c', 2); shortestPath(graph, 'a', 'c', customWeightFn); - expect(customWeightFn).toHaveBeenCalledWith({ edgeWeight: 2, currentPathWeight: undefined, hop: 1 }); - expect(customWeightFn).toHaveBeenCalledWith({ edgeWeight: 1, currentPathWeight: 2, hop: 2 }); + expect(customWeightFn).toHaveBeenCalledWith({ edgeWeight: 2, currentPathWeight: undefined, hop: 1, sourceGraph: graph }); + expect(customWeightFn).toHaveBeenCalledWith({ edgeWeight: 1, currentPathWeight: 2, hop: 2, sourceGraph: graph }); }); it('should compute shortest path with a custom weight function in a graph with multiple paths', () => { diff --git a/src/types.ts b/src/types.ts index 91c6bb7..81b0c4f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,3 +1,5 @@ +import { Graph } from './Graph.js'; + export type EdgeWeight = number; export type Edge = { @@ -19,8 +21,9 @@ export type SerializedInput = { export type NoInfer = [T][T extends any ? 0 : never]; -export type NextWeightFnParams = { +export type NextWeightFnParams = { edgeWeight: EdgeWeight; currentPathWeight: EdgeWeight | undefined; hop: number; + sourceGraph: Graph; }; From 3113074deae34466005d5b06804d600222288f34 Mon Sep 17 00:00:00 2001 From: Ravan Nannapaneni Date: Fri, 21 Feb 2025 22:56:35 +0800 Subject: [PATCH 5/5] Add additional properties to the NextWeightFnParams ... --- src/algorithms/shortestPath/getPath.ts | 6 +++- .../shortestPath/shortestPath.spec.ts | 29 ++++++++++++++++--- src/types.ts | 6 +++- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/algorithms/shortestPath/getPath.ts b/src/algorithms/shortestPath/getPath.ts index 9dfd883..95d6ef5 100644 --- a/src/algorithms/shortestPath/getPath.ts +++ b/src/algorithms/shortestPath/getPath.ts @@ -40,7 +40,11 @@ export function getPath( nodeList.push(node); const edgeWeight = graph.getEdgeWeight(currentNode, node) - totalWeight = nextWeightFn({edgeWeight, currentPathWeight: totalWeight, hop: hop, sourceGraph: graph}); + totalWeight = nextWeightFn({ + edgeWeight, currentPathWeight: totalWeight, + hop: hop, graph: graph, path: tracks, + previousNode: node, currentNode: currentNode + }); node = currentNode; hop++; } diff --git a/src/algorithms/shortestPath/shortestPath.spec.ts b/src/algorithms/shortestPath/shortestPath.spec.ts index 1d85d4f..134eca9 100644 --- a/src/algorithms/shortestPath/shortestPath.spec.ts +++ b/src/algorithms/shortestPath/shortestPath.spec.ts @@ -110,13 +110,20 @@ describe("Dijkstra's Shortest Path Algorithm", function () { describe('addWeightFunction', () => { it('should return edgeWeight if currentPathWeight is undefined', () => { const graph = new Graph(); - const params = { edgeWeight: 5, currentPathWeight: undefined, hop: 1, sourceGraph: graph }; + const params = { + edgeWeight: 5, currentPathWeight: undefined, hop: 1, + graph: graph, path: { d: new Map(), p: new Map(), q: new Set() }, + previousNode: 'a', currentNode: 'b' + }; expect(addWeightFunction(params)).toBe(5); }); it('should return the sum of edgeWeight and currentPathWeight', () => { const graph = new Graph() - const params = { edgeWeight: 5, currentPathWeight: 10, hop: 1, sourceGraph: graph }; + const params = { edgeWeight: 5, currentPathWeight: 10, hop: 1, + graph: graph, path: { d: new Map(), p: new Map(), q: new Set() }, + previousNode: 'a', currentNode: 'b' + }; expect(addWeightFunction(params)).toBe(15); }); }); @@ -156,8 +163,22 @@ describe('shortestPath with custom weight functions', () => { const graph = new Graph().addEdge('a', 'b', 1).addEdge('b', 'c', 2); shortestPath(graph, 'a', 'c', customWeightFn); - expect(customWeightFn).toHaveBeenCalledWith({ edgeWeight: 2, currentPathWeight: undefined, hop: 1, sourceGraph: graph }); - expect(customWeightFn).toHaveBeenCalledWith({ edgeWeight: 1, currentPathWeight: 2, hop: 2, sourceGraph: graph }); + expect(customWeightFn).toHaveBeenCalledWith({ edgeWeight: 2, currentPathWeight: undefined, hop: 1, + graph: graph, currentNode: 'b', previousNode: 'c', + path: { + d: new Map([['a', 0], ['b', 1], ['c', 3]]), + p: new Map([['b', 'a'], ['c', 'b']]), + q: new Set(), + }, + }); + expect(customWeightFn).toHaveBeenCalledWith({ edgeWeight: 1, currentPathWeight: 2, hop: 2, + graph: graph, currentNode: 'a', previousNode: 'b', + path: { + d: new Map([['a', 0], ['b', 1], ['c', 3]]), + p: new Map([['b', 'a'], ['c', 'b']]), + q: new Set(), + } + }); }); it('should compute shortest path with a custom weight function in a graph with multiple paths', () => { diff --git a/src/types.ts b/src/types.ts index 81b0c4f..37b3c54 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,3 +1,4 @@ +import { TraversingTracks } from './algorithms/shortestPath/types.js'; import { Graph } from './Graph.js'; export type EdgeWeight = number; @@ -25,5 +26,8 @@ export type NextWeightFnParams = { edgeWeight: EdgeWeight; currentPathWeight: EdgeWeight | undefined; hop: number; - sourceGraph: Graph; + graph: Graph; + path: TraversingTracks>; + previousNode: NoInfer; + currentNode: NoInfer; };