Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
dsu Class Reference

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

Collaboration diagram for dsu:
[legend]

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

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

45 {
46 p.assign(n, 0);
47 /// initially, all of them are their own parents
48 for (uint64_t i = 0; i < n; i++) {
49 p[i] = i;
50 }
51 /// initially all have depth are equals to zero
52 depth.assign(n, 0);
53 maxElement.assign(n, 0);
54 minElement.assign(n, 0);
55 for (uint64_t i = 0; i < n; i++) {
56 depth[i] = 0;
57 maxElement[i] = i;
58 minElement[i] = i;
59 }
60 setSize.assign(n, 0);
61 /// initially set size will be equals to one
62 for (uint64_t i = 0; i < n; i++) {
63 setSize[i] = 1;
64 }
65 }
T assign(T... args)
vector< uint64_t > minElement
minimum of each set to which i belongs to
Definition dsu_path_compression.cpp:39
vector< uint64_t > p
keeps track of the parent of ith element
Definition dsu_path_compression.cpp:35
vector< uint64_t > maxElement
maximum of each set to which i belongs to
Definition dsu_path_compression.cpp:38
vector< uint64_t > depth
tracks the depth(rank) of i in the tree
Definition dsu_path_compression.cpp:36
vector< uint64_t > setSize
size of each chunk(set)
Definition dsu_path_compression.cpp:37
Here is the call graph for this function:

◆ 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

44 {
45 p.assign(n, 0);
46 /// initially all of them are their own parents
47 depth.assign(n, 0);
48 setSize.assign(n, 0);
49 for (uint64_t i = 0; i < n; i++) {
50 p[i] = i;
51 depth[i] = 0;
52 setSize[i] = 1;
53 }
54 }
Here is the call graph for this function:

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

73 {
74 /// using path compression
75 if (p[i] == i) {
76 return i;
77 }
78 return (p[i] = findSet(p[i]));
79 }
uint64_t findSet(uint64_t i)
Method to find the representative of the set to which i belongs to, T(n) = O(1)
Definition dsu_path_compression.cpp:73
Here is the call graph for this function:

◆ 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

61 {
62 /// using union-rank
63 while (i != p[i]) {
64 i = p[i];
65 }
66 return i;
67 }

◆ 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
135 {
137 ans.push_back(get_min(i));
138 ans.push_back(get_max(i));
139 ans.push_back(size(i));
140 return ans;
141 }
uint64_t size(uint64_t i)
A utility function that returns the size of the set to which i belongs to.
Definition dsu_path_compression.cpp:148
uint64_t get_max(uint64_t i)
A utility function that returns the max element of the set to which i belongs to.
Definition dsu_path_compression.cpp:155
uint64_t get_min(uint64_t i)
A utility function that returns the min element of the set to which i belongs to.
Definition dsu_path_compression.cpp:162
Here is the call graph for this function:

◆ 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
155{ return maxElement[findSet(i)]; }
Here is the call graph for this function:

◆ 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
162{ return minElement[findSet(i)]; }
Here is the call graph for this function:

◆ 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
119 {
121 while (p[i] != i) {
122 ans.push_back(i);
123 i = p[i];
124 }
125 ans.push_back(i);
126 return ans;
127 }

◆ 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
123 {
124 if (findSet(i) == findSet(j)) {
125 return true;
126 }
127 return false;
128 }
Here is the call graph for this function:

◆ 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
107 {
108 if (findSet(i) == findSet(j)) {
109 return true;
110 }
111 return false;
112 }
Here is the call graph for this function:

◆ 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
148{ return setSize[findSet(i)]; }
Here is the call graph for this function:

◆ 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

87 {
88 /// check if both belongs to the same set or not
89 if (isSame(i, j)) {
90 return;
91 }
92
93 // we find the representative of the i and j
94 uint64_t x = findSet(i);
95 uint64_t y = findSet(j);
96
97 /// always keeping the min as x
98 /// shallow tree
99 if (depth[x] > depth[y]) {
100 std::swap(x, y);
101 }
102 /// making the shallower root's parent the deeper root
103 p[x] = y;
104
105 /// if same depth, then increase one's depth
106 if (depth[x] == depth[y]) {
107 depth[y]++;
108 }
109 /// total size of the resultant set
110 setSize[y] += setSize[x];
111 /// changing the maximum elements
114 }
bool isSame(uint64_t i, uint64_t j)
A utility function which check whether i and j belongs to same set or not.
Definition dsu_path_compression.cpp:123
T max(T... args)
T min(T... args)
T swap(T... args)
Here is the call graph for this function:

◆ 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

75 {
76 /// checks if both belongs to same set or not
77 if (isSame(i, j)) {
78 return;
79 }
80 /// we find representative of the i and j
81 uint64_t x = findSet(i);
82 uint64_t y = findSet(j);
83
84 /// always keeping the min as x
85 /// in order to create a shallow tree
86 if (depth[x] > depth[y]) {
87 std::swap(x, y);
88 }
89 /// making the shallower tree, root parent of the deeper root
90 p[x] = y;
91
92 /// if same depth, then increase one's depth
93 if (depth[x] == depth[y]) {
94 depth[y]++;
95 }
96 /// total size of the resultant set
97 setSize[y] += setSize[x];
98 }
Here is the call graph for this function:

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