Function Repository Resource:

# MergeFindSet

Provide a merge-find set data structure with standard operations

Contributed by: Daniel Lichtblau
 ResourceFunction["MergeFindSet"][init,"Initialize"] uses the value or values from init to initialize a merge-find set. ResourceFunction["MergeFindSet"][mfs,j,"Find"] gives the canonical value associated to element j in the merge-find set mfs. ResourceFunction["MergeFindSet"][mfs,j,k,"Merge"] merges the subsets from mfs containing elements j and k provided they are distinct.

## Details

A merge-find set is a data structure that contains disjoint subsets of a master set indexed by integers 1n. It supports both fast finding of canonical subset elements and fast merging of subsets.
A merge-find set is also called a disjoint-set or a union-find set.
Merge-find sets are used in graph partitioning algorithms to keep track of unconnected components as they are merged. One such application is in efficient implementations of Kruskal's minimum spanning tree algorithm.
ResourceFunction["MergeFindSet"] only works with explicit indices 1n. A more general one can handle arbitrary set elements, though at the expense of speed and memory requirements.
The initialization can specify either a positive integer n or a disjoint partition of Range[n]. In the former case, the initial subsets are singletons containing the individual integers from 1 to n.
Each subset in a merge-find set has a canonical leader element. Elements that are not leaders have parents.
A leader for a given element is located by following the parent path beginning at that element. A leader is its own parent and this gives a termination condition for locating an elements leader.
A merge-set with n elements is comprised of n pairs of integers. The kth element is {pk,sk}, where pk is the parent of k. When k is its own parent, sk gives the total size of its subset; it is not used in other cases.
In "Merge" operations, smaller subsets are merged into larger.
Whenever a "Find" is performed, elements in the parent path that do not point directly to the leader will be changed to point directly to it.
ResourceFunction["MergeFindSet"] with one argument will automatically assume the operation is "Initialize".
ResourceFunction["MergeFindSet"][mfs,j] for integer j will automatically assume the operation is "Find".
ResourceFunction["MergeFindSet"][mfs,j,k] for integers j and k will automatically assume the operation is "Merge".
The "Find" operation will thread over its second argument.
ResourceFunction["MergeFindSet"] has the attribure HoldFirst.

## Examples

### Basic Examples (5)

Initialize a merge-find set with eight elements and show the data:

 In:= Out= Merge the subsets containing respectively the second and third elements:

 In:= Out= Successively merge the subsets containing the fourth and fifth elements and then the second and fifth elements:

 In:= Out= Find the leader of the subset containing the fifth element:

 In:= Out= Notice that this find operation caused the fifth to point directly to its leader:

 In:= Out= ### Scope (7)

 In:= Out= Find the leaders of the first and last elements:

 In:= Out= Merge the first and last elements:

 In:= Out= Check that the last element now has the same leader as the first:

 In:= Out= Notice that the last now has the same leader as the first:

 In:= Out= Now merge the second and second-to-last elements:

 In:= Out= Merge the tenth and eleventh elements:

 In:= Out= All elements now have the same leader, so the subsets are merged into one set:

 In:= Out= ### Properties and Relations (4)

Merging all elements is not measurably worse than O(n) in speed.

Merge all elements into one set by merging consecutive pairs, doubling the pair size until finished:

 In:= Out= Double the size:

 In:= Out= Double the size again:

 In:= Out= We see substantially the same speed if we merge successively with the first element:

 In:= Out= ### Applications (2)

#### Minimum Spanning Trees: Kruskal's algorithm (2)

A minimum spanning tree for a connected graph is a tree that hits every vertex, has no cycles and is of minimal total edge weight. Kruskal's method for computing the minimum spanning tree of a connected, weighted undirected graph works as follows. Start with the set of all vertices and no connections. Add connecting edges in order of length provided they are between two vertices that are not yet connected. At each such step, merge the connected subsets containing the two respective vertices. It has algorithmic complexity O(e log(e)) where e is the number of edges. We do this for the special case where of a complete graph given by a set of points in the plane, with edge weights being the distances between vertices.

The following code implements Kruskal's algorithm:

 In:= Create random points in the plane:

 In:= Out= Use Kruskal's method to compute their minimum spanning tree:

 In:= Out= Show the graph of the spanning tree:

 In:= Out= For n vertices, we have edges. The time complexity of Kruskal's method is O(e log(e)). The space requirement is O(e).

Use Kruskal's method on a larger point set:

 In:= Out= Compute the minimal spanning tree:

 In:= Out= Show the total branch length of this spanning tree:

 In:= Out= Show the spanning tree:

 In:= Out= Double the size of the point set:

 In:= Out= Show the spanning tree:

 In:= Out= The Kruskal method built into FindSpanningTree has the same asymptotic behavior, but is many times faster:

 In:= Out= Timing ratios over several doublings of the vertex count (hence quadrupling the edge counts) shows the asymptotic behaviors are comparable:

 In:= Out= ## Version History

• 1.0.0 – 05 April 2021