cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

allShortestPaths doesn't return all possible paths

I have one requirement where I need all paths between two nodes. So first I tried to fetch all paths using AllShortestPath() and then need to order them using some specific criteria. My issue is that I am using the below query and getting one path

MATCH (source:A{id:11}), (target:A{id:13}) WITH source,target MATCH path = allShortestPaths((source)-[*]-(target)) RETURN path

When I have made the below change MATCH path = allShortestPaths((source)-[:RelA|RelB*]-(target)) 
I am getting different path. But the first one is generic one So I should get all paths.
I am not able to understand why the function is behaving differently

1 REPLY 1

I don't know your data to validate my hypothesis, but it would seem that your overall shortestPaths (no relationship type constraint) is shorter that the paths between source and target that must traverse through relationships of type 'RelA' or 'RelB'. This would explain your results.

For example, create test data with two paths. The shortest path has one hop, while the second path has three hops. 

create(n:Node{id:0})-[:REL]->(m:Node{id:1})
merge(n)-[:RelA]->()-[:RelB]->()-[:RelA]->(m)

Screen Shot 2022-07-19 at 7.14.10 AM.png 

As shown, the shortest path from id=0 to id=1 is through the single 'REL' relationship. If I restrict my path to having to traverse only 'RelA' or 'RelB' relationships, then the overall shortest path is not a candidate any longer and the shortest path is now the path through nodes 72 and 73.

Screen Shot 2022-07-19 at 7.21.22 AM.png

Screen Shot 2022-07-19 at 7.22.33 AM.png