Showing posts with label REF:COREMEN BOOK. Show all posts
Showing posts with label REF:COREMEN BOOK. Show all posts

Sunday, July 23, 2023

UNION MAKE SET FIND SET


 













void make_set(int v) {
    parent[v] = v;
}

int find_set(int v) {
    if (v == parent[v])
        return v;
    return find_set(parent[v]);
}

void union_sets(int a, int b) {
    a = find_set(a);
    b = find_set(b);
    if (a != b)
        parent[b] = a;
}











There are two ways to improve it: 
1. Path Compression
2. Union by Rank