|
| 1 | +package io.github.hextriclosan.algorithm.collections; |
| 2 | + |
| 3 | +import io.github.hextriclosan.algorithm.collections.disjointset.FindCompressStrategy; |
| 4 | +import io.github.hextriclosan.algorithm.collections.disjointset.FullCompression; |
| 5 | + |
| 6 | +import java.util.Collection; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.Objects; |
| 10 | + |
| 11 | +/** |
| 12 | + * A data structure that maintains a collection of disjoint (non-overlapping) sets. |
| 13 | + * It supports efficient union and find operations, as well as strategies for path compression and ranking. |
| 14 | + * |
| 15 | + * @param <E> the type of elements stored in the Disjoint Set |
| 16 | + */ |
| 17 | +public class DisjointSet<E> { |
| 18 | + private final Map<E, E> parentByElement; |
| 19 | + private final Map<E, Integer> rankByElement; |
| 20 | + private final FindCompressStrategy<E> findCompressStrategy; |
| 21 | + |
| 22 | + /** |
| 23 | + * Constructs a DisjointSet with default initializations using {@link FullCompression} strategy. |
| 24 | + */ |
| 25 | + public DisjointSet() { |
| 26 | + this(new HashMap<>(), new HashMap<>(), new FullCompression<>()); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Constructs a DisjointSet with a specified path compression strategy. |
| 31 | + * |
| 32 | + * @param findCompressStrategy the strategy used for path compression and find operations |
| 33 | + */ |
| 34 | + public DisjointSet(FindCompressStrategy<E> findCompressStrategy) { |
| 35 | + this(new HashMap<>(), new HashMap<>(), findCompressStrategy); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Constructs a DisjointSet with custom initial parent and rank mappings, along with a specified path compression strategy. |
| 40 | + * This constructor allows the user to recreate a DisjointSet with a particular state, including predefined parent-to-element |
| 41 | + * relationships, rank values, and a customized path compression strategy. |
| 42 | + * |
| 43 | + * @param parentByElement the initial mapping of elements to their parent elements |
| 44 | + * @param rankByElement the initial mapping of elements to their ranks (or sizes) |
| 45 | + * @param findCompressStrategy the strategy used for path compression and find operations |
| 46 | + * @throws NullPointerException if any of the parameters are null |
| 47 | + */ |
| 48 | + public DisjointSet(Map<E, E> parentByElement, Map<E, Integer> rankByElement, FindCompressStrategy<E> findCompressStrategy) { |
| 49 | + Objects.requireNonNull(parentByElement, "parentByElement"); |
| 50 | + Objects.requireNonNull(rankByElement, "rankByElement"); |
| 51 | + Objects.requireNonNull(findCompressStrategy, "findCompressStrategy"); |
| 52 | + this.parentByElement = parentByElement; |
| 53 | + this.rankByElement = rankByElement; |
| 54 | + this.findCompressStrategy = findCompressStrategy; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Creates a new set with the specified element in the Disjoint Set structure. |
| 59 | + * If the element is already present, no action is taken. |
| 60 | + * |
| 61 | + * @param element the element to initialize as a new set |
| 62 | + * @throws NullPointerException if the specified element is null |
| 63 | + */ |
| 64 | + public void makeSet(E element) { |
| 65 | + Objects.requireNonNull(element, "element"); |
| 66 | + if (!parentByElement.containsKey(element)) { |
| 67 | + parentByElement.put(element, element); |
| 68 | + rankByElement.put(element, 0); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Creates new sets for each element in the specified collection in the Disjoint Set structure. |
| 74 | + * If an element is already present, no action is taken for that element. |
| 75 | + * |
| 76 | + * @param elements the collection of elements to initialize as new sets |
| 77 | + * @throws NullPointerException if the specified collection is null or contains null elements |
| 78 | + */ |
| 79 | + public void makeSets(Collection<E> elements) { |
| 80 | + Objects.requireNonNull(elements, "elements"); |
| 81 | + for (E element : elements) { |
| 82 | + makeSet(element); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Unites the sets that contain the specified elements into a single set. |
| 88 | + * |
| 89 | + * @param first the first element |
| 90 | + * @param second the second element |
| 91 | + * @throws NullPointerException if either of the elements is null |
| 92 | + */ |
| 93 | + public void union(E first, E second) { |
| 94 | + Objects.requireNonNull(first, "first"); |
| 95 | + Objects.requireNonNull(second, "second"); |
| 96 | + E firstRoot = find(first); |
| 97 | + E secondRoot = find(second); |
| 98 | + |
| 99 | + if (firstRoot == secondRoot) { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + int firstRootRank = rankByElement.get(firstRoot); |
| 104 | + int secondRootRank = rankByElement.get(secondRoot); |
| 105 | + |
| 106 | + if (firstRootRank > secondRootRank) { |
| 107 | + parentByElement.put(secondRoot, firstRoot); |
| 108 | + } else { |
| 109 | + parentByElement.put(firstRoot, secondRoot); |
| 110 | + if (firstRootRank == secondRootRank) { |
| 111 | + rankByElement.put(secondRoot, secondRootRank + 1); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Finds the representative (root) of the set containing the specified element, |
| 118 | + * using the configured find and path compression strategy. |
| 119 | + * |
| 120 | + * @param element the element to find |
| 121 | + * @return the representative (root) of the set containing the element |
| 122 | + * @throws NullPointerException if the element is null |
| 123 | + */ |
| 124 | + public E find(E element) { |
| 125 | + Objects.requireNonNull(element, "element"); |
| 126 | + return findCompressStrategy.apply(parentByElement, element); |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments