Merge pull request #1 from AayushVyasKIIT/AayushVyasKIIT-DSU-CLASS-BASED

Class based DSU : Path Compression O(1) & Union Rank O(logN)
This commit is contained in:
Aayush Vyas
2021-08-27 17:10:16 +05:30
committed by GitHub
2 changed files with 62 additions and 8 deletions

View File

@@ -4,13 +4,20 @@ using namespace std;
#define vi vector<int>
//Disjoint set union
class DSU{
private:
// p: keeps track of parent of i
// depth: tracks the depth of i
// setSize: size of each chunk(set)
// maxElement : max of each set, using maxElement[representative]
// minElement : min of each set, using minElement[representative]
vi p,depth,setSize,maxElement,minElement;
public:
// parameter : int n -> maximum number of items
DSU(int n){
p.assign(n,0);
//initially all of them are their parents.
//initially all of them are their own parents.
for(int i=0;i<n;i++){
p[i] = i;
}
@@ -35,11 +42,12 @@ class DSU{
if(p[i]==i){
return i;
}
//path compression
//path compression i -> root(representative)
return (p[i] = findSet(p[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;
}
@@ -56,22 +64,24 @@ class DSU{
//making the shallower root's parent 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];
//changing the maximum elements
maxElement[y] = max(maxElement[x],maxElement[y]);
minElement[y] = min(minElement[x],minElement[y]);
}
//checks if both sets equal
//checks if both belongs to same set
bool isSame(int i,int j){
if(findSet(i) == findSet(j)){
return true;
}
return false;
}
//returns min max size of i's set
void get(int i){
cout << get_min(i) << " " << get_max(i) << " " <<size(i) << endl;
}
@@ -79,19 +89,45 @@ class DSU{
int size(int i){
return setSize[findSet(i)];
}
//returns max of the set whose part is i
int get_max(int i){
return maxElement[findSet(i)];
}
//returns min of the set whose part is i
int get_min(int i){
return minElement[findSet(i)];
}
};
/*
test case#1:
5 11
union 1 2
get 3
get 2
union 2 3
get 2
union 1 3
get 5
union 4 5
get 5
union 4 1
get 5
*/
/*
output case#1:
3 3 1
1 2 2
1 3 3
5 5 1
4 5 2
1 5 5
*/
int main(){
std::ios_base::sync_with_stdio(0);std::cin.tie(0);std::cout.tie(0);
int n,q;cin>>n;cin>>q;
//n: number of items
//q: number of queries to be made
DSU d(n+1);
while(q--){
@@ -99,11 +135,11 @@ int main(){
if(op == "union"){
int i,j;
cin >> i >> j;
d.UnionSet(i,j);
d.UnionSet(i,j); //performs union operation on i and j
}else{
int i;
cin >> i;
d.get(i);
d.get(i); //print min max and size of set.
}
}

View File

@@ -5,10 +5,15 @@ using namespace std;
#define vi vector<int>
class DSU{
private:
// p: keeps track of parent of i
// depth: tracks the depth of i
// setSize: size of each chunk(set)
vi 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<n;i++){
@@ -18,27 +23,38 @@ class DSU{
}
}
int findSet(int i){
//union rank 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;
@@ -49,9 +65,11 @@ class DSU{
};
int main(){
//number of items
int n;cin>>n;
DSU d(n+1);
//perform operations here
d.unionSet(2,4);
d.unionSet(3,4);
cout << d.findSet(2) <<endl;