Skip to content

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

Merged
merged 5 commits into from
Feb 21, 2025

Conversation

ravann
Copy link
Contributor

@ravann ravann commented Feb 16, 2025

Created weightfunction to calculate weights of the path instead of hardcoded value ...

Created weightfunction to calculate weights of the path instead of hardcoded value ...
@@ -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
Copy link
Contributor

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

): {
nodes: [Node, Node, ...Node[]];
weight: number;
} {
const { p } = tracks;
const nodeList: Node[] & { weight?: EdgeWeight } = [];

let totalWeight = 0;
let totalWeight = undefined as unknown as EdgeWeight;
Copy link
Contributor

Choose a reason for hiding this comment

The 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 ?

@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Member

@curran curran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great start! Happy to merge when:

  • New test cases are added that test the new behavior
  • Documentation is added to the README that explains the new option

* 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 ...
@ravann
Copy link
Contributor Author

ravann commented Feb 18, 2025

@JesusTheHun updated based on your comments. Please review.

@curran I updated the Readme.md. I could add tests on addWeightFunction, but its a simple function. Any guidance is welcome.

@JesusTheHun
Copy link
Contributor

but its a simple function

Everything starts that way ^^
I agree with @curran, please write a test case for :

  • No given nextWeightFn, expect a sum of weights
  • Give a nextWeightFn, expect accordingly
  • Give a nextWeightFn, expect the correct parameters for a path with 3 nodes
  • Give a nextWeightFn to the shortest path function with a graph that contains at least 2 paths, for the sake of completion

Copy link
Contributor

@JesusTheHun JesusTheHun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your work is appreciated !

@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

src/types.ts Outdated
@@ -18,3 +18,9 @@ export type SerializedInput<Node = unknown, LinkProps = unknown> = {
};

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

export type WeightParams = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe be more specific with NextWeightFnParams ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is done

Updated weight function name to NextWeightFnParams ...

Added new unit tests to test the weight function related functionality
@ravann
Copy link
Contributor Author

ravann commented Feb 19, 2025

@JesusTheHun, @curran updated the code and Readme based on the changes suggested.

Btw, I realized getPath function traverses the graph in reverse order. It doesnt effect the default weight function OR my usecase. However, if there is a weight function that depends on how the graph is traversed, this can have an impact (e.g. newWeight = currentPathWeight + edgeWeight ** hop )

I don't have capacity to look into this right away, but can tackle this in a few weeks.

@JesusTheHun
Copy link
Contributor

@ravann how do tests feel like now ? 😄

@ravann
Copy link
Contributor Author

ravann commented Feb 20, 2025

@curran is it ok to merge the PR?

src/types.ts Outdated
edgeWeight: EdgeWeight;
currentPathWeight: EdgeWeight | undefined;
hop: number;
sourceGraph: Graph<Node, LinkProps>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be called "graph" to be consistent with other functions

@@ -18,3 +20,10 @@ export type SerializedInput<Node = unknown, LinkProps = unknown> = {
};

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

export type NextWeightFnParams<Node = unknown, LinkProps = unknown> = {
Copy link
Contributor

@JesusTheHun JesusTheHun Feb 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also add the "path" as parameter, that would allow to get the total number of hops for the cases when the calculation is based on the distance from the source.

Edit: actually, I think we should also add the previous node and the current node, so the properties of the link can be retrieved from the graph.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is done.

@ravann
Copy link
Contributor Author

ravann commented Feb 21, 2025

@JesusTheHun do you think the change can be merged now?

currentPathWeight: EdgeWeight | undefined;
hop: number;
graph: Graph<Node, LinkProps>;
path: TraversingTracks<NoInfer<Node>>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"NoInfer" only matters in a function signature.

Copy link
Member

@curran curran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fantastic! I really appreciate the work on this @ravann and thank you so much @JesusTheHun for reviewing.

@curran curran merged commit 423d284 into datavis-tech:master Feb 21, 2025
@curran
Copy link
Member

curran commented Feb 21, 2025

Published in https://github.com/datavis-tech/graph-data-structure/releases/tag/v4.4.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants