To expand on st0le answer a little ...
So you have a list of items:
a, b, c, d, e, f
And a list of relationships:
a-b
c-d
d
Initialize by placing each item in its group.
Then iterate over the list of your relationships.
For each relationship, find the group in which each member is a member, and then combine these groups.
So in this example:
1: init -> {a}, {b}, {c}, {d}, {e}, {f} 2: ab -> {a,b}, {c}, {d}, {e}, {f} 3: cd -> {a,b}, {c,d}, {e}, {f} 4: de -> {a,b}, {c,d,e}, {f}
Obviously, you have to check all your relationships. Depending on how you implement โfinding,โ part of this will affect the performance of your algorithm. So what you really want to know is the fastest way to find an element in a set of element groups. A naive approach will do this in O (n). You can improve this by keeping a record of which group this item is in. Then, of course, when you combine the two groups, you will need to update your record. But this is still useful because you can combine a smaller group into a larger group, which saves on how many records you need to update.