createUnionFind
Creates a disjoint-set (union-find) structure: tracks a partition of items into
non-overlapping sets, merging two sets in near-constant time via union, and answering
“are these in the same set?” via connected — the classic tool for clustering items by
an equivalence relation built up incrementally (e.g. grouping records that pairwise match
on some criterion into connected components), without recomputing the whole partition
from scratch after every new match.
Uses union-by-rank and path compression internally, so find/union/connected are all
near-O(1) amortized regardless of how many items or unions have been performed.
Items are compared by Map key semantics (SameValueZero, like Set/Map themselves) —
so object identity for objects, value equality for primitives.
Available since v3.0.8
Import
Section titled “Import”Signature
Section titled “Signature”Returns
Section titled “Returns”UnionFind<T> — A UnionFind.
Examples
Section titled “Examples”Group items by a pairwise match built up incrementally
Section titled “Group items by a pairwise match built up incrementally”union() merges two items into the same set; connected() checks whether two items ended up in the same set, even through a chain of intermediate matches.
find() returns the shared representative of a set
Section titled “find() returns the shared representative of a set”Every item in the same set resolves to the same representative via find() — useful to bucket items by their final group afterwards.
Related Types
Section titled “Related Types”UnionFind
Section titled “UnionFind”A disjoint-set structure created by createUnionFind.
