Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么不';路径压缩后,我们是否更新不相交集的秩?_C++_Algorithm_Time Complexity_Disjoint Sets - Fatal编程技术网

C++ 为什么不';路径压缩后,我们是否更新不相交集的秩?

C++ 为什么不';路径压缩后,我们是否更新不相交集的秩?,c++,algorithm,time-complexity,disjoint-sets,C++,Algorithm,Time Complexity,Disjoint Sets,我已经用秩启发式和路径压缩为不相交集制作了一个模板 template <typename T> class disJSet { map<T,T> parent; map<T,int> rank; public: //Linear time complexity void makeSet(vector<T> it) { for(T i:it) {

我已经用秩启发式和路径压缩为不相交集制作了一个模板

template <typename T>
class disJSet
{
    map<T,T> parent;
    map<T,int> rank;
    public:
    //Linear time complexity 
    void makeSet(vector<T> it)
    {
        for(T i:it)
        {
            parent[i]=i;
            rank[i]=0;
        }
    }

    //Time complexity of O(log*n)
    T find(T el)
    {
        if(el!=parent[el])
            parent[el]=find(parent[el]);
        
        return parent[el];
    }

    //Time complexity of O(log*n)
    bool unionOp(T a,T b)
    {
        T a_id=find(a);
        T b_id=find(b);

        if(a_id==b_id)
            return false;

        if(rank[a_id]<rank[b_id])
            parent[a_id]=b_id;
        else
        {
            parent[b_id]=a_id;
            if(rank[a_id]==rank[b_id])
            {
                rank[b_id]=rank[b_id]+1;
            }
        }
        return true;    
    }
};
模板
类disJSet
{
映射父对象;
地图等级;
公众:
//线性时间复杂度
void生成集(向量it)
{
for(ti:it)
{
父[i]=i;
秩[i]=0;
}
}
//O的时间复杂度(log*n)
T查找(T el)
{
如果(el!=父项[el])
父[el]=查找(父[el]);
返回父项[el];
}
//O的时间复杂度(log*n)
布尔工会组织(TA、TB)
{
T a_id=查找(a);
T b_id=查找(b);
if(a_id==b_id)
返回false;
如果(rank[a_id]在路径压缩后无法更新rank,因为可能有其他到该根的路径比新路径长度长


路径压缩后不需要更新秩,因为它只需要表示路径长度的上界。

是的,我知道了。我尝试了反例,最后得出结论,路径压缩后不需要更新秩。由于JU上的秩启发式和路径压缩,树实际上是平衡的t单分支实际上不会降低树的高度。它可能会产生最多1的差异。尽管如果在驱动程序代码中显式调用
find()
,这可能不是真的。