-
Notifications
You must be signed in to change notification settings - Fork 52
weightFunction to calculate weight of path ... #107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
c8c56e7
a26f3d5
0ba2777
6ea85cc
3113074
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,22 +19,25 @@ export function getPath<Node, LinkProps>( | |
tracks: TraversingTracks<NoInfer<Node>>, | ||
source: NoInfer<Node>, | ||
destination: NoInfer<Node>, | ||
weightFunction: (edgeWeight: number, currentPathWeight: number, hop: number) => number = addWeightFunction | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer an object as a single parameter instead of multi parameters. It makes evolutions easier. Also, make the properties more explicit + add jsdoc There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a good practice to make a reference to the graph accessible to the function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
): { | ||
nodes: [Node, Node, ...Node[]]; | ||
weight: number; | ||
} { | ||
const { p } = tracks; | ||
const nodeList: Node[] & { weight?: EdgeWeight } = []; | ||
|
||
let totalWeight = 0; | ||
let totalWeight = undefined as unknown as EdgeWeight; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why cast the type to an invalid one instead of setting the correct type ? |
||
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) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would call it "nextWeightFn", as "weightFunction" is more ambiguous