es-next-tools
Version:
A comprehensive utility library for JavaScript and TypeScript that provides a wide range of functions for common programming tasks, including mathematical operations, date manipulations, array and object handling, string utilities, and more.
58 lines (57 loc) • 1.82 kB
JavaScript
/**
* Represents a Union-Find (Disjoint Set) data structure.
*/
export class UnionFind {
parent;
rank;
/**
* Creates a Union-Find structure with a specified size.
* @param {number} size - The number of elements in the structure.
*/
constructor(size) {
this.parent = Array.from({ length: size }, (_, i) => i);
this.rank = Array(size).fill(1);
}
/**
* Finds the root of the set containing the element.
* @param {number} element - The element to find.
* @returns {number} The root of the set.
*/
find(element) {
if (this.parent[element] !== element) {
this.parent[element] = this.find(this.parent[element]); // Path compression
}
return this.parent[element];
}
/**
* Unites two sets.
* @param {number} element1 - The first element.
* @param {number} element2 - The second element.
*/
union(element1, element2) {
const root1 = this.find(element1);
const root2 = this.find(element2);
if (root1 !== root2) {
// Union by rank
if (this.rank[root1] > this.rank[root2]) {
this.parent[root2] = root1;
}
else if (this.rank[root1] < this.rank[root2]) {
this.parent[root1] = root2;
}
else {
this.parent[root2] = root1;
this.rank[root1]++;
}
}
}
/**
* Checks if two elements are in the same set.
* @param {number} element1 - The first element.
* @param {number} element2 - The second element.
* @returns {boolean} True if they are in the same set, false otherwise.
*/
connected(element1, element2) {
return this.find(element1) === this.find(element2);
}
}