Skip to content

Commit 0ba2777

Browse files
authored
Ravann patch 1 (#3)
Updated weight function name to NextWeightFnParams ... Added new unit tests to test the weight function related functionality
1 parent a26f3d5 commit 0ba2777

File tree

5 files changed

+84
-9
lines changed

5 files changed

+84
-9
lines changed

src/algorithms/shortestPath/getPath.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type { EdgeWeight, NoInfer } from '../../types.js';
22
import type { TraversingTracks } from './types.js';
3-
import type { WeightParams } from '../../types.js';
3+
import type { NextWeightFnParams } from '../../types.js';
44

55
import { Graph } from '../../Graph.js';
66

77
/**
88
* Computes edge weight as the sum of all the edges in the path.
99
*/
10-
export function addWeightFunction( wp: WeightParams): number {
10+
export function addWeightFunction( wp: NextWeightFnParams): number {
1111
if (wp.currentPathWeight === undefined) {
1212
return wp.edgeWeight;
1313
}
@@ -23,7 +23,7 @@ export function getPath<Node, LinkProps>(
2323
tracks: TraversingTracks<NoInfer<Node>>,
2424
source: NoInfer<Node>,
2525
destination: NoInfer<Node>,
26-
nextWeightFn: (params: WeightParams) => number = addWeightFunction
26+
nextWeightFn: (params: NextWeightFnParams) => number = addWeightFunction
2727
): {
2828
nodes: [Node, Node, ...Node[]];
2929
weight: number | undefined;
@@ -39,7 +39,8 @@ export function getPath<Node, LinkProps>(
3939
const currentNode = p.get(node)!;
4040

4141
nodeList.push(node);
42-
totalWeight = nextWeightFn({ edgeWeight: graph.getEdgeWeight(currentNode, node), currentPathWeight: totalWeight, hop });
42+
const edgeWeight = graph.getEdgeWeight(currentNode, node)
43+
totalWeight = nextWeightFn({edgeWeight, currentPathWeight: totalWeight, hop });
4344
node = currentNode;
4445
hop++;
4546
}

src/algorithms/shortestPath/shortestPath.spec.ts

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import { describe, expect, it } from 'vitest';
1+
import { describe, expect, it, vi } from 'vitest';
22
import { Graph } from '../../Graph.js';
33
import { serializeGraph } from '../../utils/serializeGraph.js';
44
import { shortestPath } from './shortestPath.js';
55
import { shortestPaths } from './shortestPaths.js';
6+
import { addWeightFunction } from './getPath.js';
7+
import { NextWeightFnParams } from '../../types.js';
68

79
describe("Dijkstra's Shortest Path Algorithm", function () {
810
it('Should compute shortest path on a single edge.', function () {
@@ -104,3 +106,75 @@ describe("Dijkstra's Shortest Path Algorithm", function () {
104106
expect(postSerializedGraph.links).toContainEqual({ source: 'f', target: 'c' });
105107
});
106108
});
109+
110+
describe('addWeightFunction', () => {
111+
it('should return edgeWeight if currentPathWeight is undefined', () => {
112+
const params = { edgeWeight: 5, currentPathWeight: undefined, hop: 1 };
113+
expect(addWeightFunction(params)).toBe(5);
114+
});
115+
116+
it('should return the sum of edgeWeight and currentPathWeight', () => {
117+
const params = { edgeWeight: 5, currentPathWeight: 10, hop: 1 };
118+
expect(addWeightFunction(params)).toBe(15);
119+
});
120+
});
121+
122+
describe('shortestPath with custom weight functions', () => {
123+
it('should compute shortest path with default weight function (sum of weights)', () => {
124+
const graph = new Graph().addEdge('a', 'b', 1).addEdge('b', 'c', 2);
125+
expect(shortestPath(graph, 'a', 'c')).toEqual({
126+
nodes: ['a', 'b', 'c'],
127+
weight: 3,
128+
});
129+
});
130+
131+
it('should compute shortest path with a custom weight function', () => {
132+
const customWeightFn = ({ edgeWeight, currentPathWeight, hop }: NextWeightFnParams) => {
133+
if (currentPathWeight === undefined) {
134+
return edgeWeight;
135+
}
136+
return currentPathWeight + edgeWeight ** hop;
137+
};
138+
139+
const graph = new Graph().addEdge('a', 'b', 2).addEdge('b', 'c', 3);
140+
expect(shortestPath(graph, 'a', 'c', customWeightFn)).toEqual({
141+
nodes: ['a', 'b', 'c'],
142+
weight: 7,
143+
});
144+
});
145+
146+
it('should pass correct parameters to custom weight function for a path with 3 nodes', () => {
147+
const customWeightFn = vi.fn(({ edgeWeight, currentPathWeight, hop }: NextWeightFnParams) => {
148+
if (currentPathWeight === undefined) {
149+
return edgeWeight;
150+
}
151+
return currentPathWeight + edgeWeight ** hop;
152+
});
153+
154+
const graph = new Graph().addEdge('a', 'b', 1).addEdge('b', 'c', 2);
155+
shortestPath(graph, 'a', 'c', customWeightFn);
156+
157+
expect(customWeightFn).toHaveBeenCalledWith({ edgeWeight: 2, currentPathWeight: undefined, hop: 1 });
158+
expect(customWeightFn).toHaveBeenCalledWith({ edgeWeight: 1, currentPathWeight: 2, hop: 2 });
159+
});
160+
161+
it('should compute shortest path with a custom weight function in a graph with multiple paths', () => {
162+
const customWeightFn = ({ edgeWeight, currentPathWeight }: NextWeightFnParams) => {
163+
if (currentPathWeight === undefined) {
164+
return edgeWeight;
165+
}
166+
return edgeWeight + currentPathWeight;
167+
};
168+
169+
const graph = new Graph()
170+
.addEdge('a', 'b', 1)
171+
.addEdge('b', 'c', 2)
172+
.addEdge('a', 'd', 1)
173+
.addEdge('d', 'c', 1);
174+
175+
expect(shortestPath(graph, 'a', 'c', customWeightFn)).toEqual({
176+
nodes: ['a', 'd', 'c'],
177+
weight: 2,
178+
});
179+
});
180+
});

src/algorithms/shortestPath/shortestPath.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { NoInfer } from '../../types.js';
33
import { dijkstra } from './dijkstra.js';
44
import { getPath, addWeightFunction } from './getPath.js';
55
import { TraversingTracks } from './types.js';
6-
import type { WeightParams } from '../../types.js';
6+
import type { NextWeightFnParams } from '../../types.js';
77

88
/**
99
* Dijkstra's Shortest Path Algorithm.
@@ -14,7 +14,7 @@ export function shortestPath<Node, LinkProps>(
1414
graph: Graph<Node, LinkProps>,
1515
source: NoInfer<Node>,
1616
destination: NoInfer<Node>,
17-
nextWeightFn: (params: WeightParams) => number = addWeightFunction
17+
nextWeightFn: (params: NextWeightFnParams) => number = addWeightFunction
1818
): {
1919
nodes: [Node, Node, ...Node[]];
2020
weight: number | undefined;

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type { Edge, Serialized, SerializedInput, EdgeWeight, WeightParams } from './types.js';
1+
export type { Edge, Serialized, SerializedInput, EdgeWeight, NextWeightFnParams } from './types.js';
22

33
export { Graph } from './Graph.js';
44
export { CycleError } from './CycleError.js';

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export type SerializedInput<Node = unknown, LinkProps = unknown> = {
1919

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

22-
export type WeightParams = {
22+
export type NextWeightFnParams = {
2323
edgeWeight: EdgeWeight;
2424
currentPathWeight: EdgeWeight | undefined;
2525
hop: number;

0 commit comments

Comments
 (0)