# Neo4j's connected component algorithm

**URL:** <https://community.neo4j.com/t/neo4js-connected-component-algorithm/18734>\
**Category:** Graph Data Science / Graph Analytics\
**Tags:** apoc\
**Created:** [May 16, 2020, 3:25am UTC](https://community.neo4j.com/t/neo4js-connected-component-algorithm/18734 "2020-05-16T03:25:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ruanlovelin](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ruanlovelin/32/720_2.png) [@ruanlovelin](https://community.neo4j.com/u/ruanlovelin)\
**Post date:** [May 16, 2020, 3:25am UTC](https://community.neo4j.com/t/neo4js-connected-component-algorithm/18734/1 "2020-05-16T03:25:45Z")

</div>

For example, there are A, B, C, D, E, F, G, H, I, J, K, L, M, N 14 nodes. A, B, C, D, F, L have a common attribute value x is defined as x community. C, D, E, G, L have a common attribute value y is defined as y community. F, H, I, M have a common attribute value z is defined as z community. H, I, J, K, M, N have a common attribute value w is defined as w community. According to neo4j's connected component algorithm, these 14 nodes are considered as a community. I expect to divide the community according to another standard. I hope to use the number of overlapping nodes as the community division standard. If the "overlapping number is 3" is used as the standard for community division, then A, B, C, D, E, F, G, L are a community, and F, H, I, J, K, M, N As a community.Thank you very much!

 ![IMG_20200516_111343](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/f/fab4093f00a8d25e2f91322217206e4bb2695a22.jpeg)

---

<div class="post-metadata">

**Author:** ![ruanlovelin](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ruanlovelin/32/720_2.png) [@ruanlovelin](https://community.neo4j.com/u/ruanlovelin)\
**Post date:** [May 19, 2020, 10:36pm UTC](https://community.neo4j.com/t/neo4js-connected-component-algorithm/18734/2 "2020-05-19T22:36:44Z")

</div>

I thought of a solution, but there are still some confusion:

1. Save the node id of each community in a list:match(m:property)\<--(n:nodes)  
with m.community,collect(n.id) as list
2. Use apoc.coll.intersection (first, second) to calculate the intersection of the lists in pairs. If the number of elements in the intersection is\> 3, then merge: apoc.coll.union (first, second)
3. Use apoc.periodic.commit () until the number of elements in the intersection \<3.

Code show as below:  
CALL apoc.periodic.commit("match(m:property)\<--(n:nodes)  
with m.community,collect(n.id) as list  
where size(apoc.coll.intersection( **first** , **second** ))\>2 LIMIT {limit}  
with **first** , **second**  
apoc.coll.union( **first** , **second** )  
return size(apoc.coll.intersection(first,second))\>2",  
{limit:5}  
)

What to replace the 'first' and 'second'?Please give me some advice.Thank you very much!
