001package algs15;
002import java.util.Arrays;
003import  stdlib.*;
004
005/* **************************************************************************
006 *  Compilation:  javac QuickUnionPathCompressionUF.java
007 *  Execution:  java QuickUnionPathCompressionUF < input.txt
008 *  Dependencies: StdIn.java StdOut.java
009 *
010 *  Quick-union with path compression.
011 *
012 ****************************************************************************/
013
014public class CompressionUF implements UF {
015        private int[] id;    // id[i] = parent of i
016        private int count;   // number of components
017
018        // Create an empty union find data structure with N isolated sets.
019        public CompressionUF(int N) {
020                count = N;
021                id = new int[N];
022                for (int i = 0; i < N; i++) {
023                        id[i] = i;
024                }
025        }
026
027        // Return the number of disjoint sets.
028        public int count() {
029                return count;
030        }
031        
032        // Are objects p and q in the same set?
033        public boolean connected(int p, int q) {
034                return find(p) == find(q);
035        }
036        
037        // Return component identifier for component containing p
038        public int find(int p) {
039                int root = p;
040                while (root != id[root])
041                        root = id[root];
042                while (id[p] != root) {
043                        int newp = id[p];
044                        id[p] = root;
045                        if (TestUF.SHOW_COMPRESSION_STEPS) { StdOut.format("%2d %2d> %2d%s\n", p, root, this.count(), this); toGraphviz(); }
046                        p = newp;
047                }
048                return root;
049        }
050
051        // Replace sets containing p and q with their union.
052        public void union(int p, int q) {
053                int pid = find(p); // loser
054                int qid = find(q); // champion
055                if (pid == qid) return;
056                id[pid] = qid;
057                count--;
058        }
059
060        public String toString() { return Arrays.toString (id); }
061        public void toGraphviz() { GraphvizBuilder.ufToFile (id); }
062
063        public static void main(String[] args) {
064                boolean print = true;
065                StdIn.fromFile ("data/tinyUF.txt"); 
066                //StdIn.fromFile ("data/mediumUF.txt"); print = false;
067                //StdIn.fromFile ("data/largeUF.txt"); print = false;
068
069                int N = StdIn.readInt();
070                CompressionUF uf = new CompressionUF(N);
071                if (print) { uf.toGraphviz(); StdOut.println("   : " + uf); }
072
073                // read in a sequence of pairs of integers (each in the range 0 to N-1),
074                // calling find() for each pair: If the members of the pair are not already
075                // call union() and print the pair.
076                Stopwatch sw = new Stopwatch ();
077                while (!StdIn.isEmpty()) {
078                        int p = StdIn.readInt();
079                        int q = StdIn.readInt();
080                        if (uf.connected(p, q)) continue;
081                        uf.union(p, q);
082                        if (print) { StdOut.println(p + " " + q + ": " + uf); uf.toGraphviz(); }
083                }
084                StdOut.format("CompressionUF # components: %d [%f]\n", uf.count(), sw.elapsedTime ());
085        }
086
087}