TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
dsu Class Reference

Disjoint sets union data structure, class based representation. More...

Public Member Functions

 dsu (uint64_t n)
 contructor for initialising all data members.
 
uint64_t findSet (uint64_t i)
 Method to find the representative of the set to which i belongs to, T(n) = O(1)
 
void UnionSet (uint64_t i, uint64_t j)
 Method that combines two disjoint sets to which i and j belongs to and make a single set having a common representative.
 
bool isSame (uint64_t i, uint64_t j)
 A utility function which check whether i and j belongs to same set or not.
 
vector< uint64_t > get (uint64_t i)
 prints the minimum, maximum and size of the set to which i belongs to
 
uint64_t size (uint64_t i)
 A utility function that returns the size of the set to which i belongs to.
 
uint64_t get_max (uint64_t i)
 A utility function that returns the max element of the set to which i belongs to.
 
uint64_t get_min (uint64_t i)
 A utility function that returns the min element of the set to which i belongs to.
 
 dsu (uint64_t n)
 constructor for initialising all data members
 
uint64_t findSet (uint64_t i)
 Method to find the representative of the set to which i belongs to, T(n) = O(logN)
 
void unionSet (uint64_t i, uint64_t j)
 Method that combines two disjoint sets to which i and j belongs to and make a single set having a common representative.
 
bool isSame (uint64_t i, uint64_t j)
 A utility function which check whether i and j belongs to same set or not.
 
vector< uint64_t > getParents (uint64_t i)
 Method to print all the parents of i, or the path from i to representative.
 

Private Attributes

vector< uint64_t > p
 keeps track of the parent of ith element
 
vector< uint64_t > depth
 tracks the depth(rank) of i in the tree
 
vector< uint64_t > setSize
 size of each chunk(set)
 
vector< uint64_t > maxElement
 maximum of each set to which i belongs to
 
vector< uint64_t > minElement
 minimum of each set to which i belongs to
 

Detailed Description

Disjoint sets union data structure, class based representation.

Parameters
nnumber of elements

Definition at line 34 of file dsu_path_compression.cpp.

Constructor & Destructor Documentation

◆ dsu() [1/2]

dsu::dsu ( uint64_t n)
inlineexplicit

contructor for initialising all data members.

Parameters
nnumber of elements

initially, all of them are their own parents

initially all have depth are equals to zero

initially set size will be equals to one

Definition at line 46 of file dsu_path_compression.cpp.

46 {
47 p.assign(n, 0);
49 for (uint64_t i = 0; i < n; i++) {
50 p[i] = i;
51 }
53 depth.assign(n, 0);
54 maxElement.assign(n, 0);
55 minElement.assign(n, 0);
56 for (uint64_t i = 0; i < n; i++) {
57 depth[i] = 0;
58 maxElement[i] = i;
59 minElement[i] = i;
60 }
61 setSize.assign(n, 0);
63 for (uint64_t i = 0; i < n; i++) {
64 setSize[i] = 1;
65 }
66 }
vector< uint64_t > minElement
minimum of each set to which i belongs to
vector< uint64_t > p
keeps track of the parent of ith element
vector< uint64_t > maxElement
maximum of each set to which i belongs to
vector< uint64_t > depth
tracks the depth(rank) of i in the tree
vector< uint64_t > setSize
size of each chunk(set)

◆ dsu() [2/2]

dsu::dsu ( uint64_t n)
inlineexplicit

constructor for initialising all data members

Parameters
nnumber of elements

initially all of them are their own parents

Definition at line 45 of file dsu_union_rank.cpp.

45 {
46 p.assign(n, 0);
48 depth.assign(n, 0);
49 setSize.assign(n, 0);
50 for (uint64_t i = 0; i < n; i++) {
51 p[i] = i;
52 depth[i] = 0;
53 setSize[i] = 1;
54 }
55 }

Member Function Documentation

◆ findSet() [1/2]

uint64_t dsu::findSet ( uint64_t i)
inline

Method to find the representative of the set to which i belongs to, T(n) = O(1)

Parameters
ielement of some set
Returns
representative of the set to which i belongs to.

using path compression

Definition at line 74 of file dsu_path_compression.cpp.

74 {
76 if (p[i] == i) {
77 return i;
78 }
79 return (p[i] = findSet(p[i]));
80 }
uint64_t findSet(uint64_t i)
Method to find the representative of the set to which i belongs to, T(n) = O(1)

◆ findSet() [2/2]

uint64_t dsu::findSet ( uint64_t i)
inline

Method to find the representative of the set to which i belongs to, T(n) = O(logN)

Parameters
ielement of some set
Returns
representative of the set to which i belongs to

using union-rank

Definition at line 62 of file dsu_union_rank.cpp.

62 {
64 while (i != p[i]) {
65 i = p[i];
66 }
67 return i;
68 }

◆ get()

vector< uint64_t > dsu::get ( uint64_t i)
inline

prints the minimum, maximum and size of the set to which i belongs to

Parameters
ielement of some set
Returns
void

Definition at line 136 of file dsu_path_compression.cpp.

136 {
137 vector<uint64_t> ans;
138 ans.push_back(get_min(i));
139 ans.push_back(get_max(i));
140 ans.push_back(size(i));
141 return ans;
142 }
uint64_t size(uint64_t i)
A utility function that returns the size of the set to which i belongs to.
uint64_t get_max(uint64_t i)
A utility function that returns the max element of the set to which i belongs to.
uint64_t get_min(uint64_t i)
A utility function that returns the min element of the set to which i belongs to.

◆ get_max()

uint64_t dsu::get_max ( uint64_t i)
inline

A utility function that returns the max element of the set to which i belongs to.

Parameters
ielement of some set
Returns
maximum of the set to which i belongs to

Definition at line 156 of file dsu_path_compression.cpp.

156{ return maxElement[findSet(i)]; }

◆ get_min()

uint64_t dsu::get_min ( uint64_t i)
inline

A utility function that returns the min element of the set to which i belongs to.

Parameters
ielement of some set
Returns
minimum of the set to which i belongs to

Definition at line 163 of file dsu_path_compression.cpp.

163{ return minElement[findSet(i)]; }

◆ getParents()

vector< uint64_t > dsu::getParents ( uint64_t i)
inline

Method to print all the parents of i, or the path from i to representative.

Parameters
ielement of some set
Returns
void

Definition at line 120 of file dsu_union_rank.cpp.

120 {
121 vector<uint64_t> ans;
122 while (p[i] != i) {
123 ans.push_back(i);
124 i = p[i];
125 }
126 ans.push_back(i);
127 return ans;
128 }

◆ isSame() [1/2]

bool dsu::isSame ( uint64_t i,
uint64_t j )
inline

A utility function which check whether i and j belongs to same set or not.

Parameters
ielement of some set
jelement of some set
Returns
true if element i and j ARE in the same set
false if element i and j are NOT in same set

Definition at line 124 of file dsu_path_compression.cpp.

124 {
125 if (findSet(i) == findSet(j)) {
126 return true;
127 }
128 return false;
129 }

◆ isSame() [2/2]

bool dsu::isSame ( uint64_t i,
uint64_t j )
inline

A utility function which check whether i and j belongs to same set or not.

Parameters
ielement of some set
jelement of some set
Returns
true if element i and j are in same set
false if element i and j are not in same set

Definition at line 108 of file dsu_union_rank.cpp.

108 {
109 if (findSet(i) == findSet(j)) {
110 return true;
111 }
112 return false;
113 }

◆ size()

uint64_t dsu::size ( uint64_t i)
inline

A utility function that returns the size of the set to which i belongs to.

Parameters
ielement of some set
Returns
size of the set to which i belongs to

Definition at line 149 of file dsu_path_compression.cpp.

149{ return setSize[findSet(i)]; }

◆ UnionSet()

void dsu::UnionSet ( uint64_t i,
uint64_t j )
inline

Method that combines two disjoint sets to which i and j belongs to and make a single set having a common representative.

Parameters
ielement of some set
jelement of some set
Returns
void

check if both belongs to the same set or not

always keeping the min as x shallow tree

making the shallower root's parent the deeper root

if same depth, then increase one's depth

total size of the resultant set

changing the maximum elements

Definition at line 88 of file dsu_path_compression.cpp.

88 {
90 if (isSame(i, j)) {
91 return;
92 }
93
94 // we find the representative of the i and j
95 uint64_t x = findSet(i);
96 uint64_t y = findSet(j);
97
100 if (depth[x] > depth[y]) {
101 std::swap(x, y);
102 }
104 p[x] = y;
105
107 if (depth[x] == depth[y]) {
108 depth[y]++;
109 }
111 setSize[y] += setSize[x];
113 maxElement[y] = std::max(maxElement[x], maxElement[y]);
114 minElement[y] = std::min(minElement[x], minElement[y]);
115 }
bool isSame(uint64_t i, uint64_t j)
A utility function which check whether i and j belongs to same set or not.

◆ unionSet()

void dsu::unionSet ( uint64_t i,
uint64_t j )
inline

Method that combines two disjoint sets to which i and j belongs to and make a single set having a common representative.

Parameters
ielement of some set
jelement of some set
Returns
void

checks if both belongs to same set or not

we find representative of the i and j

always keeping the min as x in order to create a shallow tree

making the shallower tree, root parent of the deeper root

if same depth, then increase one's depth

total size of the resultant set

Definition at line 76 of file dsu_union_rank.cpp.

76 {
78 if (isSame(i, j)) {
79 return;
80 }
82 uint64_t x = findSet(i);
83 uint64_t y = findSet(j);
84
87 if (depth[x] > depth[y]) {
88 std::swap(x, y);
89 }
91 p[x] = y;
92
94 if (depth[x] == depth[y]) {
95 depth[y]++;
96 }
98 setSize[y] += setSize[x];
99 }

Member Data Documentation

◆ depth

vector< uint64_t > dsu::depth
private

tracks the depth(rank) of i in the tree

Definition at line 37 of file dsu_path_compression.cpp.

◆ maxElement

vector<uint64_t> dsu::maxElement
private

maximum of each set to which i belongs to

Definition at line 39 of file dsu_path_compression.cpp.

◆ minElement

vector<uint64_t> dsu::minElement
private

minimum of each set to which i belongs to

Definition at line 40 of file dsu_path_compression.cpp.

◆ p

vector< uint64_t > dsu::p
private

keeps track of the parent of ith element

Definition at line 36 of file dsu_path_compression.cpp.

◆ setSize

vector< uint64_t > dsu::setSize
private

size of each chunk(set)

Definition at line 38 of file dsu_path_compression.cpp.


The documentation for this class was generated from the following files: