-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeightedQuickUnionUF.hpp
114 lines (105 loc) · 3.56 KB
/
WeightedQuickUnionUF.hpp
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
#ifndef ALGORITHMS_WEIGHTEDQUICKUNIONUF_HPP
#define ALGORITHMS_WEIGHTEDQUICKUNIONUF_HPP
#include <cstdint>
#include <string>
#include <vector>
#include <stdexcept>
using namespace std;
/**
* The {@code WeightedQuickUnionUF} class represents a union–find data type
* (also known as the disjoint-sets data type).
* It supports the classic union and find operations,
* along with a count operation that returns the total number
* of sets.
*
* The union–find data type models a collection of sets containing
* n elements, with each element in exactly one set.
* The elements are named 0 through n–1.
* Initially, there are n sets, with each element in its
* own set. The canonical element of a set
* (also known as the root, identifier,
* leader, or set representative)
* is one distinguished element in the set. Here is a summary of
* the operations:
*
* - find(p) returns the canonical element
* of the set containing p. The find operation
* returns the same value for two elements if and only if
* they are in the same set.
*
* - union(p, q) merges the set
* containing element p with the set containing
* element q. That is, if <em>p</em> and <em>q</em>
* are in different sets, replace these two sets
* with a new set that is the union of the two.
*
* - count</em>() returns the number of sets.
*
* The canonical element of a set can change only when the set
* itself changes during a call to union;it cannot
* change during a call to either find or count.
*
* This implementation uses weighted quick union by rank
* with path compression by halving.
* The constructor takes Θ(n) time, where
* n is the number of elements.
* The union and find operations take
* Θ(log(n)) time in the worst case.
* The count operation takes Θ(1) time.
*
* @author Benjamin Chan
*
* Adapted from Algorithms, 4th edition, {@authors Robert Sedgewick and Kevin Wayne}
* and their booksite https://algs4.cs.princeton.edu/
*
* The Java program from which this C++ code was adapted from is found at
* https://algs4.cs.princeton.edu/15uf/WeightedQuickUnionUF.java.html.
*/
class WeightedQuickUnionUF {
// parent[i] = parent of i
vector<int> parent;
// size[i] = number of elements in subtree rooted at i
vector<int8_t> rank;
// number of components
int thisCount;
public:
/**
* Initializes an empty union-find data structure with
* {@code n} elements {@code 0} through {@code n-1}.
* Initially, each elements is in its own set.
*
* @param n the number of elements
* @throws invalid_argument if {@code n < 0}
*/
explicit WeightedQuickUnionUF(int n);
/**
* Returns the canonical element of the set containing element {@code p}.
*
* @param p an element
* @return the canonical element of the set containing {@code p}
* @throws invalid_argument unless {@code 0 <= p < n}
*/
int find(int p);
/**
* Returns the number of sets.
*
* @return the number of sets (between {@code 1} and {@code n})
*/
inline int count() {
return thisCount;
}
/**
* Merges the set containing element {@code p} with the
* the set containing element {@code q}.
*
* @param p one element
* @param q the other element
* @throws invalid_argument unless
* both {@code 0 <= p < n} and {@code 0 <= q < n}
*/
void weightedUnion(int p, int q);
private:
/// validates that p is a valid index
void validate(int p);
};
#endif //ALGORITHMS_WEIGHTEDQUICKUNIONUF_HPP