Union Find

Photo by Anthony DELANOIX on Unsplash
Photo by Anthony DELANOIX on Unsplash
Union Find data structure is also known as disjoint set data structure. It is a data structure which stores a collection of disjoint sets. It provides two operations, one for merging two sets and one for finding the set with a given element.

Union Find data structure is also known as disjoint set data structure. It is a data structure which stores a collection of disjoint sets. It provides two operations, one for merging two sets and one for finding the set with a given element.

Union Find

There is an array called parents in the Union Find data structure. It records the set to which each element belongs. Initially, each element forms a set by itself. Afterwards, the given two sets are merged by calling union(). find() returns the set number of the passed element.

public class UnionFind {
    private final int[] parents;

    public UnionFind(int size) {
        parents = new int[size];
        for (int i = 0; i < size; i++) {
            parents[i] = i;
        }
    }

    int find(int x) {
        while (x != parents[x]) {
            parents[x] = parents[parents[x]];
            x = parents[x];
        }
        return x;
    }

    void union(int x, int y) {
        int parentX = find(x);
        int parentY = find(y);

        if (parentX < parentY) {
            parents[parentY] = parentX;
        } else if (parentX > parentY) {
            parents[parentX] = parentY;
        }
    }
}

Time and Space Complexity

When initializing parents, the required time complexity is O(n), where n is the number of elements.

The time complexity required by Find() is O(α(n)), where α is the inverse Ackermann function , it grows very slowly, so it can be regarded as constant time. So, O(α(n)) ≈ O(1). Therefore, the time complexity of Find() is O(1).

Union() uses Find(), so its time complexity is O(1).

The Union Find data structure requires a parents internally. So, its space complexity is O(n).

Example

Reference

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like
Photo by Pedro Lastra on Unsplash
Read More

Breadth First Search (BFS)

Breadth-first search (BFS) algorithm is a graph search algorithm. Starting from a given vertex, it first explores all adjacent vertices of that vertex. Next, explores the adjacent vertices of these adjacent vertices in sequence. In this way, it explores level by level until the result is found, or all vertices are explored.
Read More
Photo by Léonard Cotte on Unsplash
Read More

Depth First Search (DFS)

Depth-first search (DFS) algorithm is a graph search algorithm. Starting from a given vertex, it first explores an adjacent vertex of that vertex. Next, it explores an adjacent vertex of this vertex. In this way, the exploration continues until a certain vertex has no adjacent vertices, it will return to the previous vertex, and explores the next adjacent vertex of the vertex. It will continue to explore like this until it finds a result, or explores all vertices.
Read More