# Separating Returned Nodes

**URL:** <https://community.neo4j.com/t/separating-returned-nodes/40981>\
**Category:** Cypher\
**Created:** [July 6, 2021, 8:32pm UTC](https://community.neo4j.com/t/separating-returned-nodes/40981 "2021-07-06T20:32:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![andy\_hegedus](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/andy_hegedus/32/18744_2.png) [@andy\_hegedus](https://community.neo4j.com/u/andy_hegedus)\
**Post date:** [July 6, 2021, 8:32pm UTC](https://community.neo4j.com/t/separating-returned-nodes/40981/1 "2021-07-06T20:32:47Z")

</div>

Hi,  
The result from a simple query shows three groups of green nodes. Group A is only connected to left blue node, Group B is only connected to the right blue node, and Group C is connected to both.

```auto
MATCH p=(c:cpc)-[:Classified_as]-(a:patent{num:9698021})-[:Classified_as]-(n:cpc)-[:Classified_as]-(b:patent{num:7950348})-[:Classified_as]-(x:cpc)
return p

```

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/a/5/a53ae18fd345c195d875193e401280860776006a.png)

What is the most straightforward way of defining the three group? (Note: the green-green connection for this question)  
c variable should contain all nodes connected to the left blue node including those in n  
n variable should contain the shared nodes  
x variable should contain all connected to the right blue node including those in n.

What I am interested in (think Venn Diagram)  
n  
(c-n)  
(x-n)  
Andy

---

<div class="post-metadata">

**Author:** ![david\_allen](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/david_allen/32/13090_2.png) [@david\_allen](https://community.neo4j.com/u/david_allen)\
**Post date:** [July 7, 2021, 12:33pm UTC](https://community.neo4j.com/t/separating-returned-nodes/40981/2 "2021-07-07T12:33:26Z")

</div>

To define these groups node-for-node exactly how you like may be very difficult to do without simply hand-labeling them.

But the general problem space you're in is made for community detection algorithms. I recommend taking a look at this page and playing with these approaches, because taking a big graph and partitioning it into multiple different groups or "communities" is just fundamentally what these algorithms do.

Will they do it exactly as you want? Questionable -- but if you read the algorithm descriptions, you can make the right algorithm choice and probably get very close to what you want on big datasets.

> **[Community detection - Neo4j Graph Data Science](https://neo4j.com/docs/graph-data-science/current/algorithms/community/)**
>
> This chapter provides explanations and examples for each of the community detection algorithms in the Neo4j Graph Data Science library.
