cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Convert Graph to a Tree

jeet
Node Link

I have this graph


it has intermediate nodes of yellow colors

I want convert this to a tree in way that it clones Engine node and its children as 2 diff engine nodes one under Train and one under car as it has 2 incoming links

is something like this possible?

Thanks

5 REPLIES 5

ameyasoft
Graph Maven
If I understood correctly, you can try this:

Get the id of your Engine node. I recreated your scenario and in my case id = 13.

MATCH (a) where id(a) = 13  
CALL apoc.path.spanningTree(a,{maxLevel:2}) YIELD path
return path

Result:

Thanks @ameyasoft however I was looking to have 2 diff engine nodes one under car and one under train.

Your data model shows one Engine node is connected to both Train and Car nodes. If you need two separate Engines then you should have:

(Train-[]- (Engine {type: "train"}) 
(Car)-[]-(Engine {type: "car"})

I was able to find apoc.convert.toTree() this solves my issue i get 2 copies of engines

MATCH p=(n:Part {partNumber:"100", revision: "A"})-[:HAS_BOM_PROPERTY|WITH_PART*]->(m) WITH COLLECT(p) AS ps CALL apoc.convert.toTree(ps) yield value RETURN value;

however i need to get rid the intermediate yellow nodes now.

https://run.mocky.io/v3/75583fed-56f5-4c0e-b378-a768ce53b015

MATCH (p1:Part)-[r1]->(b:BOM)-[r2]->(p2:Part)
MERGE (p1)-[rBom:BOM]->(p2)
SET rBom = b
DETACH DELETE b