billDickens
billDickens

Reputation: 111

Connecting the end of a set to the beginning

We have a set of nodes that are connected. Each node has a link to the next node in the chain. When the chain runs out, that end node just hangs out there. See the graphic below.

Node path

Each of these nodes has the same level, so as long as they are in the chain, they have the same number. So what I am hoping to do is come up with a cypher query that builds a link between the max ID and the MIN ID that share the same line number. So basically connecting the end, with the beginning. Is there a clever way to do this ?

Upvotes: 0

Views: 51

Answers (1)

Graphileon
Graphileon

Reputation: 5385

Your question lacks some clarity, but what about thinking along the lines below ?

// find all levels in your dataset of nodes in the chains
MATCH (n)
WHERE (n)-[:NEXT]-()
WITH COLLECT(DISTINCT n.level) AS levels
UNWIND levels AS level

// for each level, find the chain
MATCH (start {level:level})-[:NEXT*]->(end {level:level})
WHERE NOT (
            ({level:level})-[:NEXT]->(start) 
            OR 
            (end)-[:NEXT]->({level:level})
         )

// connect end to start 
MERGE (end)-[:MYRELTYPE]->(start)

Upvotes: 1

Related Questions