# Get Leaf Nodes for all Nodes in directed graph

**URL:** https://community.neo4j.com/t/get-leaf-nodes-for-all-nodes-in-directed-graph/25700
**Category:** Neo4j Graph Platform
**Tags:** cypher, shortest-path
**Created:** [September 21, 2020, 8:22am UTC](https://community.neo4j.com/t/get-leaf-nodes-for-all-nodes-in-directed-graph/25700 "2020-09-21T08:22:40Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![sd.zulfiqar](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/sd.zulfiqar/32/4477_2.png) [@sd.zulfiqar](https://community.neo4j.com/u/sd.zulfiqar)
#### Post date: [September 21, 2020, 8:22am UTC](https://community.neo4j.com/t/get-leaf-nodes-for-all-nodes-in-directed-graph/25700/1 "2020-09-21T08:22:40Z")

</div>

Hi Team,

I have a simple graph that has Nodes which represent duplicate record id in the below form  
Duplicate Id, Original Id  
A,B  
B,C  
C,D  
X,Y  
Y,Z

The directed graph looks like A -\> B -\>C -\>D and I want CSV result that looks like below that will represent the ultimate Original Id  
A,D  
B,D  
C,D  
X,Z  
Y,Z

Have been struggling for more than a day being newbie to graph databases, any help will be highly appreciated.

Thanks

---

<div class="post-metadata">

### Author: ![tony.chiboucas](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/tony.chiboucas/32/5980_2.png) [@tony.chiboucas](https://community.neo4j.com/u/tony.chiboucas)
#### Post date: [September 28, 2020, 6:30pm UTC](https://community.neo4j.com/t/get-leaf-nodes-for-all-nodes-in-directed-graph/25700/2 "2020-09-28T18:30:39Z")

</div>

Here ya go:  
[http://console.neo4j.org/r/vqzcbq](http://console.neo4j.org/r/vqzcbq)

![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/8/8051fcd417a00c54f102c9a403c05c8656acfde8.png)

Cypher:

```auto
MATCH p=(a:Ident)-[:ORIGID*]->(b:Ident)
WHERE NOT (b)-[:ORIGID]->(:Ident)
WITH a, LAST(nodes(p)) as b
RETURN a.name, b.name
ORDER BY a.name

```

![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/4/4d4ac6fbe44d4017684130a92237a5652b255dfb.png)

---

<div class="post-metadata">

### Author: ![sd.zulfiqar](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/sd.zulfiqar/32/4477_2.png) [@sd.zulfiqar](https://community.neo4j.com/u/sd.zulfiqar)
#### Post date: [September 20, 2021, 12:40pm UTC](https://community.neo4j.com/t/get-leaf-nodes-for-all-nodes-in-directed-graph/25700/3 "2021-09-20T12:40:00Z")

</div>

Thank you for the reply and it worked well for small clusters however I have 1 large cluster of 50 connected nodes and it is struggling to make all traversals and failing. Is there any way to prevent further traversals from Origin node if the 1 leaf node is found? All nodes in my cluster are connected to each other so at all times there is only 1 leaf node in a cluster so I can use that fact to have a query that works without failing.

---

<div class="post-metadata">

### Author: ![andrew\_bowman](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/andrew_bowman/32/73_2.png) [@andrew\_bowman](https://community.neo4j.com/u/andrew_bowman)
#### Post date: [September 24, 2021, 11:53pm UTC](https://community.neo4j.com/t/get-leaf-nodes-for-all-nodes-in-directed-graph/25700/4 "2021-09-24T23:53:02Z")

</div>

So it's a directed graph, but not acyclic.

You could use subgraphNodes() from APOC path expanders, but you would need to pre-match to leaf nodes first, collect() them and use them as end nodes, and use a `limit:1` so that it stops looking after a single path to a leaf node is identified.

Alternately, if using Neo4j 4.x, you can use subqueries to limit the expansion to only one leaf node:

```auto
MATCH (a:Ident)
CALL {
  WITH a
  MATCH (a)-[:ORIGID*]->(b:Ident)
  WHERE NOT (b)-[:ORIGID]->()
  RETURN b
  LIMIT 1
}
RETURN a.name as aName, b.name as bName
ORDER BY aName

```

That will find only one leaf node per a node. It won't look for others.

The LIMIT can't be dynamic from a variable, but if you executed a query prior to this to find the number of leaf nodes ( `MATCH (a:Ident) WHERE NOT (a)-[:ORGID]->() RETURN count(a)` ), and you passed that value as a parameter to your query, then you could use `RETURN DISTINCT b LIMIT $leafNodeCount` so it would stop searching when all leaf nodes have been found.
