-
Notifications
You must be signed in to change notification settings - Fork 0
/
DisjointSets.cpp
47 lines (41 loc) · 1.01 KB
/
DisjointSets.cpp
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
/**
Lab 5 - Kruskal's Algorithm
CSC 255 Objects and Algorithms (Fall 2020)
Oakton Community College
Professor: Kamilla Murashkina
@file DisjointSets.cpp
@author Russell Taylor
@date 10/24/20
*/
#include "DisjointSets.hpp"
/**
* Constructor
* @param elements the initial number of disjoint sets
*/
DisjointSets::DisjointSets(int elements) : sets(elements, -1) {
}
/**
* Finds which set contains an element, with path compression
* @param x the element to find
* @return the name of the set which contains the element
*/
int DisjointSets::find(int x) {
if (sets[x] < 0)
return x;
else
return sets[x] = find(sets[x]);
}
/**
* Merges the two disjoint sets into a new set
* @param rootA the root of the first set
* @param rootB the root of the second set
*/
void DisjointSets::unionSets(int rootA, int rootB) {
if (sets[rootB] < sets[rootA])
sets[rootA] = rootB;
else {
if (sets[rootA] == sets[rootB])
--sets[rootA];
sets[rootB] = rootA;
}
}