0
votes

I run gds.alpha.kShortestPaths.stream using Neo4j 4.0.2. The algorithm yields a path. The path has the nodes connected with NEXT relationship. I need to find the actual relationships in the Graph that connect the nodes in the path. I came up with a kludgy query that is based on my knowing how long the path is:

...
YIELD path
with nodes(path) as nodes with nodes[0] as n0, nodes[1] as n1, nodes[2] as n2, nodes[3] as n3
match (n0)-[r1]-(n1)
match (n1)-[r2]-(n2)
match (n2)-[r3]-(n3)
return n0, n1, n2, n3, r1, r2, r3

Obviously this solution is inadequate and the only reason I posted it here is to illustrate what I need to do for a path of any length.

Thank you

1

1 Answers

1
votes

You can do this programmatically with:

YIELD index, path
WITH index, nodes(path) as nodes
WITH index, nodes, size(nodes) as number_of_nodes
UNWIND range(0,number_of_nodes - 2) as start
WITH index, nodes[start] as start_node, nodes[start + 1] as end_node
MATCH (start_node)-[r:NEXT]->(end_node)
RETURN index, start_node,r, end_node