带你读《图解算法小抄》十、不相交集(2)

简介: 带你读《图解算法小抄》十、不相交集(2)

带你读《图解算法小抄》十、不相交集(1)https://developer.aliyun.com/article/1348197?groupCode=tech_library


DisjointSet

export default class DisjointSet {
  /**
   * @param {function(value: *)} [keyCallback]
   */
  // 构造函数,参数是键回调函数
  constructor(keyCallback) {
    this.keyCallback = keyCallback; // 键回调函数
    this.items = {}; // 存储元素
  }
  /**
   * @param {*} itemValue
   * @return {DisjointSet}
   */
  // 创建新的集合
  makeSet(itemValue) {
    const disjointSetItem = new DisjointSetItem(itemValue, this.keyCallback);
    if (!this.items[disjointSetItem.getKey()]) {
      // 如果元素尚未存在,添加新元素
      this.items[disjointSetItem.getKey()] = disjointSetItem;
    }
    return this;
  }
  /**
   * 查找集合的表示节点。
   *
   * @param {*} itemValue
   * @return {(string|null)}
   */
  find(itemValue) {
    const templateDisjointItem = new DisjointSetItem(itemValue, this.keyCallback);
    // 尝试查找元素本身
    const requiredDisjointItem = this.items[templateDisjointItem.getKey()];
    if (!requiredDisjointItem) {
      return null;
    }
    return requiredDisjointItem.getRoot().getKey();
  }
  /**
   * 按秩合并。
   *
   * @param {*} valueA
   * @param {*} valueB
   * @return {DisjointSet}
   */
  union(valueA, valueB) {
    const rootKeyA = this.find(valueA);
    const rootKeyB = this.find(valueB);
    if (rootKeyA === null || rootKeyB === null) {
      throw new Error('一个或两个值不在集合中');
    }
    if (rootKeyA === rootKeyB) {
      // 如果两个元素已经在同一集合中,只需要返回其键
      return this;
    }
    const rootA = this.items[rootKeyA];
    const rootB = this.items[rootKeyB];
    if (rootA.getRank() < rootB.getRank()) {
      // 如果rootB的树更大,那么rootB成为新的根
      rootB.addChild(rootA);
      return this;
    }
    // 如果rootA的树更大,那么rootA成为新的根
    rootA.addChild(rootB);
    return this;
  }
  /**
   * @param {*} valueA
   * @param {*} valueB
   * @return {boolean}
   */
  // 判断两个值是否在同一个集合中
  inSameSet(valueA, valueB) {
    const rootKeyA = this.find(valueA);
    const rootKeyB = this.find(valueB);
    if (rootKeyA === null || rootKeyB === null) {
      throw new Error('一个或两个值不在集合中');
    }
    return rootKeyA === rootKeyB;
  }}

2. 参考文献

维基百科

Abdul Bari 在 YouTube 上的教程

相关文章
|
9月前
|
存储 缓存 算法
带你读《图解算法小抄》一、链表(1)
带你读《图解算法小抄》一、链表(1)
带你读《图解算法小抄》一、链表(1)
|
1月前
|
存储 JavaScript 前端开发
【经典算法】LeetCode350:两个数组的交集 II(Java/C/Python3/JavaScript实现含注释说明,Easy)
【经典算法】LeetCode350:两个数组的交集 II(Java/C/Python3/JavaScript实现含注释说明,Easy)
13 1
|
1月前
|
存储 算法
算法训练,牛客.判断是不是平衡二叉树 牛客.最大子矩阵两个数组的交集牛客.数组中两个字符串的最小距离
算法训练,牛客.判断是不是平衡二叉树 牛客.最大子矩阵两个数组的交集牛客.数组中两个字符串的最小距离
算法训练,牛客.判断是不是平衡二叉树 牛客.最大子矩阵两个数组的交集牛客.数组中两个字符串的最小距离
|
2月前
|
算法
【面试算法——动态规划 20】最长公共子序列&& 不相交的线
【面试算法——动态规划 20】最长公共子序列&& 不相交的线
|
2月前
|
算法 JavaScript
|
8月前
|
人工智能 算法 架构师
再现神作!字节算法小抄官方整版,已助1000+应届生拿到25w+年薪
2023年经济下行趋势明显,程序员出路在哪儿? 今年,毕业人数将达到1158万,导致很多公司招聘非常谨慎、要求也变得非常更高。
再现神作!字节算法小抄官方整版,已助1000+应届生拿到25w+年薪
|
8月前
|
算法
代码随想录算法训练营第五十三天 | LeetCode 1143. 最长公共子序列、1035. 不相交的线、53. 最大子数组和
代码随想录算法训练营第五十三天 | LeetCode 1143. 最长公共子序列、1035. 不相交的线、53. 最大子数组和
41 1
|
8月前
|
存储 算法 Serverless
代码随想录算法训练营第六天 | LeetCode 242.有效的字母异位词、349. 两个数组的交集、202. 快乐数、1. 两数之和
代码随想录算法训练营第六天 | LeetCode 242.有效的字母异位词、349. 两个数组的交集、202. 快乐数、1. 两数之和
54 0
代码随想录算法训练营第六天 | LeetCode 242.有效的字母异位词、349. 两个数组的交集、202. 快乐数、1. 两数之和
|
2月前
|
存储 算法 vr&ar
☆打卡算法☆LeetCode 160. 相交链表 算法解析
☆打卡算法☆LeetCode 160. 相交链表 算法解析
|
8月前
|
SQL 算法 架构师
字节算法中了80%!靠着这份GitHub上的算法小抄,成功斩获Offer
前言 最近,GitHub上的算法小抄又火了!已经有不少人靠它手撕算法题,拿下了字节、腾讯等大厂offer

热门文章

最新文章