Create a disjoint set over the dense integer range 0..size - 1.
Parent pointers and ranks are stored directly in typed arrays, with no
element-to-index Map or element array. Calling append adds and returns
the next dense index.
@param ― size - Initial number of elements.
@throws ― {DisjointSetInvalidInputError} When size is not a safe integer in
the supported Int32Array index range.
Create a disjoint set over the dense integer range 0..size - 1.
Parent pointers and ranks are stored directly in typed arrays, with no
element-to-index Map or element array. Calling append adds and returns
the next dense index.
@param ― size - Initial number of elements.
@throws ― {DisjointSetInvalidInputError} When size is not a safe integer in
the supported Int32Array index range.
backed by one { parent, rank } object per
element, optionally pre-populated with elements that each start in their own
singleton set.
Merges follow union by rank. The shorter tree is attached under the taller
one, so tree height grows only when two trees of equal rank meet. Lookups
follow path halving, a single-pass compression that repoints every node it
visits to its grandparent and then continues from that grandparent. Together
they bound each operation at O(alpha(n)) amortized time.
Prefer
indexedDisjointSet
when elements are the dense integer range
0..size - 1, or
structOfArraysDisjointSet
for large generic element counts.
@param ― elements - Elements to insert, each in its own singleton set.
@throws ― {DisjointSetInvalidInputError} When elements yields the same element twice.
backed by one { parent, rank } object per
element, optionally pre-populated with elements that each start in their own
singleton set.
Merges follow union by rank. The shorter tree is attached under the taller
one, so tree height grows only when two trees of equal rank meet. Lookups
follow path halving, a single-pass compression that repoints every node it
visits to its grandparent and then continues from that grandparent. Together
they bound each operation at O(alpha(n)) amortized time.
Prefer
indexedDisjointSet
when elements are the dense integer range
0..size - 1, or
structOfArraysDisjointSet
for large generic element counts.
@param ― elements - Elements to insert, each in its own singleton set.
@throws ― {DisjointSetInvalidInputError} When elements yields the same element twice.
Collect every set into a Map keyed by representative. Members appear in
insertion order. O(n * alpha(n)) time and O(n) space. The result is a copy
and does not track later mutations.
every element is a dense integer in `0..size - 1`, or `append` can mint the next one, so no value-to-index registry is needed; at 100,000 elements each measured workload runs 4.2x to 138x faster than the generic layouts
5 bytes of typed-array storage per initial element and no per-element Map entry; `append` grows geometrically and can reserve up to twice the current size
objectDisjointSet
kind object
identifiers are arbitrary, sparse, or non-numeric, and a node object per element is worth inspecting while debugging; the object and struct-of-arrays layouts stay within the same order of magnitude at 100,000 elements
5 bytes of node storage plus one Map entry per element, and one heap object allocation per element
structOfArraysDisjointSet
kind struct-of-arrays
identifiers are arbitrary, sparse, or non-numeric, n is at or above 100,000, and no per-element node object is ever read
5 bytes of node storage plus one Map entry and one array slot per element, with typed-array growth on insert
Every error this module throws is DisjointSetInvalidInputError or DisjointSetParseError, with a code from a closed, exported list. Branch on the code; the message is for people.
Use knotkit/disjoint-set for this task. If Knotkit is missing, follow https://knotkit.jkomyno.dev/getting-started.md#install-from-source. Import from 'knotkit/disjoint-set'.
Exports:
- `indexedDisjointSet` → `IndexedDisjointSet`: Dense integer disjoint set over `0..size - 1`: parent and rank in typed arrays, no element-to-index registry; `append` mints the next index.
- `objectDisjointSet` → `GrowableDisjointSet<T>`: Growable disjoint set over any values, keeping one `{ parent, rank }` object per element and a registry from value to dense index.
- `structOfArraysDisjointSet` → `GrowableDisjointSet<T>`: Growable disjoint set over any values, keeping parent and rank in typed arrays behind the same value-to-index registry.
Pick the layout by these rules:
- `indexedDisjointSet` (kind 'indexed') when every element is a dense integer in `0..size - 1`, or `append` can mint the next one, so no value-to-index registry is needed; at 100,000 elements each measured workload runs 4.2x to 138x faster than the generic layouts
- `objectDisjointSet` (kind 'object') when identifiers are arbitrary, sparse, or non-numeric, and a node object per element is worth inspecting while debugging; the object and struct-of-arrays layouts stay within the same order of magnitude at 100,000 elements
- `structOfArraysDisjointSet` (kind 'struct-of-arrays') when identifiers are arbitrary, sparse, or non-numeric, n is at or above 100,000, and no per-element node object is ever read
Inputs and their sources:
- element encoding for a binary artifact of a generic layout, which no artifact carries back: `ElementCodec<T>` from `knotkit/wire`
- element validation for a JSON artifact, whose projections carry no element types: `(value: unknown) => value is T` from `the caller`
Use something else when:
- look up a value by key, or check whether a single value is present → Set, Map
- sort edges by weight before merging them one by one → Array.prototype.sort with a comparator
- read membership as it was at an earlier moment while the set keeps changing → knotkit/snapshot-set
- split a group apart, or undo a merge → rebuild the structure from the unions you kept, because a merge is never undone
- walk a graph's adjacency, or find the shortest path → knotkit/graph
Errors are DisjointSetInvalidInputError or DisjointSetParseError with a `code` property. Branch on `error.code`, never on the message. Save and load through `knotkit/disjoint-set/wire` (serializeDisjointSet, deserializeDisjointSet, serializeDisjointSetJson, deserializeDisjointSetJson).
Full page: https://knotkit.jkomyno.dev/modules/disjoint-set.md
Types: node_modules/knotkit/disjoint-set/ and https://knotkit.jkomyno.dev/reference/knotkit/disjoint-set/