cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! Site migration is underway. Phase 2: migrate recent content

What's the difference between `shortestpath` and `allshortestpaths`?

czp
Node Link
query1: match p=shortestpath((a:A)-[e:E*1..3]->(b:B)) return p,size(e) AS steps
query2: match p=allshortestpaths((a:A)-[e:E*1..3]->(b:B)) return p,size(e) AS steps

I did some tests and they all got the same result, my expectation is that if there are multiple shortest paths between two points then only one is returned, obviously I misunderstood.
So, what topology should I use to test the difference between these two queries, and does neo4j support finding any shortest path?

1 ACCEPTED SOLUTION

busymo16
Ninja
Ninja

Hi @czp, keep in mind that if you have multiple input records/rows, you will get a separate result from shortestPath() for each row, since Cypher operations operate per row.

With shortestPath() , your output rows should be <= the number of input rows (since rows, where no path exists, will be weeded out, and there should be at most one result per row).

With allShortestPaths() , your output rows may be greater than your input rows, depending on how many paths have the same length per input row.

View solution in original post

1 REPLY 1

busymo16
Ninja
Ninja

Hi @czp, keep in mind that if you have multiple input records/rows, you will get a separate result from shortestPath() for each row, since Cypher operations operate per row.

With shortestPath() , your output rows should be <= the number of input rows (since rows, where no path exists, will be weeded out, and there should be at most one result per row).

With allShortestPaths() , your output rows may be greater than your input rows, depending on how many paths have the same length per input row.