Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
dsu_union_rank.cpp File Reference

DSU (Disjoint sets) More...

#include <cassert>
#include <cstdint>
#include <iostream>
#include <vector>
Include dependency graph for dsu_union_rank.cpp:

Classes

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

Functions

static void test1 ()
 Self-implementations, 1st test.
 
static void test2 ()
 Self-implementations, 2nd test.
 
int main ()
 Main function.
 

Detailed Description

DSU (Disjoint sets)

dsu : It is a very powerful data structure which keeps track of different clusters(sets) of elements, these sets are disjoint(doesnot have a common element). Disjoint sets uses cases : for finding connected components in a graph, used in Kruskal's algorithm for finding Minimum Spanning tree. Operations that can be performed: 1) UnionSet(i,j): add(element i and j to the set) 2) findSet(i): returns the representative of the set to which i belogngs to. 3) getParents(i): prints the parent of i and so on and so forth. Below is the class-based approach which uses the heuristic of union-ranks. Using union-rank in findSet(i),we are able to get to the representative of i in slightly delayed O(logN) time but it allows us to keep tracks of the parent of i.

Author
AayushVyasKIIT
See also
dsu_path_compression.cpp

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit
183 {
184 test1(); // run 1st test case
185 test2(); // run 2nd test case
186
187 return 0;
188}
static void test2()
Self-implementations, 2nd test.
Definition dsu_union_rank.cpp:158
static void test1()
Self-implementations, 1st test.
Definition dsu_union_rank.cpp:134
Here is the call graph for this function:

◆ test1()

static void test1 ( )
static

Self-implementations, 1st test.

Returns
void

< number of elements

< object of class disjoint sets

< performs union operation on 1 and 2

134 {
135 /* checks the parents in the resultant structures */
136 uint64_t n = 10; ///< number of elements
137 dsu d(n + 1); ///< object of class disjoint sets
138 d.unionSet(2, 1); ///< performs union operation on 1 and 2
139 d.unionSet(1, 4);
140 d.unionSet(8, 1);
141 d.unionSet(3, 5);
142 d.unionSet(5, 6);
143 d.unionSet(5, 7);
144 d.unionSet(9, 10);
145 d.unionSet(2, 10);
146 // keeping track of the changes using parent pointers
147 vector<uint64_t> ans = {7, 5};
148 for (uint64_t i = 0; i < ans.size(); i++) {
149 assert(d.getParents(7).at(i) ==
150 ans[i]); // makes sure algorithm works fine
151 }
152 cout << "1st test passed!" << endl;
153}
Disjoint sets union data structure, class based representation.
Definition dsu_path_compression.cpp:34
#define endl
Definition matrix_exponentiation.cpp:36
Here is the call graph for this function:

◆ test2()

static void test2 ( )
static

Self-implementations, 2nd test.

Returns
void

< number of elements

< object of class disjoint sets

performs union operation on 1 and 2

keeping track of the changes using parent pointers

makes sure algorithm works fine

158 {
159 // checks the parents in the resultant structures
160 uint64_t n = 10; ///< number of elements
161 dsu d(n + 1); ///< object of class disjoint sets
162 d.unionSet(2, 1); /// performs union operation on 1 and 2
163 d.unionSet(1, 4);
164 d.unionSet(8, 1);
165 d.unionSet(3, 5);
166 d.unionSet(5, 6);
167 d.unionSet(5, 7);
168 d.unionSet(9, 10);
169 d.unionSet(2, 10);
170
171 /// keeping track of the changes using parent pointers
172 vector<uint64_t> ans = {2, 1, 10};
173 for (uint64_t i = 0; i < ans.size(); i++) {
174 assert(d.getParents(2).at(i) ==
175 ans[i]); /// makes sure algorithm works fine
176 }
177 cout << "2nd test passed!" << endl;
178}
Here is the call graph for this function: