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
shortestPath doesn't work for cycles/loops #8667
Comments
@IanRogers shortestPath You are telling it to find the shortestPath from Node A to itself and the shortest path to your starting location is a path of length 0. I encourage you to write your own traversal function with the use case you have in mind. Closing this as it's working as intended. |
I'm kinda disappointed that you closed this so quickly out of hand because the use case is clear: what is the most efficient way to tell if the current node is part of a loop of relationships labelled "R". And I'm not convinced that shortestPath is working correctly as the query was @spacecowboy can you reopen this issue at least until this question is answered please? |
The shortestPath algorithm explicitly disallows the case of startNode=endNode. See https://github.com/neo4j/neo4j/blob/3.2/community/graph-algo/src/main/java/org/neo4j/graphalgo/impl/path/ShortestPath.java#L142. I believe this is due to the fact that the bi-directional BFS search uses a check of node-uniqueness to prune out already touched nodes and detect when the left and right searches reach each other. This means that it is not possible to use this algorithm to detect existing cycles. However, it would be possible to use it to detect if a planned relationship creation would result in a cycle. For example, if you want to create a relationship between two different nodes On the question of the default minimum value for shortest path, it appears that both the varlength-expand syntax and the shortestPath syntax default to
These two example both give the correct answer. However, when you express the min length as 1, then we get:
As described above, shortestPath gives the incorrect answer because it cannot handle the same start and end node. However, it would be possible to get Cypher to revert to the explicit var-length algorithm for this case, and we'll investigate that option. It does not satisfy your requirement on the most efficient algorithm, but would at least give the correct answer. |
Is there a solution to this available in the graph algorithm package? |
@marcusze I'd recommend asking that question over in https://github.com/neo4j/graph-data-science |
I'm trying to detect simple relationship cycles/loops as efficiently as possible, but shortestPath doesn't return anything...
The text was updated successfully, but these errors were encountered: