Take the 2-minute tour ×
Computational Science Stack Exchange is a question and answer site for scientists using computers to solve scientific problems. It's 100% free, no registration required.

Say we are given a congruence relation$~\sim$ in a dataset with $n$ elements. I am looking for an algorithm for optimally sorting the $n$ elements into $m$ clusters according to given congruence relations. For instance if the data contains ${a,b,c,d,e,f,g,h}$, and: $$a\sim b,\ d\sim b,\ e \sim h,\ f \sim c$$ The data should be sorted into the following clusters: $$\{a,b,d\},\ \{c,f\},\ \{e,h\},\ \{g\}$$ As said I'm looking for an efficient algorithm to solve this, I am led to believe this can be done in $O(n)$, but I can't seem to work out the details.

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

I've always heard this referred to as a "Union Find". It's described here, as well as the optimizations you can do to beat the naive implementation: http://www.algorithmist.com/index.php/Union_Find

share|improve this answer
2  
For further reference, a great presentation on the subject can be found here –  nbubis Feb 19 '13 at 21:26
add comment

Write your relation as a sparse graph and use a "connected components" function, like this.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.