Disjoint Set Unionπ©π©π©
Tue Feb 01 2022 13:37:16 GMT+0000 (Coordinated Universal Time)
Saved by @vaibhav_55
/*This is the program to ilustrate the Disjoint set union(DSU) concept Applications:- 1)cycle detection in graph 2)no of connected components with their size 3)MST->kruskal's Algorithm 4)to find which element belongs to which set */ //globel declearation of parent and size array int size[100001]; int parent[100001]; //this function is call at the very beginning in the main funciton void assignParents() { for (int i = 0; i <= 100001; i++) { parent[i] = i; size[i] = 1; } } //--->Function to find super parent of a node<--- /* -->without any optimization<-- // int find_super_parent(int a) // { // if (parent[a] == a) // { // return a; // } // else // { // return find_super_parent(parent[a]); // } // } */ //-->with path optimization<-- int find_super_parent(int a) { if (parent[a] == a) { return a; } else { int super_parent = find_super_parent(parent[a]); parent[a] = super_parent; return super_parent; } } /* ll super_parent(ll n) { if (parent[n] == n) return n; else return super_parent(parent[n]); } void make_union(ll a, ll b) { ll p_a = super_parent(a); ll p_b = super_parent(b); if (p_a == p_b) cout << a << " and " << b << " already belongs to same set.\n"; else { if (size[p_a] < size[p_b]) { parent[p_a] = p_b; size[p_b] += size[p_a]; } else { parent[p_b] = p_a; size[p_a] += size[p_b]; } } */ /*---------------------------------------------------------------------------*/ //--->Function to make union of two elements/sets<--- //-->without any optimization // void make_union(int a, int b) // { // int pa = find_super_parent(a); // int pb = find_super_parent(b); // if (pa != pb) // { // cout << a << " " << b << endl; // parent[pa] = pb; // cout << "The set consisting entered elements has been merged" << endl; // } // else // { // cout << "Entered elements already belongs to same set" << endl; // } // } //-->with size optimization void make_union(int a, int b) { int pa = find_super_parent(a); int pb = find_super_parent(b); if (pa == pb) { cout << "Entered elements already belongs to same set" << endl; } else { if (size[pa] < size[pb]) { parent[pa] = pb; size[pb] += size[pa]; } else { parent[pb] = pa; size[pa] += size[pb]; } } }
Comments