Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package edu.princeton.cs.algs4;
- /******************************************************************************
- * Compilation: javac WeightedQuickUnionPathCompressionUF.java Execution:
- * java WeightedQuickUnionPathCompressionUF < 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
- *
- * Weighted quick-union with path compression.
- *
- ******************************************************************************/
- /**
- * The {@code WeightedQuickUnionPathCompressionUF} class represents a union–find
- * data structure. It supports the <em>union</em> and <em>find</em> operations,
- * along with methods for determining whether two sites are in the same
- * component and the total number of components.
- * <p>
- * This implementation uses weighted quick union (by size) with full path
- * compression. Initializing a data structure with <em>n</em> sites takes linear
- * time. Afterwards, <em>union</em>, <em>find</em>, and <em>connected</em> take
- * logarithmic time (in the worst case) and <em>count</em> takes constant time.
- * Moreover, the amortized time per <em>union</em>, <em>find</em>, and
- * <em>connected</em> operation has inverse Ackermann complexity.
- * <p>
- * For additional documentation, see
- * <a href="https://algs4.cs.princeton.edu/15uf">Section 1.5</a> of
- * <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
- *
- * @author Robert Sedgewick
- * @author Kevin Wayne
- */
- public class WeightedQuickUnionPathCompressionUF {
- private int[] parent; // parent[i] = parent of i
- private int[] size; // size[i] = number of sites in tree rooted at i
- // Note: not necessarily correct if i is not a root
- // node
- private int count; // number of components
- /**
- * Initializes an empty union–find data structure with {@code n} sites
- * {@code 0} through {@code n-1}. Each site is initially in its own
- * component.
- *
- * @param n
- * the number of sites
- * @throws IllegalArgumentException
- * if {@code n < 0}
- */
- public WeightedQuickUnionPathCompressionUF(int n) {
- count = n;
- parent = new int[n];
- size = new int[n];
- for (int i = 0; i < n; i++) {
- parent[i] = i;
- size[i] = 1;
- }
- }
- /**
- * Returns the number of components.
- *
- * @return the number of components (between {@code 1} and {@code n})
- */
- public int count() {
- return count;
- }
- /**
- * Returns the component identifier for the component containing site
- * {@code p}.
- *
- * @param p
- * the integer representing one site
- * @return the component identifier for the component containing site
- * {@code p}
- * @throws IllegalArgumentException
- * unless {@code 0 <= p < n}
- */
- public int find(int p) {
- validate(p);
- int root = p;
- // we find the root, then we have two choices almost as fast
- while (root != parent[root])
- root = parent[root];
- // option 1: with grand parent path compression
- while (p != root) {
- int newp = parent[p];
- parent[p] = root;
- p = newp;
- }
- // option 2: with full path compression
- // in practice linear time m log n
- while (p != root) {
- int newp = parent[p];
- parent[p] = root;
- p = newp;
- }
- return root;
- }
- /**
- * Returns true if the the two sites are in the same component.
- *
- * @param p
- * the integer representing one site
- * @param q
- * the integer representing the other site
- * @return {@code true} if the two sites {@code p} and {@code q} are in the
- * same component; {@code false} otherwise
- * @throws IllegalArgumentException
- * unless both {@code 0 <= p < n} and {@code 0 <= q < n}
- */
- public boolean connected(int p, int q) {
- return find(p) == find(q);
- }
- // 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));
- }
- }
- /**
- * Merges the component containing site {@code p} with the the component
- * containing site {@code q}.
- *
- * @param p
- * the integer representing one site
- * @param q
- * the integer representing the other site
- * @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;
- // make smaller root point to larger one
- if (size[rootP] < size[rootQ]) {
- parent[rootP] = rootQ;
- size[rootQ] += size[rootP];
- } else {
- parent[rootQ] = rootP;
- size[rootP] += size[rootQ];
- }
- count--;
- }
- /**
- * Reads in a sequence of pairs of integers (between 0 and n-1) from
- * standard input, where each integer represents some site; if the sites are
- * in different components, merge the two components and print the pair to
- * standard output.
- *
- * @param args
- * the command-line arguments
- */
- public static void main(String[] args) {
- int n = StdIn.readInt();
- WeightedQuickUnionPathCompressionUF uf = new WeightedQuickUnionPathCompressionUF(
- n);
- while (!StdIn.isEmpty()) {
- int p = StdIn.readInt();
- int q = StdIn.readInt();
- if (uf.connected(p, q))
- continue;
- uf.union(p, q);
- StdOut.println(p + " " + q);
- }
- StdOut.println(uf.count() + " components");
- }
- }
- /******************************************************************************
- * Copyright 2002-2016, Robert Sedgewick and Kevin Wayne.
- *
- * This file is part of algs4.jar, which accompanies the textbook
- *
- * Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne, Addison-Wesley
- * Professional, 2011, ISBN 0-321-57351-X. http://algs4.cs.princeton.edu
- *
- *
- * algs4.jar is free software: you can redistribute it and/or modify it under
- * the terms of the GNU General Public License as published by the Free Software
- * Foundation, either version 3 of the License, or (at your option) any later
- * version.
- *
- * algs4.jar is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
- * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * algs4.jar. If not, see http://www.gnu.org/licenses.
- ******************************************************************************/
Add Comment
Please, Sign In to add comment