union-find-ts
Version:
An immutable Union-Find structure.
120 lines (119 loc) • 5.14 kB
TypeScript
/**
* A Union-Find data structure.
*/
export interface UnionFind<T> {
readonly items: T[];
readonly roots: number[];
readonly ranks: number[];
readonly map: (val: T) => number;
}
/**
* Group the items in a UF structure according to the components built using `link()`.
* @param uf The UnionFind to group
* @returns A list of lists, each one of which is a component in [uf].
*/
export declare const toGroups: <T>(uf: UnionFind<T>) => T[][];
/**
*
* @param uf A component map
* @returns The groups that are greater than size one.
*/
export declare const toConnectedGroups: <T>(uf: UnionFind<T>) => T[][];
declare type HasGroup = {
/**
*
* @param uf A Component Map
* @param num Item number in the component
* @returns true if the roots object has a nonzero value for this item.
*/
<T>(uf: UnionFind<T>, num: number): boolean;
/**
*
* @param uf A Component Map
* @param item Item from the mapped collection
* @returns true if the roots object has a nonzero value for this item.
*/
<T>(uf: UnionFind<T>, item: T): boolean;
};
/**
* Determines whether the item has been grouped yet (might be itself)
* @param uf A Component Map
* @param num Item number in the component
* @returns true if the roots object has a nonzero value for this item.
*/
export declare const hasGroup: HasGroup;
export declare type Linker<V> = {
(item: V): V[];
};
/**
* Create a UnionFind structure for the given items.
* Optional linker and linkItem functions can allow the components to be fully resolved during object construction.
* @param size the number of items in the graph
* @param map Key function mapping item to a unique key
* @param linker optional function that identifies a list of item keys that should be linked to the item number.
*/
export declare function unionFind(size: number, map: (val: number) => number, links: number[][]): UnionFind<number>;
/**
* Create a UnionFind structure for the given items.
* Optional linker and linkItem functions can allow the components to be fully resolved during object construction.
* @param items The list of items in the forest
* @param map Key function mapping item to a unique key
* @param links optional function that identifies a list of item keys that should be linked to the item number.
*/
export declare function unionFind<T>(items: T[], map: (val: T) => number, links: T[][]): UnionFind<T>;
/**
* Create a UnionFind structure for the given items.
* Optional linker and linkItem functions can allow the components to be fully resolved during object construction.
* @param items The list of items in the forest
* @param map Key function mapping item to a unique key
* @param linker optional function that identifies a list of item keys that should be linked to the item number.
*/
export declare function unionFind<T>(items: T[], map: (val: T) => number, linker?: Linker<T>): UnionFind<T>;
/**
* Create a UnionFind structure of the given size. The [items] attribute will be initialized to a list of numbers from one to size + 1.
* Optional linker and linkItem functions can allow the components to be fully resolved during object construction.
* @param size Number of items in the UnionFind
* @param map Map function that maps a number to its name. use (x)=>x as a reasonable default.
* @param linker optional function that identifies a list of item keys that should be linked to the item number.
*/
export declare function unionFind(size: number, map: (val: number) => number, linker?: Linker<number>): UnionFind<number>;
declare type Finder = {
/**
* Find the group number of the item number given in @item.
* The item number can be obtained by calling UnionFind::map.
* @param uf a component map
* @param item the ordinal of the item
* @returns The component number of the item, or the component number if not defined.
*/
<T>(uf: UnionFind<T>, item: number): number;
/**
* @param ufLike an object containing the roots of a UnionFind.
* @param item the ordinal number of the item within @ufLike.
* @returns The component number of the item, or the component number if not defined.
*/
<T extends {
roots: number[];
}>(ufLike: T, item: number): number;
};
/**
* Find group of item in the given UnionFind.
* @param param0 UnionFind object
* @param item Item to find group.
* @returns Group number of [item]
*/
export declare const find: Finder;
export declare const findItem: <T>(uf: UnionFind<T>, item: T) => number;
/**
* Update the roots for the root of the item in question,
* as well as for the items.
*
* Change the rank for the groups, as well.
* @param uf
* @param left
* @param right
*/
export declare function link<T>(uf: UnionFind<T>, left: number, right: number): UnionFind<T>;
export declare const linkItem: <T>(uf: UnionFind<T>, left: T, right: T) => UnionFind<T>;
export declare const linkItemAll: <T>(uf: UnionFind<T>, left: T, right: T[]) => UnionFind<T>;
export declare const linkAll: <T>(uf: UnionFind<T>, left: number, right: number[]) => UnionFind<T>;
export {};