How to get group of connected nodes?

I have a group of nodes connected like this:

Within a group, each of the nodes will be connected to similar nodes by 1 or more hops.

I need to get a group members separately ? Not really worried about how they are connected, as long as they are connected, they should be returned.
How can I do this?

I am expecting this result from the above graph:
[0,1,2,3,4]
[5,6,7,8,9]