TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
dsu_path_compression.cpp
Go to the documentation of this file.
1
21#include <cassert>
22#include <cstdint>
23#include <iostream>
24#include <vector>
25
26using std::cout;
27using std::endl;
28using std::vector;
29
34class 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 }
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 }
124 bool isSame(uint64_t i, uint64_t j) {
125 if (findSet(i) == findSet(j)) {
126 return true;
127 }
128 return false;
129 }
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 }
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
170static 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}
187static 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
206int 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}
Disjoint sets union data structure, class based representation.
vector< uint64_t > get(uint64_t i)
prints the minimum, maximum and size of the set to which i belongs to
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)
uint64_t size(uint64_t i)
A utility function that returns the size of the set to which i belongs to.
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
bool isSame(uint64_t i, uint64_t j)
A utility function which check whether i and j belongs to same set or not.
uint64_t get_max(uint64_t i)
A utility function that returns the max element of the set to which i belongs to.
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...
vector< uint64_t > setSize
size of each chunk(set)
uint64_t get_min(uint64_t i)
A utility function that returns the min element of the set to which i belongs to.
static void test2()
Self-implementations, 2nd test.
int main()
Main function.
static void test1()
Self-test implementations, 1st test.
#define endl