122 lines
4.4 KiB
Java
122 lines
4.4 KiB
Java
/**
|
|
* MIT License
|
|
*
|
|
* Copyright (c) 2021 Exercism
|
|
* Copyright (c) 2026 Networked Embedded Systems, University of Duisburg-Essen
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*/
|
|
|
|
/**
|
|
* To solve our exercise, we are going to use the Union-find data structure,
|
|
* also called the Disjoint-set data
|
|
* structure or Merge-find set. The Union-find data structure permit us to solve
|
|
* Dynamic Connectivity problems,
|
|
* by helping us model the connections between elements of a system. Within such
|
|
* systems, the "connection" relation
|
|
* is an equivalence relation. ie; An element is connected to itself, x
|
|
* connected to y implies y is connected to x,
|
|
* and x connected to y, y connected to z, implies x is connected to z.
|
|
* For additional documentation, check
|
|
* <a href="https://en.wikipedia.org/wiki/Disjoint-set_data_structure">Wikipedia
|
|
* Disjoint-set Data Structure</a>
|
|
* <a href="https://en.wikipedia.org/wiki/Dynamic_connectivity">Dynamic
|
|
* Connectivity</a>
|
|
* <a href="https://algs4.cs.princeton.edu/15uf">Section 1.5</a> of
|
|
* <i>Algorithms, 4th Edition, A nice book on DSA</i> by Robert Sedgewick and
|
|
* Kevin Wayne.
|
|
*/
|
|
class UnionFind {
|
|
|
|
/**
|
|
* The disjoint-set of elements. An element is a parent (representative) of a
|
|
* subset of the disjoint set if it
|
|
* is its own parent; ie parent[i] == i. At initialization all elements are
|
|
* their own parent.
|
|
*/
|
|
private final int[] parent;
|
|
|
|
/**
|
|
* The sizes of each subset. This will help us better implemented the data
|
|
* structure.
|
|
*/
|
|
private final int[] size;
|
|
|
|
/**
|
|
* We initialize the data structure with a number of elements.
|
|
*
|
|
* @param numberOfElements the number of elements
|
|
*/
|
|
UnionFind(int numberOfElements) {
|
|
parent = new int[numberOfElements];
|
|
size = new int[numberOfElements];
|
|
|
|
for (int i = 0; i < parent.length; i++) {
|
|
parent[i] = i;
|
|
size[i] = 1;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* We find the parent or representation of an element in the set. We browse up
|
|
* the ancestors of an element until
|
|
* we reach the parent that is its own parent. This parent is the element's
|
|
* representative
|
|
*
|
|
* @param element the element
|
|
* @return the parent representative of this element
|
|
*/
|
|
int find(int element) {
|
|
while (element != parent[element]) {
|
|
// We use a technique called path compression, so that subsequent find
|
|
// operations will be faster
|
|
parent[element] = parent[parent[element]];
|
|
element = parent[element];
|
|
}
|
|
return element;
|
|
}
|
|
|
|
/**
|
|
* Unify or connect two elements. The connection is done by making both elements
|
|
* have the same parent
|
|
* representative
|
|
*
|
|
* @param elementA first element
|
|
* @param elementB second element
|
|
*/
|
|
void union(int elementA, int elementB) {
|
|
int parentA = find(elementA);
|
|
int parentB = find(elementB);
|
|
if (parentA == parentB) {
|
|
return;
|
|
}
|
|
|
|
// We place subsets of smaller sizes under bigger subsets making the find
|
|
// operation more performant
|
|
if (size[parentA] < size[parentB]) {
|
|
parent[parentA] = parentB;
|
|
size[parentB] += size[parentA];
|
|
} else {
|
|
parent[parentB] = parentA;
|
|
size[parentA] += size[parentB];
|
|
}
|
|
}
|
|
}
|