(about find and union) These two techniques complement each other; applied together, the amortized time per operation is only O(α(n)), where α(n) is the inverse of the function f(n) = A(n,n), and A is the extremely quickly-growing Ackermann function. Since α(n) is the inverse of this function, α(n) is less than 5 for all remotely practical values of n. Thus, the amortized running time per operation is effectively a small constant.
// Do not edit the class below except for// the constructor and the createSet, find,// and union methods. Feel free to add new// properties and methods to the class.classUnionFind {constructor() {// Write your code here.this.arr = [];this.weights = []; }createSet(value) {this.arr[value] = value;this.weights[value] =0;returnnull; }// find(value) {// let currentValue = value;// while (this.arr[currentValue] !== undefined) {// if (this.arr[currentValue] === currentValue) {// return currentValue;// }// currentValue = this.arr[currentValue];// }// return null;// }find(value) {if (value ===undefined) returnnull;if (this.arr[value] === value)return value;returnthis.arr[value] =this.find(this.arr[value]); }union(valueOne, valueTwo) {// Write your code here.constroot1=this.find(valueOne);constroot2=this.find(valueTwo);if (root1 === root2) returnnull;if (root1 ===null|| root2 ===null) returnnull;// this.arr[root1] = root2;console.log( { root1, root2, } )/usingweightsconstweight1=this.weights[root1];constweight2=this.weights[root2];if (weight1 >= weight2) {this.arr[root1] = root2; } else {this.arr[root2] = root1; }returnnull; }}// Do not edit the line below.exports.UnionFind = UnionFind;