Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/data-structures/disjoint-set/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe('DisjointSet', () => {
beforeEach(() => {
ds = new DisjointSet(5);
});

test('initialize', () => {
for (let i = 0; i < 5; i += 1) {
// Act & Assert
Expand Down
27 changes: 14 additions & 13 deletions src/data-structures/disjoint-set/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// Explanation:
// - Hello Byte: https://youtu.be/92UpvDXc8fs?si=rEbdXNPEIm_g97NK&t=237

export class DisjointSet {
#parent;

#rank;

constructor(size: number) {
this.#parent = Array.from({ length: size }, (_, i) => i);
this.#rank = new Uint32Array(size);
constructor(n: number) {
this.#parent = Array.from({ length: n }, (_, i) => i);
this.#rank = new Uint32Array(n);
}

find(x: number) {
Expand All @@ -18,19 +19,19 @@ export class DisjointSet {
return this.#parent[x];
}

union(a: number, b: number) {
const rootA = this.find(a);
const rootB = this.find(b);
union(x: number, y: number) {
const rootX = this.find(x);
const rootY = this.find(y);

if (rootA === rootB) return false;
if (rootX === rootY) return false;

if (this.#rank[rootA] < this.#rank[rootB]) {
this.#parent[rootA] = rootB;
} else if (this.#rank[rootA] > this.#rank[rootB]) {
this.#parent[rootB] = rootA;
if (this.#rank[rootX] < this.#rank[rootY]) {
this.#parent[rootX] = rootY;
} else if (this.#rank[rootX] > this.#rank[rootY]) {
this.#parent[rootY] = rootX;
} else {
this.#parent[rootB] = rootA;
this.#rank[rootA] += 1;
this.#parent[rootY] = rootX;
this.#rank[rootX] += 1;
}

return true;
Expand Down