Disjoint-set data structure

An interesting problem, given some numbers, group them into individual sets based on certain rules. For example, for numbers between, we group them into sets (0, 0), [1, 5), [10, 30), [30, 60), [60, 100]. I found this article on topcode is very helpful. Below is an implementation in C++.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <algorithm>
#include <climits>
#include <stack>
#include <queue>
#include <map>
#include <iterator>
#include <assert.h>

using namespace std;

struct Node {
int rank;
int value;
Node * parent;
};

class DisjointSets{
private:
int _count;
int _numElements;
int _numSets;
unordered_map<int, Node *> _nodes;
public:
DisjointSets(): _numElements(0), _numSets(0){}

~DisjointSets(){
for(auto it = _nodes.begin(); it != _nodes.end(); ++it){
delete it->second;
}
_nodes.clear();
}

int find_set(int element) {
if(!_nodes.count(element)){
return -1;
}
Node * curr = _nodes[element];

while(curr->parent){
curr = curr->parent;
}
Node * root = curr;

// update the parents along the way
curr = _nodes[element];
while(curr != root){
Node * next = curr->parent;
curr->parent = root;
curr = next;
}

return root->value;
}

void make_union(int setId1, int setId2){
if(setId1 == setId2){
return;
}

if(!_nodes.count(setId1)){
return;
}
Node* set1 = _nodes[setId1];
if(!_nodes.count(setId2)){
return;
}
Node* set2 = _nodes[setId2];

if(set1->rank > set2->rank){
set2->parent = set1;
} else if(set1->rank < set2->rank){
set1->parent = set2;
} else {
set2->parent = set1;
set1->rank++;
}
--_numSets;
}

void add_element(int element) {
_nodes[element] = new Node();
_nodes[element]->parent = NULL;
_nodes[element]->value = element;
_nodes[element]->rank = 0;

_numElements++;;
_numSets++;
}

int get_num_elements() const {
return _numElements;
}

int get_num_sets() const {
return _numSets;
}
};

void printElementSets(DisjointSets & s, const vector<int>& nums)
{
assert(nums.size() <= s.get_num_elements());
for (int i = 0; i < nums.size(); ++i) {
cout << s.find_set(nums[i]) << " ";
}
cout << endl;
}

void groupTogether(DisjointSets& s, const vector<int>& nums, map<int, vector<int>>& groups){
assert(nums.size() <= s.get_num_elements());
for (int i = 0; i < nums.size() && i < s.get_num_elements(); ++i) {
groups[s.find_set(nums[i])].push_back(nums[i]);
}
}

void prettyPrintGroups(const map<int, vector<int>>& groups){
for(auto it = groups.begin(); it != groups.end(); ++it){
std::cout << "Group " << it->first << " -------------"<< std::endl;
for(int i = 0; i < it->second.size(); ++i){
std::cout << it->second[i] << " ";
}
std::cout << std::endl;
}
}

void testGenerator(int size){
vector<pair<int, int>> rules{{0, 0}, {1, 5}, {10, 30}, {30, 60}, {60, 100}};
vector<int> randoms;
for(int i = 0; i < size; ++i){
randoms.push_back(random() % 100);
}

DisjointSets s;

for_each(randoms.begin(), randoms.end(), [&](int num){s.add_element(num); });

std::cout << "Orginal data:" << std::endl;
printElementSets(s, randoms);

for(int i = 0; i < rules.size(); ++i){
int rep = rules[i].first;
// push representative to DisjointSets
s.add_element(rep);
for(int j = rules[i].first + 1 ; j < rules[i].second; ++j){
s.make_union(s.find_set(rep), s.find_set(j));
}
}
std::cout << "After union find, group representative:" << std::endl;
printElementSets(s, randoms);
std::cout << std::endl;

// group together
map<int, vector<int>> groups;
groupTogether(s, randoms, groups);
prettyPrintGroups(groups);
}

int main(int argc, char *argv[])
{
const int DATA_SIZE = 10;
testGenerator(DATA_SIZE);
return 0;
}

Sample Output:
Orginal data:
83 86 77 15 93 35 86 92 49 21
After union find, group representative:
60 60 60 10 60 30 60 60 30 10

Group 10 ————-
15 21
Group 30 ————-
35 49
Group 60 ————-
83 86 77 93 86 92