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

时间:2015-12-25 06:14:24

标签: algorithm disjoint-sets

我在CodeChef上解决了this问题并通过了editorial

这里是实现的不相交集算法的伪代码:

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)

unionfind的时间复杂度是多少?

IMO,find的时间复杂度为O(depth),因此在最坏的情况下,如果我不使用路径压缩,则复杂性变为O(n)。由于union也使用find,因此它也具有O(n)的复杂性。如果相反,我们会从find中删除union,而是将两个集的父项传递给unionunion的复杂度为O(1)。如果我错了,请纠正我。

如果应用路径压缩,那么时间复杂度是多少?

2 个答案:

答案 0 :(得分:1)

没有路径压缩:当我们使用不相交集的链表表示形式和加权联合启发式时,将发生m个MAKE-SET,UNION(按rank),FIND-SET操作的序列,其中n个是MAKE-SET操作。因此,它需要O(m + nlogn)。

仅路径压缩:运行时间为  theta(n + f *(1 +(log(base(2 + f / n))n))) 其中f是没有查找集操作,而n是没有make set操作

通过等级和路径压缩并集:  O(m * p(n)),其中p(n)小于等于4

答案 1 :(得分:0)

如果既不使用排名也不使用路径压缩,则union和find 的时间复杂度将是线性的,因为在最坏的情况下,有必要在每个查询中遍历整个树。

如果你只按队列使用union,没有路径压缩,那么复杂性就是对数。
The detailed solution很难理解,但基本上你不会遍历整个树,因为只有两组的等级相等,树的深度才会增加。因此,每次查询的迭代次数为O(log * n)。

如果使用路径压缩优化,复杂性会更低,因为它会使信息变得平坦。树,从而减少遍历。每次操作的摊销时间甚至比O(n)还要快,因为您可以阅读here