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;
}
相关推荐
hie9889412 分钟前
MATLAB锂离子电池伪二维(P2D)模型实现
人工智能·算法·matlab
Jay_51513 分钟前
C++多态与虚函数详解:从入门到精通
开发语言·c++
路来了15 分钟前
Python小工具之PDF合并
开发语言·windows·python
杰克尼22 分钟前
BM5 合并k个已排序的链表
数据结构·算法·链表
.30-06Springfield1 小时前
决策树(Decision tree)算法详解(ID3、C4.5、CART)
人工智能·python·算法·决策树·机器学习
我不是哆啦A梦1 小时前
破解风电运维“百模大战”困局,机械版ChatGPT诞生?
运维·人工智能·python·算法·chatgpt
xiaolang_8616_wjl1 小时前
c++文字游戏_闯关打怪
开发语言·数据结构·c++·算法·c++20
WJ.Polar1 小时前
Python数据容器-list和tuple
开发语言·python
small_wh1te_coder1 小时前
硬件嵌入式学习路线大总结(一):C语言与linux。内功心法——从入门到精通,彻底打通你的任督二脉!
linux·c语言·汇编·嵌入式硬件·算法·c
FrostedLotus·霜莲2 小时前
C++主流编辑器特点比较
开发语言·c++·编辑器