union find算法 c++

1.原理参考

labuladong-fucking-algorithm/算法思维系列/UnionFind算法详解.md at master · jiajunhua/labuladong-fucking-algorithm · GitHub

2.初级模式

cpp 复制代码
#include <iostream>

class UF {

    public:
    // 记录连通分量

        /* 构造函数,n 为图的节点总数 */
        UF(int n) {
        count = n;
        //    int arr[n];
        parent_arr = new int[n]; 
        for(int i=0; i<n; i++)
        {
            parent_arr[i] = i;
        }
        };
        /* 其他函数 */

        ~UF()
        {
            delete[] parent_arr;
        }

        void union_func(int p, int q);

        int find(int x);

        int getCount();

        bool connect(int p, int q);

    private:
        int count;
        // 节点 x 的节点是 parent[x]
        //  int[] parent;
        int* parent_arr; 

};

void UF::union_func(int p, int q)
{
    int rootP = find(p);
    int rootQ = find(q);

    if(rootQ == rootP)
        return;
    
    parent_arr[rootQ] = rootP;

    count--;

}


int UF::find(int x)
{
    while (parent_arr[x] != x)
    {
        x = parent_arr[x];
    }

    return x;
}


int UF::getCount()
{
    return count;
}


bool UF::connect(int p, int q)
{
    int rootP = find(p);
    int rootQ = find(q);

    return rootP == rootQ;
}


int main()
{
    UF union_find(7);

    union_find.union_func(0, 1);
    union_find.union_func(0, 2);
    union_find.union_func(0, 3);
    union_find.union_func(2, 4);
    union_find.union_func(2, 5);
    union_find.union_func(3, 6);

    std::cout << union_find.getCount() << std::endl;

    return 0;
}
  1. 进阶模式

3.1 平衡性优化

3.2 路径压缩

cpp 复制代码
#include <iostream>

class UF {

    public:
    // 记录连通分量

        /* 构造函数,n 为图的节点总数 */
        UF(int n) {
        count = n;
        //    int arr[n];
        parent_arr = new int[n]; 
        size_arr = new int[n];
        for(int i=0; i<n; i++)
        {
            parent_arr[i] = i;
            size_arr[i] = i;
        }
        };
        /* 其他函数 */

        ~UF()
        {
            delete[] parent_arr;
        }

        void union_func(int p, int q);

        int find(int x);

        int getCount();

        bool connect(int p, int q);

    private:
        int count;
        int* parent_arr;
        int* size_arr; 

};

void UF::union_func(int p, int q)
{
    int rootP = find(p);
    int rootQ = find(q);

    // 小树接到大树下面,较平衡
    if (size_arr[rootP] > size_arr[rootQ]) {
        parent_arr[rootQ] = rootP;
        size_arr[rootP] += size_arr[rootQ];
    } else {
        parent_arr[rootP] = rootQ;
        size_arr[rootQ] += size_arr[rootP];
    }

    count--;

}


int UF::find(int x)
{
    while (parent_arr[x] != x)
    {
        // 进行路径压缩
        parent_arr[x] = parent_arr[parent_arr[x]];
        x = parent_arr[x];
    }

    return x;
}


int UF::getCount()
{
    return count;
}


bool UF::connect(int p, int q)
{
    int rootP = find(p);
    int rootQ = find(q);

    return rootP == rootQ;
}


int main()
{
    UF union_find(7);

    union_find.union_func(0, 1);
    union_find.union_func(0, 2);
    union_find.union_func(0, 3);
    union_find.union_func(2, 4);
    union_find.union_func(2, 5);
    union_find.union_func(3, 6);

    std::cout << union_find.getCount() << std::endl;

    return 0;
}
相关推荐
EnigmaCoder2 分钟前
Java多线程:核心技术与实战指南
java·开发语言
前端极客探险家10 分钟前
告别卡顿与慢响应!现代 Web 应用性能优化:从前端渲染到后端算法的全面提速指南
前端·算法·性能优化
程序员Xu1 小时前
【OD机试题解法笔记】连续出牌数量
笔记·算法·深度优先
CoovallyAIHub1 小时前
单目深度估计重大突破:无需标签,精度超越 SOTA!西湖大学团队提出多教师蒸馏新方案
深度学习·算法·计算机视觉
CoovallyAIHub1 小时前
从FCOS3D到PGD:看深度估计如何快速搭建你的3D检测项目
深度学习·算法·计算机视觉
偷偷的卷1 小时前
【算法笔记 day three】滑动窗口(其他类型)
数据结构·笔记·python·学习·算法·leetcode
北京地铁1号线2 小时前
Zero-Shot(零样本学习),One-Shot(单样本学习),Few-Shot(少样本学习)概述
人工智能·算法·大模型
大白的编程日记.2 小时前
【计算机基础理论知识】C++篇(二)
开发语言·c++·学习
网小鱼的学习笔记2 小时前
python中MongoDB操作实践:查询文档、批量插入文档、更新文档、删除文档
开发语言·python·mongodb
C语言小火车2 小时前
野指针:C/C++内存管理的“幽灵陷阱”与系统化规避策略
c语言·c++·学习·指针