# Finding all disconnected subgraphs in a graph

**URL:** <https://community.neo4j.com/t/finding-all-disconnected-subgraphs-in-a-graph/27241>\
**Category:** Graph Data Science / Graph Analytics\
**Created:** [October 15, 2020, 4:49am UTC](https://community.neo4j.com/t/finding-all-disconnected-subgraphs-in-a-graph/27241 "2020-10-15T04:49:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![natakorn.chanpetch](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/natakorn.chanpetch/32/13789_2.png) [@natakorn.chanpetch](https://community.neo4j.com/u/natakorn.chanpetch)\
**Post date:** [October 15, 2020, 4:49am UTC](https://community.neo4j.com/t/finding-all-disconnected-subgraphs-in-a-graph/27241/1 "2020-10-15T04:49:32Z")

</div>

As you can see on a figure, there are two kind of network. The first one on right hand is a main graph and another one is subgraph. My question is Are there any algorithms or techniques (cypher) that can query only the sub-graph?

 ![graph](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/6/6f4c2543687362e8fc1e3a4d65661a2a48d9b478.png)

---

<div class="post-metadata">

**Author:** ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)\
**Post date:** [October 15, 2020, 6:52am UTC](https://community.neo4j.com/t/finding-all-disconnected-subgraphs-in-a-graph/27241/2 "2020-10-15T06:52:31Z")

</div>

Hello @natakorn.chanpetch 🙂

I think you should have a look at the [Weakly Connected Components](https://neo4j.com/docs/graph-data-science/current/algorithms/wcc/) algorithm

Example: [How to remove connected components less than x nodes? - #43 by community.neo4j.k9ft](https://community.neo4j.com/t/how-to-remove-connected-components-less-than-x-nodes/24166/43)

Regards,  
Cobra

---

<div class="post-metadata">

**Author:** ![natakorn.chanpetch](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/natakorn.chanpetch/32/13789_2.png) [@natakorn.chanpetch](https://community.neo4j.com/u/natakorn.chanpetch)\
**Post date:** [October 15, 2020, 8:47am UTC](https://community.neo4j.com/t/finding-all-disconnected-subgraphs-in-a-graph/27241/3 "2020-10-15T08:47:01Z")

</div>

thank you sir, it works very well for your example. I applied it a bit for solving my problem, but it not work well with a large of network (6xx nodes and 6xx relationships). By the way, I will read the wcc algorithm and understand it as fast a s possible. I hope it can tackle the problem. 😃

---

<div class="post-metadata">

**Author:** ![natakorn.chanpetch](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/natakorn.chanpetch/32/13789_2.png) [@natakorn.chanpetch](https://community.neo4j.com/u/natakorn.chanpetch)\
**Post date:** [October 15, 2020, 9:23am UTC](https://community.neo4j.com/t/finding-all-disconnected-subgraphs-in-a-graph/27241/4 "2020-10-15T09:23:13Z")

</div>

I used the wcc algorithm's stream function to display each component (componentId) and set filtering by select only record that has the number of elements between 2 and 10 nodes. Moreover, a result was really good. Thank you again for helping me 😄 @Cobra

---

<div class="post-metadata">

**Author:** ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)\
**Post date:** [October 15, 2020, 9:24am UTC](https://community.neo4j.com/t/finding-all-disconnected-subgraphs-in-a-graph/27241/5 "2020-10-15T09:24:17Z")

</div>

Nice, no problem, I'm happy to help 🙂
