简体   繁体   中英

Weighted Quick Union Find

I am taking an algorithms course where they go over weighted quick union find. I am confused about why we are concerned about the size of a tree as opposed to the depth?

When I tried writing out the code, my code looked different than the solution provided.

From my understanding, the size of the tree (total number of nodes in a tree) is not as important as the depth of the tree when it comes to the run time of the union function (lg n) because it is the depth that will determine how many look ups are needed to get to the root of a node?

Thanks

My code:
public void union(int p, int q) {
    int root_p = root(p);
    int root_q = root(q);

    // If the two trees are not already connected, union them
    if(root_p != root_q) {

        // The two trees aren't connected, check which is deeper
        // Attach the one that is more shallow to the deeper one
        if (depth[root_p] > depth[root_q]) {
            // p is deeper, point q's root to p
            id[root_q] = root_p;
        } else if (depth[root_q] > depth[root_p]) {
            // q is deeper, point p's root to p
            id[root_p] = root_q;
        } else {
            // They are of equal depth, point q's root to p and increment p's depth by 1
            id[root_q] = root_p;
            depth[root_p] += 1;
        }
    }
}


Solution code provided:

public void union(int p, int q) {
    int rootP = find(p);
    int rootQ = find(q);
    if (rootP == rootQ) return;

    // make smaller root point to larger one
    if (size[rootP] < size[rootQ]) {
        parent[rootP] = rootQ;
        size[rootQ] += size[rootP];
    }
    else {
        parent[rootQ] = rootP;
        size[rootP] += size[rootQ];
    }
    count--;
}

You are correct that the depth (actually height) is more directly related to the run time, but using either one will result in O(log N) run time for union and find.

The proof is easy -- Given that when we begin (when all sets are disjoint), every root with height h has at least 2 (h-1) nodes, this invariant is maintained by union and find operations. Therefore, if a node has size n , then its height will be at most floor(log 2 (n))+1

So either one will do. BUT, very soon you will learn about path compression, which makes it difficult to keep track of the height of roots, but the size will still be available. At that point you will be able to use rank , which is kind of like height, or continue to use the size. Again either one will do, but I find the size easier to reason about so I always use that.

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