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.
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'
import {
functionsplitmix32(seed:number): () =>number
Creates a deterministic 32-bit pseudorandom number generator.
Each call advances a private state by 0x9e3779b9. This constant equals
floor(2^32 / phi), where phi is the golden ratio. Addition wraps modulo
2^32. Because the increment is odd, it is coprime to 2^32. The resulting
Weyl sequence visits every 32-bit state once before it repeats.
The mixing function uses the constants 0x21f0aaad and 0x735a2d97 from
the
search
for low avalanche bias. Its XOR-shifts and multiplications by odd integers
are invertible over 32-bit values. The mixer therefore preserves the full
period while diffusing nearby states across the output bits.
Math.imul and the bitwise operators keep the mixing steps in 32-bit
arithmetic. The final unsigned conversion produces an integer from 0 to
2^32 - 1. Dividing by 2^32 maps that integer to [0, 1).
The SplitMix family was introduced by Steele, Lea, and Flood in
Draw an integer uniformly from 0..upperExclusive - 1.
Multiplying a source value by 2^32 produces one unsigned 32-bit draw. When
the requested range does not divide 2^32 evenly, a direct remainder would
make its lowest outcomes slightly more likely. Rejection sampling discards
the incomplete high end of the 32-bit range before taking the remainder, so
every returned integer has exactly the same number of source draws mapping
to it.
The source can be seeded or otherwise injected by the caller. Each value is
validated at the point it is consumed because a stateful source can become
invalid after earlier valid draws.
Consumes one source draw in the common case. The rejection region is always
smaller than upperExclusive, so fewer than half of all draws are rejected
and the expected number of draws is below two.
@param ― random - Function returning values in [0, 1).
@param ― upperExclusive - Exclusive upper bound, from 1 through 2^32.
@throws ― {PrngInvalidInputError} With code INVALID_BOUND when the bound is
not an integer in 1..2^32, or INVALID_SOURCE_VALUE when the source
returns a value outside [0, 1).
Return a uniformly shuffled copy of values using Fisher-Yates.
Runs in O(values.length) time and uses O(values.length) space for the copy.
The input is never mutated.
@throws ― {PrngInvalidInputError} With code INVALID_SOURCE_VALUE when the
random source returns a value outside [0, 1).
shuffled } from'knotkit/sampling'
const
constrandom: () =>number
random=
functionsplitmix32(seed:number): () =>number
Creates a deterministic 32-bit pseudorandom number generator.
Each call advances a private state by 0x9e3779b9. This constant equals
floor(2^32 / phi), where phi is the golden ratio. Addition wraps modulo
2^32. Because the increment is odd, it is coprime to 2^32. The resulting
Weyl sequence visits every 32-bit state once before it repeats.
The mixing function uses the constants 0x21f0aaad and 0x735a2d97 from
the
search
for low avalanche bias. Its XOR-shifts and multiplications by odd integers
are invertible over 32-bit values. The mixer therefore preserves the full
period while diffusing nearby states across the output bits.
Math.imul and the bitwise operators keep the mixing steps in 32-bit
arithmetic. The final unsigned conversion produces an integer from 0 to
2^32 - 1. Dividing by 2^32 maps that integer to [0, 1).
The SplitMix family was introduced by Steele, Lea, and Flood in
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.
Draw an integer uniformly from 0..upperExclusive - 1.
Multiplying a source value by 2^32 produces one unsigned 32-bit draw. When
the requested range does not divide 2^32 evenly, a direct remainder would
make its lowest outcomes slightly more likely. Rejection sampling discards
the incomplete high end of the 32-bit range before taking the remainder, so
every returned integer has exactly the same number of source draws mapping
to it.
The source can be seeded or otherwise injected by the caller. Each value is
validated at the point it is consumed because a stateful source can become
invalid after earlier valid draws.
Consumes one source draw in the common case. The rejection region is always
smaller than upperExclusive, so fewer than half of all draws are rejected
and the expected number of draws is below two.
@param ― random - Function returning values in [0, 1).
@param ― upperExclusive - Exclusive upper bound, from 1 through 2^32.
@throws ― {PrngInvalidInputError} With code INVALID_BOUND when the bound is
not an integer in 1..2^32, or INVALID_SOURCE_VALUE when the source
returns a value outside [0, 1).
Draw an integer uniformly from 0..upperExclusive - 1.
Multiplying a source value by 2^32 produces one unsigned 32-bit draw. When
the requested range does not divide 2^32 evenly, a direct remainder would
make its lowest outcomes slightly more likely. Rejection sampling discards
the incomplete high end of the 32-bit range before taking the remainder, so
every returned integer has exactly the same number of source draws mapping
to it.
The source can be seeded or otherwise injected by the caller. Each value is
validated at the point it is consumed because a stateful source can become
invalid after earlier valid draws.
Consumes one source draw in the common case. The rejection region is always
smaller than upperExclusive, so fewer than half of all draws are rejected
and the expected number of draws is below two.
@param ― random - Function returning values in [0, 1).
@param ― upperExclusive - Exclusive upper bound, from 1 through 2^32.
@throws ― {PrngInvalidInputError} With code INVALID_BOUND when the bound is
not an integer in 1..2^32, or INVALID_SOURCE_VALUE when the source
returns a value outside [0, 1).
Return true when x and y belong to the same set. O(alpha(n))
amortized.
@throws ― {DisjointSetInvalidInputError} When either element is absent.
areConnected('account-0', 'account-9999') // true
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.