@thi.ng/disjoint-set
Version:
TypedArray-based disjoint set implementation with quick union & path compression
62 lines • 1.87 kB
TypeScript
/**
* Typed array based Disjoint Set implementation with quick union and path
* compression, after Sedgewick & Wayne.
*
* @remarks
* References:
*
* - https://en.wikipedia.org/wiki/Disjoint-set_data_structure
* - https://algs4.cs.princeton.edu/lectures/15UnionFind-2x2.pdf
*/
export declare class DisjointSet {
roots: Uint32Array;
ranks: Uint8Array;
count: number;
/**
* Creates new instance with `n` initial singular subsets.
*
* @param n - initial capacity, ID range `[0,n)`
*/
constructor(n: number);
/**
* Returns canonical ID (tree root) for given `id`. Unless `id`
* already is unified with some other ID, this will always return
* `id` itself (since each node is initially its own root).
*
* @param id - node ID
*/
canonical(id: number): number;
/**
* Connects combines the trees of the given two node IDs and returns
* the new resulting canonical tree root ID.
*
* @param a - node ID
* @param b - node ID
*/
union(a: number, b: number): number;
/**
* Returns true, if the given two nodes belong to the same tree /
* subset.
*
* @param a - node ID
* @param b - node ID
*/
unified(a: number, b: number): boolean;
/**
* Returns a `Map` of all subsets (connected components) with their
* canonical tree root IDs as keys and arrays of node IDs as values.
*
* @remarks
* If only the number of subsets is required, use the `count`
* property of this class instance instead (O(1), updated with each
* call to {@link DisjointSet.union}).
*/
subsets(): Map<number, number[]>;
}
/**
* Creates a new {@link DisjointSet} with capacity `n`.
*
* @param n -
*/
export declare const defDisjointSet: (n: number) => DisjointSet;
//# sourceMappingURL=index.d.ts.map