Disjoint sets union data structure, class based representation.
More...
|
| | 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.
|
| |
|
|
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
|
| |
Disjoint sets union data structure, class based representation.
- Parameters
-
◆ dsu() [1/2]
contructor for initialising all data members.
- Parameters
-
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 {
48
49 for (uint64_t i = 0; i < n; i++) {
51 }
52
56 for (uint64_t i = 0; i < n; i++) {
60 }
62
63 for (uint64_t i = 0; i < n; i++) {
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]
constructor for initialising all data members
- Parameters
-
initially all of them are their own parents
45 {
47
50 for (uint64_t i = 0; i < n; i++) {
54 }
55 }
◆ 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
-
- Returns
- representative of the set to which i belongs to.
using path compression
74 {
75
77 return i;
78 }
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
◆ 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
-
- Returns
- representative of the set to which i belongs to
using union-rank
62 {
63
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
-
- Returns
- void
136 {
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
◆ 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
-
- Returns
- maximum of the set to which i belongs to
◆ 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
-
- Returns
- minimum of the set to which i belongs to
◆ 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
-
- Returns
- void
120 {
123 ans.push_back(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
-
| i | element of some set |
| j | element 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 {
126 return true;
127 }
128 return false;
129 }
◆ 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
-
| i | element of some set |
| j | element 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 {
110 return true;
111 }
112 return false;
113 }
◆ 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
-
- Returns
- size of the set to which i belongs to
◆ 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
-
| i | element of some set |
| j | element 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
91 return;
92 }
93
94
97
98
99
102 }
103
105
106
109 }
110
112
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
◆ 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
-
| i | element of some set |
| j | element 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
79 return;
80 }
81
84
85
86
89 }
90
92
93
96 }
97
99 }
The documentation for this class was generated from the following files: