From fc4b20a15c6cedf44c8889bc969227c2f4166ecc Mon Sep 17 00:00:00 2001 From: Aayush Vyas <70998175+AayushVyasKIIT@users.noreply.github.com> Date: Sat, 28 Aug 2021 14:33:11 +0530 Subject: [PATCH] Delete DSU_union_rank.cpp --- data_structures/DSU_union_rank.cpp | 96 ------------------------------ 1 file changed, 96 deletions(-) delete mode 100644 data_structures/DSU_union_rank.cpp diff --git a/data_structures/DSU_union_rank.cpp b/data_structures/DSU_union_rank.cpp deleted file mode 100644 index a3b023c50..000000000 --- a/data_structures/DSU_union_rank.cpp +++ /dev/null @@ -1,96 +0,0 @@ -#include -#include - - -using namespace std; - -class DSU{ - private: - // p: keeps track of parent of i - // depth: tracks the depth of i - // setSize: size of each chunk(set) - vector p,depth,setSize; - public: - //parameter : int n -> maximum number of items - DSU(int n){ - p.assign(n,0); - //initially all of them their own parents - depth.assign(n,0); - setSize.assign(n,0); - for(int i=0;i p[i] - while(i!=p[i]){ - i = p[i]; - } - return i; - } - //union of 2 sets - void unionSet(int i,int j){ - //check if both belongs to same set or not - if(isSame(i,j)){ - return; - } - //we find representative of the i and j - int x = findSet(i); - int y = findSet(j); - - //always keeping the min as x - //in order to create a shallow tree - if(depth[x]>depth[y]){ - swap(x,y); - } - //making the shallower tree' root parent of the deeper root - p[x] = y; - - //if same depth then increase one's depth - if(depth[x] == depth[y]){ - depth[y]++; - } - //total size of the resultant set - setSize[y]+=setSize[x]; - } - // checks if both belongs to same set - bool isSame(int i,int j){ - if(findSet(i) == findSet(j)){ - return true; - } - return false; - } - void getParents(int i){ - while(p[i]!=i){ - cout << i << " ->"; - i = p[i]; - } - cout << p[i] << endl; - } - -}; - -int main(){ - int n; - n = 10; - //n: number of items - DSU d(n+1); - d.unionSet(2,1); //performs union operation on 1 and 2 - d.unionSet(1,4); - d.unionSet(8,1); - - d.unionSet(3,5); - d.unionSet(5,6); - d.unionSet(5,7); - - d.unionSet(9,10); - d.unionSet(2,10); - - //keeping track of the changes using parent pointers - d.getParents(7); - d.getParents(2); - - -} \ No newline at end of file