What would be the correct algorithm to solve our problem in Neo4j?

Our problem:

Given a one start point (node) or more (nodes) we want to create a directed tree (hierarchical) graph in Neo4j . We have the restriction that all directions are always one-way. Further we know what nodes are connected, but we don't know the end nodes.

We are looking for an algorithm which can find inside graph in a way that given the start point all end points can be reached.

Try something like this to get the end nodes from a starting node:

match(n:Test{key:1})-[:REL*]-(m)
match p = (m)-[:REL]-()
with m, size (collect(p)) as noOfRelationships
where noOfRelationships = 1
return m as endNodes

You could probably also use breadth first search, depth first search, or - if you wanted to be fancy - single source shortest path - Dijkstra Single-Source Shortest Path - Neo4j Graph Data Science

2 Likes