Skip to content

Commit a26f3d5

Browse files
authored
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 ...
1 parent c8c56e7 commit a26f3d5

File tree

6 files changed

+41
-13
lines changed

6 files changed

+41
-13
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,23 @@ console.log(result.nodes); // Prints the array of nodes in the shortest path
196196
console.log(result.weight); // Prints the total weight of the path
197197
```
198198

199+
<a name="shortest-path" href="#shortest-path">#</a> <b>shortestPath</b>(<i>graph</i>, <i>sourceNode</i>, <i>destinationNode</i>, <i>nextWeightFn</i>)
200+
201+
Calculates the weight based on the custom function.
202+
203+
```javascript
204+
import type { WeightParams } from '../../types.js';
205+
function multiplyWeightFunction(wp: WeightParams): number {
206+
if (wp.currentPathWeight === undefined) {
207+
return wp.edgeWeight;
208+
}
209+
return wp.edgeWeight * wp.currentPathWeight;
210+
}
211+
var result = shortestPath(graph, 'a', 'c', multiplyWeightFunction);
212+
console.log(result.nodes); // Prints the array of nodes in the shortest path
213+
console.log(result.weight); // Prints the total weight of the path
214+
```
215+
199216
<p align="center">
200217
<a href="https://datavis.tech/">
201218
<img src="https://cloud.githubusercontent.com/assets/68416/15298394/a7a0a66a-1bbc-11e6-9636-367bed9165fc.png">

src/algorithms/shortestPath/getPath.ts

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

45
import { Graph } from '../../Graph.js';
56

6-
export function addWeightFunction(edgeWeight: number, currentPathWeight: number | undefined, hop: number): number {
7-
if (currentPathWeight === undefined) {
8-
return edgeWeight;
7+
/**
8+
* Computes edge weight as the sum of all the edges in the path.
9+
*/
10+
export function addWeightFunction( wp: WeightParams): number {
11+
if (wp.currentPathWeight === undefined) {
12+
return wp.edgeWeight;
913
}
10-
return edgeWeight + currentPathWeight;
14+
return wp.edgeWeight + wp.currentPathWeight;
1115
}
1216

1317
/**
@@ -19,23 +23,23 @@ export function getPath<Node, LinkProps>(
1923
tracks: TraversingTracks<NoInfer<Node>>,
2024
source: NoInfer<Node>,
2125
destination: NoInfer<Node>,
22-
weightFunction: (edgeWeight: number, currentPathWeight: number, hop: number) => number = addWeightFunction
26+
nextWeightFn: (params: WeightParams) => number = addWeightFunction
2327
): {
2428
nodes: [Node, Node, ...Node[]];
25-
weight: number;
29+
weight: number | undefined;
2630
} {
2731
const { p } = tracks;
2832
const nodeList: Node[] & { weight?: EdgeWeight } = [];
2933

30-
let totalWeight = undefined as unknown as EdgeWeight;
34+
let totalWeight : EdgeWeight | undefined = undefined;
3135
let node = destination;
3236

3337
let hop = 1;
3438
while (p.has(node)) {
3539
const currentNode = p.get(node)!;
3640

3741
nodeList.push(node);
38-
totalWeight = weightFunction(graph.getEdgeWeight(currentNode, node), totalWeight, hop);
42+
totalWeight = nextWeightFn({ edgeWeight: graph.getEdgeWeight(currentNode, node), currentPathWeight: totalWeight, hop });
3943
node = currentNode;
4044
hop++;
4145
}

src/algorithms/shortestPath/shortestPath.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +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';
67

78
/**
89
* Dijkstra's Shortest Path Algorithm.
@@ -13,10 +14,10 @@ export function shortestPath<Node, LinkProps>(
1314
graph: Graph<Node, LinkProps>,
1415
source: NoInfer<Node>,
1516
destination: NoInfer<Node>,
16-
weightFunction: (edgeWeight: number, currentPathWeight: number, hop: number) => number = addWeightFunction
17+
nextWeightFn: (params: WeightParams) => number = addWeightFunction
1718
): {
1819
nodes: [Node, Node, ...Node[]];
19-
weight: number;
20+
weight: number | undefined;
2021
} {
2122
const tracks: TraversingTracks<Node> = {
2223
d: new Map(),
@@ -26,5 +27,5 @@ export function shortestPath<Node, LinkProps>(
2627

2728
dijkstra(graph, tracks, source, destination);
2829

29-
return getPath(graph, tracks, source, destination, weightFunction);
30+
return getPath(graph, tracks, source, destination, nextWeightFn);
3031
}

src/algorithms/shortestPath/shortestPaths.ts

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

4242
try {
4343
path = shortestPath(graph, source, destination);
44-
if (!path.weight || pathWeight < path.weight) break;
44+
if (!path.weight || !pathWeight || pathWeight < path.weight) break;
4545
paths.push(path);
4646
} catch (e) {
4747
break;

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 } from './types.js';
1+
export type { Edge, Serialized, SerializedInput, EdgeWeight, WeightParams } from './types.js';
22

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

src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,9 @@ export type SerializedInput<Node = unknown, LinkProps = unknown> = {
1818
};
1919

2020
export type NoInfer<T> = [T][T extends any ? 0 : never];
21+
22+
export type WeightParams = {
23+
edgeWeight: EdgeWeight;
24+
currentPathWeight: EdgeWeight | undefined;
25+
hop: number;
26+
};

0 commit comments

Comments
 (0)