Skip to content

Find which items are connected

A disjoint set merges groups as links arrive and answers whether two items are in the same group.

import {
function objectDisjointSet<T>(elements?: Iterable<T>): GrowableDisjointSet<T>

Create a

DisjointSet

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.

objectDisjointSet
} from 'knotkit/disjoint-set'
const
const accounts: GrowableDisjointSet<string>
accounts
=
objectDisjointSet<string>(elements?: Iterable<string> | undefined): GrowableDisjointSet<string>

Create a

DisjointSet

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.

objectDisjointSet
(['ada', 'grace', 'alan', 'edsger'])
// Accounts that share an email address belong to the same person.
const accounts: GrowableDisjointSet<string>
accounts
.
DisjointSet<string>.unite: (x: string, y: string) => boolean

Merge the sets containing x and y. O(alpha(n)) amortized.

@returns ― true when two distinct sets were merged, false when both elements already belonged to the same set.

@throws ― {DisjointSetInvalidInputError} When either element is absent.

unite
('ada', 'grace') // true
const accounts: GrowableDisjointSet<string>
accounts
.
DisjointSet<string>.unite: (x: string, y: string) => boolean

Merge the sets containing x and y. O(alpha(n)) amortized.

@returns ― true when two distinct sets were merged, false when both elements already belonged to the same set.

@throws ― {DisjointSetInvalidInputError} When either element is absent.

unite
('grace', 'alan') // true
const accounts: GrowableDisjointSet<string>
accounts
.
DisjointSet<string>.unite: (x: string, y: string) => boolean

Merge the sets containing x and y. O(alpha(n)) amortized.

@returns ― true when two distinct sets were merged, false when both elements already belonged to the same set.

@throws ― {DisjointSetInvalidInputError} When either element is absent.

unite
('ada', 'alan') // false, already connected
const accounts: GrowableDisjointSet<string>
accounts
.
DisjointSet<string>.areConnected: (x: string, y: string) => boolean

Return true when x and y belong to the same set. O(alpha(n)) amortized.

@throws ― {DisjointSetInvalidInputError} When either element is absent.

areConnected
('ada', 'alan') // true
const accounts: GrowableDisjointSet<string>
accounts
.
DisjointSet<string>.areConnected: (x: string, y: string) => boolean

Return true when x and y belong to the same set. O(alpha(n)) amortized.

@throws ― {DisjointSetInvalidInputError} When either element is absent.

areConnected
('ada', 'edsger') // false
const accounts: GrowableDisjointSet<string>
accounts
.
DisjointSet<string>.setCount: number

Number of disjoint sets. O(1).

setCount
// 2

unite returns false when the two items were already connected, which is how Kruskal and cycle detection count useful edges. Use indexedDisjointSet when the items are dense integer ids.

Modules: knotkit/disjoint-set · API reference