TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
dsu_path_compression.cpp
Go to the documentation of this file.
1
20
21
#include <cassert>
22
#include <cstdint>
23
#include <iostream>
24
#include <vector>
25
26
using
std::cout;
27
using
std::endl;
28
using
std::vector;
29
34
class
dsu
{
35
private
:
36
vector<uint64_t>
p
;
37
vector<uint64_t>
depth
;
38
vector<uint64_t>
setSize
;
39
vector<uint64_t>
maxElement
;
40
vector<uint64_t>
minElement
;
41
public
:
46
explicit
dsu
(uint64_t n) {
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
}
67
74
uint64_t
findSet
(uint64_t i) {
76
if
(
p
[i] == i) {
77
return
i;
78
}
79
return
(
p
[i] =
findSet
(
p
[i]));
80
}
81
88
void
UnionSet
(uint64_t i, uint64_t j) {
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
}
116
124
bool
isSame
(uint64_t i, uint64_t j) {
125
if
(
findSet
(i) ==
findSet
(j)) {
126
return
true
;
127
}
128
return
false
;
129
}
130
136
vector<uint64_t>
get
(uint64_t i) {
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
}
143
149
uint64_t
size
(uint64_t i) {
return
setSize
[
findSet
(i)]; }
156
uint64_t
get_max
(uint64_t i) {
return
maxElement
[
findSet
(i)]; }
163
uint64_t
get_min
(uint64_t i) {
return
minElement
[
findSet
(i)]; }
164
};
165
170
static
void
test1
() {
171
// the minimum, maximum, and size of the set
172
uint64_t n = 10;
173
dsu
d(n + 1);
174
// set 1
175
d.
UnionSet
(1, 2);
// performs union operation on 1 and 2
176
d.
UnionSet
(1, 4);
// performs union operation on 1 and 4
177
vector<uint64_t> ans = {1, 4, 3};
178
for
(uint64_t i = 0; i < ans.size(); i++) {
179
assert(d.
get
(4).at(i) == ans[i]);
// makes sure algorithm works fine
180
}
181
cout <<
"1st test passed!"
<<
endl
;
182
}
183
187
static
void
test2
() {
188
// the minimum, maximum, and size of the set
189
uint64_t n = 10;
190
dsu
d(n + 1);
191
// set 1
192
d.
UnionSet
(3, 5);
193
d.
UnionSet
(5, 6);
194
d.
UnionSet
(5, 7);
195
vector<uint64_t> ans = {3, 7, 4};
196
for
(uint64_t i = 0; i < ans.size(); i++) {
197
assert(d.
get
(3).at(i) == ans[i]);
// makes sure algorithm works fine
198
}
199
cout <<
"2nd test passed!"
<<
endl
;
200
}
201
206
int
main
() {
207
uint64_t n = 10;
208
dsu
d(n + 1);
209
210
test1
();
// run 1st test case
211
test2
();
// run 2nd test case
212
213
return
0;
214
}
dsu
Disjoint sets union data structure, class based representation.
Definition
dsu_path_compression.cpp:34
dsu::get
vector< uint64_t > get(uint64_t i)
prints the minimum, maximum and size of the set to which i belongs to
Definition
dsu_path_compression.cpp:136
dsu::dsu
dsu(uint64_t n)
contructor for initialising all data members.
Definition
dsu_path_compression.cpp:46
dsu::findSet
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
dsu::size
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
dsu::minElement
vector< uint64_t > minElement
minimum of each set to which i belongs to
Definition
dsu_path_compression.cpp:40
dsu::p
vector< uint64_t > p
keeps track of the parent of ith element
Definition
dsu_path_compression.cpp:36
dsu::maxElement
vector< uint64_t > maxElement
maximum of each set to which i belongs to
Definition
dsu_path_compression.cpp:39
dsu::depth
vector< uint64_t > depth
tracks the depth(rank) of i in the tree
Definition
dsu_path_compression.cpp:37
dsu::isSame
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
dsu::get_max
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
dsu::UnionSet
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 com...
Definition
dsu_path_compression.cpp:88
dsu::setSize
vector< uint64_t > setSize
size of each chunk(set)
Definition
dsu_path_compression.cpp:38
dsu::get_min
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
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
test2
void test2(const std::string &text)
Self test 2 - using 8x8 randomly generated key.
Definition
hill_cipher.cpp:505
test1
void test1(const std::string &text)
Self test 1 - using 3x3 randomly generated key.
Definition
hill_cipher.cpp:470
endl
#define endl
Definition
matrix_exponentiation.cpp:36
data_structures
dsu_path_compression.cpp
Generated by
1.18.0