0
votes

I am trying to find the Shortest Path between given Multiple Nodes. Like I have an Array of locations Node and then there is a location for the user. I want to find the Nearest Location to that user between these given nodes.

I have tried this query

UNWIND
["93049","67069","90762","86156","01067","18069","52146","04356"," 42329","53113","76131","40549","50670","50678","85774","80339"] AS plz
MATCH p=Shortestpath(
(a:plzNodes {plz: plz})-[*..14]-(b:plzNodes {plz: "88400"})
) RETURN  plz,collect(p);
1

1 Answers

0
votes

You can use the path expander procs from APOC Procedures for this, collecting the possible end nodes, and then using the path expander from the start node to those end nodes with a limit of 1: the path to the first node encountered (which will be the shortest path, due to breadth first expansion) will be returned.

WITH ["93049","67069","90762","86156","01067","18069","52146","04356"," 42329","53113","76131","40549","50670","50678","85774","80339"] AS plzes
MATCH (a:plzNodes)
WHERE a.plz in plzes
WITH collect(a) as plzNodes
MATCH (b:plzNodes {plz: "88400"})
CALL apoc.path.subgraphNodes(b, {endNodes:plzNodes, limit:1, maxLevel:14}) YIELD node as closest
RETURN closest