Disjoint Set (Union-Find) data structure A disjoint-set data structure (also called union-find) keeps track of a set of elements partitioned into a number of disjoint (non-overlapping) subsets. It supports two operations: union (join two subsets) and find (determine which subset an element is in).

Example

const ds = new DisjointSet(5);
ds.union(0, 1);
ds.union(2, 3);
ds.isConnected(0, 1); // true
ds.isConnected(0, 2); // false
ds.union(1, 2);
ds.isConnected(0, 3); // true

Hierarchy

  • DisjointSet

Constructors

Properties

Methods

Constructors

Properties

count: number
parent: number[]
rank: number[]

Methods

  • Finds the root of the set containing element x Uses path compression for optimization

    Parameters

    • x: number

    Returns number

  • Checks if elements x and y are in the same set

    Parameters

    • x: number
    • y: number

    Returns boolean

  • Unites the sets containing elements x and y Uses union by rank for optimization

    Parameters

    • x: number
    • y: number

    Returns boolean

Generated using TypeDoc