Convert Graph to a Tree

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?


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


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.

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