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 <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
182 {
183 test1(); // run 1st test case
184 test2(); // run 2nd test case
185
186 return 0;
187}
static void test2()
Self-implementations, 2nd test.
Definition dsu_union_rank.cpp:157
static void test1()
Self-implementations, 1st test.
Definition dsu_union_rank.cpp:133
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

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

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