disjoint-set is a data structure that tracks a set of elements partitioned into a number of disjoint (non-overlapping) subsets. According to the definition, two disjoint sets are sets whose intersection is the empty set.

In above image: e1, e2 is in Set1 and e3, e4, e5 is in Set2 .

There are two operations for a disjoint-set, union and find , this is the reason why disjoint-set also be called with the name union-find :

make_set() => create a disjoin-set union(e1, e2) => link 'e1' and 'e2' to the same subset find(e) => return the subset's root for 'e'

If we union any two elements from two different sets, then they will be merged into one larger set.

For example, if e2 and e3 are unioned, which also means e1 and e5 in the same set.

With the result of find , we can test whether two elements belong to the same set. If find(e1) equals find(e2) , this means e1 and e2 are in the same set.

This is a useful property of disjoin-set, especially for graph algorithms, in which we need to test whether two nodes have connectivity with each other.

Given the above specs and descriptions, how to implement this data structure by yourself?

You can spend several minutes trying it out by your hand, this is a superior way to study data structures and algorithms.