neo4j / graph-data-science

Source code for the Neo4j Graph Data Science library of graph algorithms.
https://neo4j.com/docs/graph-data-science/current/
Other
596 stars 157 forks source link

How to limit hop in Yen’s Shortest Path algorithm in Neo4j #306

Closed xiaoxlm closed 2 months ago

xiaoxlm commented 2 months ago

Guidelines

Please note that GitHub issues are only meant for bug reports/feature requests. If you have questions on how to use Neo4j, please ask on StackOverflow instead of creating an issue here.

Before creating a new issue, please check whether someone else has raised the same issue. You may be able to add context to that issue instead of duplicating the report. However, each issue should also only be focussed on a single problem, so do not describe new problems within an existing thread - these are very hard to track and manage, and your problem may be ignored. Finally, do not append comments to closed issues; if the same problem re-occurs, open a new issue, and include a link to the old one.

To help us understand your issue, please specify important details, primarily:

chatGPT mentioned that in older versions, you can limit the number of hops through "maxDepth" in the "algo.kShortestPaths" algorithm. But in the latest version of the documentation, I didn't find the relevant parameters in the related algorithms such as "gds.shortestPath.yens.stream", so do you have any other suggestions?

IoannisPanagiotas commented 2 months ago

Hi @xiaoxlm,

What. you describe. happens because of the fact that there are four possible paths with distances 2, 4, 5, 6. Since you specified k with 3, Yens will find the first three. Now the tlast path has size 5 so it is pruned afterwards which is an operation that happens after the algorithm has finished.

Currently there is no internal possibility to define maxDepth. I am not sure whether it existed and was removed at some point. We wil mark the feature request and possibly work on it at some point, though I cannot promise you anything for the moment.

For the moment, I think you could try to raise k to a larger value (for example here instead of 3 to 4 would bring you the answer you wanted) and apply the same filtering you have applied with the size of the cost array. As this will generate more paths, it is more likely to find all the paths with the size requirement you want.

I will close this for now, Let me know if you have any other question.

Best Ioannis.