Data structure-and check the set "below"

The previous section mainly introduced the idea and code implementation of Quick Find. This section will introduce the implementation and code implementation of Quick Union.

 

Quick Union - Union

  • Union of Quick Union (v1, v2): Let the root node of v1 point to the root node of v2

     

     

    image

    image

public void union(int v1, int v2) {
    int p1 = find(v1);
    int p2 = find(v2);
    if (p1 == p2) return;
    parents[p1] = p2;
}

 

  • Time complexity: O(logn) 

     

    image

public int find(int v) {
    rangeCheck(v);
    while (v != parents[v]) {
        v = parents[v];
    }
    return v;
}
find(0) == 2
find(1) == 2
find(2) == 2
find(3) == 3
find(4) == 2

 

Quick Union-optimization

  • In the process of Union, there may be tree imbalances, and even degenerate into a linked list

    image

    image

     

    image

  • There are 2 common optimization schemes

     

  1. Size-based optimization: a tree with few elements is grafted to a tree with many elements

  2. Rank-based optimization: grafting short trees to tall trees

     

Quick Union-1. Optimization based on size

image

sizes = new int[capacity];
for (int i = 0; i < sizes.length; i++) {
    size[i] = 1;
}

private int[] sizes;
public void union(int v1, int v2) {
    int p1 = find(v1);
    int p2 = find(v2);
    if (p1 == p2) return;
    if (sizes[p1] < sizes[p2]) {
        parents[p1] = p2;
        sizes[p2] += sizes[p1];
    } else {
       parents[p2] = p1;
       sizes[p1] += sizs[p2];        
    }
}


 

Optimization based on size may also have the problem of tree imbalance 

image

 

Quick Union-2. Optimization based on rank

 

 

image

ranks = new int[capacity];
for (int i = 0; i < ranks.length; i++) {
    ranks[i] = 1;
}

private int[] ranks;
public void union(int v1, int v2) {
    int p1 = find(v1);
    int p2 = find(v2);
    if (p1 == p2) return;
    if (ranks[p1] < ranks[p2]) {
        parents[p1] = p2;
    } else if (ranks[p2] < ranks[p1]) {
        parents[p2] = p1;
    } else {
       parents[p1] = p2;
       ranks[p2]++;
    }
}

 

Path Compression

 

  • Although with rank-based optimization, the tree will be relatively balanced

  • However, as the number of Unions increases, the height of the tree will still get higher and higher, resulting in slower find operations, especially the underlying nodes (because find is constantly finding the root node upwards)

  • What is path compression?

  • When find, make all nodes on the path point to the root node, thereby reducing the height of the tree

image

public int find(int v) {
rangeCheck(v);
if (parents[v] != v) {
    parents[v] = find(parents[v]);
}
return parents[v];
}

 

  • Path compression makes all nodes on the path point to the root node, so the implementation cost is slightly higher

  • There are two more effective methods, but the tree height cannot be reduced, and the implementation cost is lower than path compression.

  • Path Spliting

  • Path Halving

  • The efficiency of path splitting and path halving is about the same, but both are better than path compression

     

1. Path Spliting

  • Path split: make each node on the path point to its grandparent node (parent of parent) 

     

    image

public int find(int v) {
    rangeCheck(v);
    while (v != parents[v]) {
        int parent = parents[v];
        parents[v] = parents[parent];
        v = parent;
    }
    return v;
}

 

2. Path Halving

  • The path is halved: every other node on the path points to its grandparent node (parent of parent) 

     

    image

public int find(int v) {
    rangeCheck(v);
    while (v != parents[v]) {
        parents[v] = parents[parents[v]];
        v = parents[v];
    }
    return v;
}

 

to sum up

  Today, I mainly explained the principle and code implementation of Quick Union. Then explained the optimization based on size and rank. Finally, a brief introduction to the next path compression. I've found that this is the end, and I hope to add some new knowledge reserves to everyone's knowledge base.

Welcome to pay attention to [The Way of Infinite Testing] public account , reply [receive resources],
Python programming learning resources dry goods,
Python+Appium framework APP UI automation,
Python+Selenium framework Web UI automation,
Python+Unittest framework API automation,

Resources and codes are sent for free~
There is a QR code of the official account at the bottom of the article, you can just scan it on WeChat and follow it.

Remarks: My personal public account has been officially opened, dedicated to the sharing of test technology, including: big data testing, functional testing, test development, API interface automation, test operation and maintenance, UI automation testing, etc., WeChat search public account: "Infinite The Way of Testing", or scan the QR code below:

 Add attention and let us grow together!

Guess you like

Origin blog.csdn.net/weixin_41754309/article/details/113093670