Union-Find is just a way for you to keep who is the "leader" of some set.
For example, suppose you have 5 people A, B, C, D, E.
Initially each one starts on its own set, so you can code it like this:
for each person in people:
parent[person] = person
this way you can set every person to be the leader of its own set.
this is what it should look like:
{A} {B} {C} {D} {E}
the first operation we need is to be able to find the leader of a set, and this operation is called find
.
then we fall into a property: a leader is someone who is its own parent.
there are two possibilities, or the person is its own parent or it's not.
if it is, then it is the leader of the set.
if it is not, then we will ask the same thing (if it is its own parent) to its parent, and so it goes.
you can code it like this:
find_leader_of(person P){
if(parent[P] == P) return P
else return find_leader_of(parent[P])
}
then we have the union
operation, that's nothing more than turn 2 disjoints sets in one set only.
suppose you have this situation:
{A, B} {C, D} {E}
and you do union(B, D)
, then what happens?
first you need to find the leader of both sets:
fst_leader = find_leader_of(B)
snd_leader = find_leader_of(D)
then you choose any of these leaders to be the leader of the other:
parent[snd_leader] = fst_leader
and this results in:
union(person P1, person P2){
fst_leader = find_leader_of(P!)
snd_leader = find_leader_of(P2)
parent[snd_leader] = fst_leader
}
there are other approaches to improve union-find and other ways to choose who will be leader of who, but this is the basics you must understand in order to know what union-find is about.