简体   繁体   中英

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,

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. Also, if you are looking for the proof of actual running time begin the reading at page 573 (heavy reading and worth it).

Maybe it is not popular in online tutorials but is a must know its existence in a CS degree.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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