简体   繁体   English

Union-Find 路径压缩效率

[英]Union-Find path compression efficiency

I found some online union-find tutorial depicted path compression technique to get even less than O(log(N)) complexity for find() , below was the path compression implementation in this blog,我发现一些在线union-find 教程描述了路径压缩技术,使find()复杂度低于O(log(N)) find() ,下面是这个博客中的路径压缩实现,

int root (int Arr[], int i) {
    while(Arr[i] != i) {
        Arr[i] = Arr[ Arr[i] ]; 
        i = Arr[i]; 
    }
   return i;
}

I see that this implementation is reducing the path by only half the way and can be made even more compressed using below recursive trick,我看到这个实现只将路径减少了一半,并且可以使用以下递归技巧进一步压缩,

int recurse_root (int Arr[], int i) {
    if ( i == Arr[i] ){
        return i;
    }
    Arr[i] = recurse_root( Arr, Arr[i] )
    return A[i];
}

I wonder if I am missing anything, why this technique was not discussed in most of the online tutorials?我想知道我是否遗漏了什么,为什么大多数在线教程中没有讨论这种技术?

I wonder if I am missing anything, why this technique was not discussed in most of the online tutorials?我想知道我是否遗漏了什么,为什么大多数在线教程中没有讨论这种技术?

Such online tutorials don't use correctly path compression heuristic (as you figured out) or don't mention it because is very difficult to prove true its running time or they mention it without proof.此类在线教程没有正确使用路径压缩启发式(如您所见)或没有提及它,因为很难证明其运行时间是真实的,或者他们在没有证据的情况下提及它。

I am not a fan of online tutorials so i can't tell for true which of the 3 reasons i exposed is the most common one.我不喜欢在线教程,所以我无法确定我暴露的三个原因中哪一个是最常见的。 What i can tell you is that in the book Introduction to Algorithms (Cormen), 3rd Edition, at pages 569 and 570 you can find an excellent explication of path compression heuristic with images included.我可以告诉您的是,在《算法导论》(Cormen),第 3 版,第 569 和 570 页中,您可以找到对包含图像的路径压缩启发式的出色解释。 Also, if you are looking for the proof of actual running time begin the reading at page 573 (heavy reading and worth it).此外,如果您正在寻找实际运行时间的证明,请从第 573 页开始阅读(大量阅读,值得一读)。

Maybe it is not popular in online tutorials but is a must know its existence in a CS degree.也许它在在线教程中并不流行,但在 CS 学位中必须知道它的存在。

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

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