ManMohan Vyas
ManMohan Vyas

Reputation: 4062

Neo4j - How to group all nodes between path by distance

Lets assume there are 2 shortest path of equal distance between two given nodes. What I want is to group all nodes in between by distance.

Example there are two shortest path in graph:
1) A - B - C - D - E
2) A - B - J - K - E

What I expect final result is like:

B - 1  
C,J - 2  
D, K - 3  
E - 4  

One approach is to find all nodes and then loop in yo find the length, but that is neither efficient nor absolutely correct.

Please refer any docs and reference with your answer as I was unable to find same.

Upvotes: 0

Views: 343

Answers (1)

logisima
logisima

Reputation: 7478

This is an example based on the movie dataset :

MATCH p=allshortestPaths( (bacon:Person {name:"Kevin Bacon"})-[*]-(meg:Person {name:"Meg Ryan"}))
WITH nodes(p) AS path
UNWIND range(0, size(path)-1, 1) AS index
    WITH index, path[index] AS node
    RETURN index, collect(DISTINCT node)
    ORDER BY index ASC

When a path is found, I iterate over it to create a collection of index & node. Then you just to make an aggregation based on the index (the RETURN index, collect(DISTINCT node))

Upvotes: 2

Related Questions