maximum-matching
Version:
Implementation of Blossom's Algorithm for Maximum Matching
39 lines (38 loc) • 1.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Forest = void 0;
const Tree_1 = require("./Tree");
class Forest {
constructor(trees) {
this.trees = trees;
}
static fromRoots(roots) {
const trees = roots.map(Tree_1.Tree.withRoot);
return new Forest(trees);
}
findTreeOf(node) {
return this.trees.find((tree) => tree.has(node));
}
findTreeOrFail(node) {
const found = this.findTreeOf(node);
if (!found)
throw new Error(`Node ${node} was not found in the forest`);
return found;
}
findSubtreeOrFail(node) {
return this.findTreeOrFail(node).findSubtreeOrFail(node);
}
has(node) {
return Boolean(this.findTreeOf(node));
}
pathToItsRootTree(node) {
return this.findTreeOrFail(node).pathToRoot(node);
}
distanceToItsRootTree(node) {
return this.findTreeOrFail(node).distanceToRoot(node);
}
areInTheSameTree(node1, node2) {
return this.findTreeOrFail(node1) === this.findTreeOrFail(node2);
}
}
exports.Forest = Forest;