# DisjointSet

Defined in: [packages/disjoint-set/src/disjoint-set.ts:51](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/disjoint-set/src/disjoint-set.ts#L51)

A collection of elements partitioned into disjoint sets, also known as union-find.

Every element belongs to exactly one set, and each set is identified by one of its members, its representative. Two elements are connected when they resolve to the same representative. Which member acts as the representative is an implementation detail. It can change after any `unite` call, so treat it as a comparable token rather than a stable identity.

Elements are compared with `SameValueZero`, the same equality that `Map` and `Set` use. `NaN` equals itself, and `0` equals `-0`.

All implementations mutate in place. Complexities below are stated for `n` elements. `alpha` is the inverse Ackermann function, which stays below 5 for every practical input size, so the amortized bounds are constant in practice.

The type parameter is invariant: a mutable container both consumes and produces `T`. Every callable member is declared in property syntax so `strictFunctionTypes` keeps the parameters that mention `T` contravariant; method-syntax parameters would compare bivariantly and let `DisjointSet<Dog>` widen into `DisjointSet<Animal>`, defeating the invariance `in out T` exists to enforce. The members whose parameters mention `T` in neither position follow the same syntax so the interface reads uniformly.

## Extended by

* [`GrowableDisjointSet`](/reference/knotkit/disjoint-set/interfaces/growabledisjointset/)
* [`IndexedDisjointSet`](/reference/knotkit/disjoint-set/interfaces/indexeddisjointset/)

## Type Parameters

### T

`T`

## Properties

### kind

> `readonly` **kind**: `"object"` | `"indexed"` | `"struct-of-arrays"`

Defined in: [packages/disjoint-set/src/disjoint-set.ts:53](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/disjoint-set/src/disjoint-set.ts#L53)

Stable identifier for the implementation’s storage layout.

***

### size

> `readonly` **size**: `number`

Defined in: [packages/disjoint-set/src/disjoint-set.ts:55](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/disjoint-set/src/disjoint-set.ts#L55)

Number of elements across all sets. O(1).

***

### setCount

> `readonly` **setCount**: `number`

Defined in: [packages/disjoint-set/src/disjoint-set.ts:57](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/disjoint-set/src/disjoint-set.ts#L57)

Number of disjoint sets. O(1).

***

### has

> `readonly` **has**: (`element`) => `boolean`

Defined in: [packages/disjoint-set/src/disjoint-set.ts:59](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/disjoint-set/src/disjoint-set.ts#L59)

Return `true` when the element belongs to the disjoint set. O(1) average.

#### Parameters

##### element

`T`

#### Returns

`boolean`

***

### unite

> `readonly` **unite**: (`x`, `y`) => `boolean`

Defined in: [packages/disjoint-set/src/disjoint-set.ts:67](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/disjoint-set/src/disjoint-set.ts#L67)

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

#### Parameters

##### x

`T`

##### y

`T`

#### Returns

`boolean`

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

#### Throws

When either element is absent.

***

### areConnected

> `readonly` **areConnected**: (`x`, `y`) => `boolean`

Defined in: [packages/disjoint-set/src/disjoint-set.ts:74](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/disjoint-set/src/disjoint-set.ts#L74)

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

#### Parameters

##### x

`T`

##### y

`T`

#### Returns

`boolean`

#### Throws

When either element is absent.

***

### find

> `readonly` **find**: (`element`) => `T`

Defined in: [packages/disjoint-set/src/disjoint-set.ts:81](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/disjoint-set/src/disjoint-set.ts#L81)

Return the representative of the set containing the element. O(alpha(n)) amortized.

#### Parameters

##### element

`T`

#### Returns

`T`

#### Throws

When the element is absent.

***

### elements

> `readonly` **elements**: () => `IterableIterator`<`T`>

Defined in: [packages/disjoint-set/src/disjoint-set.ts:88](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/disjoint-set/src/disjoint-set.ts#L88)

Iterate over every element in insertion order. O(1) per step, O(n) total.

The iterator covers the elements present when the call was made, so adding elements while iterating does not extend the walk.

#### Returns

`IterableIterator`<`T`>

***

### sets

> `readonly` **sets**: () => `Map`<`T`, `T`\[]>

Defined in: [packages/disjoint-set/src/disjoint-set.ts:94](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/disjoint-set/src/disjoint-set.ts#L94)

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.

#### Returns

`Map`<`T`, `T`\[]>
