/****************************************************************************** * Compilation: javac QuickUnionUF.java * Execution: java QuickUnionUF < input.txt * Dependencies: StdIn.java StdOut.java * Data files: https://algs4.cs.princeton.edu/15uf/tinyUF.txt * https://algs4.cs.princeton.edu/15uf/mediumUF.txt * https://algs4.cs.princeton.edu/15uf/largeUF.txt * * Quick-union algorithm. * ******************************************************************************/ /** * The {@code QuickUnionUF} 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: *

*

* 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 quick union. * The constructor takes Θ(n) time, where * n is the number of sites. * The union and find operations take * Θ(n) time in the worst case. * The count operation takes Θ(1) time. *

* For alternative implementations of the same API, see * {@link UF}, {@link QuickFindUF}, and {@link WeightedQuickUnionUF}. * For additional documentation, * see Section 1.5 of * Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne. * * @author Robert Sedgewick * @author Kevin Wayne */ public class QuickUnionUF { private int[] parent; // parent[i] = parent of i private int count; // number of components /** * Initializes an empty union-find data structure with * {@code n} elements {@code 0} through {@code n-1}. * Initially, each element is in its own set. * * @param n the number of elements * @throws IllegalArgumentException if {@code n < 0} */ public QuickUnionUF(int n) { parent = new int[n]; count = n; for (int i = 0; i < n; i++) { parent[i] = i; } } /** * Returns the number of sets. * * @return the number of sets (between {@code 1} and {@code n}) */ public int count() { return count; } /** * 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 IllegalArgumentException unless {@code 0 <= p < n} */ public int find(int p) { validate(p); while (p != parent[p]) p = parent[p]; return p; } // validate that p is a valid index private void validate(int p) { int n = parent.length; if (p < 0 || p >= n) { throw new IllegalArgumentException("index " + p + " is not between 0 and " + (n-1)); } } /** * Returns true if the two elements are in the same set. * * @param p one element * @param q the other element * @return {@code true} if {@code p} and {@code q} are in the same set; * {@code false} otherwise * @throws IllegalArgumentException unless * both {@code 0 <= p < n} and {@code 0 <= q < n} * @deprecated Replace with two calls to {@link #find(int)}. */ @Deprecated public boolean connected(int p, int q) { return find(p) == find(q); } /** * Merges the set containing element {@code p} with the set * containing element {@code q}. * * @param p one element * @param q the other element * @throws IllegalArgumentException unless * both {@code 0 <= p < n} and {@code 0 <= q < n} */ public void union(int p, int q) { int rootP = find(p); int rootQ = find(q); if (rootP == rootQ) return; parent[rootP] = rootQ; count--; } /** * Reads an integer {@code n} and a sequence of pairs of integers * (between {@code 0} and {@code n-1}) from standard input, where each integer * in the pair represents some element; * if the elements are in different sets, merge the two sets * and print the pair to standard output. * * @param args the command-line arguments */ public static void main(String[] args) { int n = StdIn.readInt(); QuickUnionUF uf = new QuickUnionUF(n); while (!StdIn.isEmpty()) { int p = StdIn.readInt(); int q = StdIn.readInt(); if (uf.find(p) == uf.find(q)) continue; uf.union(p, q); StdOut.println(p + " " + q); } StdOut.println(uf.count() + " components"); } }