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;
}
相关推荐
yw00yw23 分钟前
常见的设计模式
开发语言·javascript·设计模式
我不是星海1 小时前
RabbitMQ基础入门实战
java·开发语言
重启的码农1 小时前
Windows虚拟显示器MttVDD源码分析 (6) 高级色彩与HDR管理
c++·windows·操作系统
花开富贵ii1 小时前
代码随想录算法训练营四十九天|图论part07
java·数据结构·算法·图论·prim·kruscal
jingfeng5141 小时前
C++多态
开发语言·c++
kyle~2 小时前
C/C++---浮点数与整形的转换,为什么使用sqrt函数时,要给参数加上一个极小的小数(如1e-6)
c语言·开发语言·c++
CoovallyAIHub2 小时前
无需ReID网络!FastTracker凭借几何与场景认知实现多目标跟踪新SOTA,助力智慧交通更轻更快
深度学习·算法·计算机视觉
jokr_2 小时前
C++ STL 专家容器:关联式、哈希与适配器
java·c++·哈希算法
CoovallyAIHub2 小时前
D‘RespNeT无人机图像分割数据集与YOLOv8-DRN模型,实时识别入口与障碍,助力灾后救援
深度学习·算法·计算机视觉
暖苏2 小时前
python-多线程(笔记)(持续更新)
大数据·开发语言·python