# Clustering Algorithm for a congruence relation?

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.

-