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

46 {
47 p.assign(n, 0);
48 /// initially, all of them are their own parents
49 for (uint64_t i = 0; i < n; i++) {
50 p[i] = i;
51 }
52 /// initially all have depth are equals to zero
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);
62 /// initially set size will be equals to one
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
Definition dsu_path_compression.cpp:40
vector< uint64_t > p
keeps track of the parent of ith element
Definition dsu_path_compression.cpp:36
vector< uint64_t > maxElement
maximum of each set to which i belongs to
Definition dsu_path_compression.cpp:39
vector< uint64_t > depth
tracks the depth(rank) of i in the tree
Definition dsu_path_compression.cpp:37
vector< uint64_t > setSize
size of each chunk(set)
Definition dsu_path_compression.cpp:38

◆ 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

45 {
46 p.assign(n, 0);
47 /// initially all of them are their own parents
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

74 {
75 /// using path compression
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)
Definition dsu_path_compression.cpp:74
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

62 {
63 /// using union-rank
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
136 {
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.
Definition dsu_path_compression.cpp:149
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:156
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:163
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
156{ 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
163{ 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
120 {
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
124 {
125 if (findSet(i) == findSet(j)) {
126 return true;
127 }
128 return false;
129 }
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
108 {
109 if (findSet(i) == findSet(j)) {
110 return true;
111 }
112 return false;
113 }
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
149{ 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

88 {
89 /// check if both belongs to the same set or not
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
98 /// always keeping the min as x
99 /// shallow tree
100 if (depth[x] > depth[y]) {
101 std::swap(x, y);
102 }
103 /// making the shallower root's parent the deeper root
104 p[x] = y;
105
106 /// if same depth, then increase one's depth
107 if (depth[x] == depth[y]) {
108 depth[y]++;
109 }
110 /// total size of the resultant set
111 setSize[y] += setSize[x];
112 /// changing the maximum elements
115 }
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:124
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

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

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