简体   繁体   English

基于数组的不交集数据结构的时间复杂度

[英]Time Complexity of Array based Disjoint-Set data structure

I was solving this question on CodeChef and going through the editorial . 我在CodeChef上解决了这个问题,并进行了社论

Here's the pseudo-code for the implemented disjoint-set algorithm : 这是已实现的不交集算法的伪代码:

Initialize parent[i] = i  
Let S[i] denote the initial array.

int find(int i)
    int j
    if(parent[i]==i)
                return i
    else
        j=find(parent[i])
        //Path Compression Heuristics
        parent[i]=j
        return j

set_union(int i,int j)
    int x1,y1
    x1=find(i)
    y1=find(j)
    //parent of both of them will be the one with the highest score
    if(S[x1]>S[y1])
        parent[y1]=x1
    else if ( S[x1] < S[y1])
        parent[x1]=y1

solve()
    if(query == 0)
        Input x and y
        px = find(x)
        py = find(y)
        if(px == py)
            print "Invalid query!"
        else
            set_union(px,py)
    else
        Input x.
        print find(x)

What is the time complexity of union and find ? union find的时间复杂度是多少?

IMO, the time complexity of find is O(depth) , so in worst case, if I am not using path-comression, the complexity turns out to be O(n). IMO, find的时间复杂度为O(depth) ,因此在最坏的情况下,如果我不使用路径约束,则复杂度为O(n)。 Since union also uses find , it also has the complexity of O(n). 由于union还使用find ,因此它也具有O(n)的复杂度。 If instead we throw out the find from union and instead pass the parents of two sets to union , complexity of union is O(1). 相反,如果我们扔出去的find来自union ,而是通过两个集合的家长union ,复杂性union是O(1)。 Please correct me, if I am wrong. 如果我错了,请纠正我。

If path compression is applied, then what is the time complexity? 如果应用路径压缩,那么时间复杂度是多少?

Without path compression : When we use linked list representation of disjoint sets and the weighted-union heuristic, a sequence of m MAKE-SET, UNION by rank , FIND-SET operations takes place where n of which are MAKE-SET operations. 没有路径压缩:当我们使用不相交集的链表表示形式和加权联合启发式时,将发生m个MAKE-SET,UNION by rank,FIND-SET操作的序列,其中n个是MAKE-SET操作。 So , it takes O(m+ nlogn). 因此,它需要O(m + nlogn)。

With only path compression : The running time is theta( n + f * ( 1 + (log (base( 2 + f/n)) n ) ) ) where f is no of find sets operations and n is no of make set operations 仅使用路径压缩:运行时间为theta(n + f *(1 +(log(base(2(f + n))n)))),其中f为查找集操作数,n为make set操作数

With both union by rank and path compression: O( m*p(n )) where p(n) is less than equal to 4 结合按等级压缩和路径压缩:O(m * p(n))其中p(n)小于等于4

The time complexity of both union and find would be linear if you use neither ranks nor path compression, because in the worst case, it would be necessary to iterate through the entire tree in every query. 如果既不使用等级也不使用路径压缩,则并集和查找的时间复杂度将是线性的,因为在最坏的情况下,有必要在每个查询中遍历整个树。

If you use only union by ranks, without path compression, the complexity would be logarithmic. 如果仅按等级使用联合,而没有路径压缩,则复杂度将是对数的。
The detailed solution is quite difficult to understand, but basically you wouldn't traverse the entire tree, because the depth of the tree would only increase if the ranks of the two sets are equal. 详细的解决方案很难理解,但是基本上您不会遍历整个树,因为只有当两组的秩相等时,树的深度才会增加。 So the iteration would be O(log*n) per query. 因此,每个查询的迭代次数将为O(log * n)。

If you use the path compression optimization, the complexity would be even lower, because it "flattens" the tree, thus reducing the traversal. 如果使用路径压缩优化,则复杂度会更低,因为它可以“拉平”树,从而减少遍历。 Its amortized time per operation is even faster than O(n) , as you can read here . 您可以在此处阅读,它每次操作的摊销时间甚至比O(n)还要快

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM