in reply to How to make buckets of like data
Plenty of algorithms available, no need to reinvent the wheel. =)
see also older discussion and cpan connected component.
Cheers Rolf