| 
001002
 003
 004
 005
 006
 007
 008
 009
 010
 011
 012
 013
 014
 015
 016
 017
 018
 019
 020
 021
 022
 023
 024
 025
 026
 027
 028
 029
 030
 031
 032
 033
 034
 035
 036
 037
 038
 039
 040
 041
 042
 043
 044
 045
 046
 047
 048
 049
 050
 051
 052
 053
 054
 055
 056
 057
 058
 059
 060
 061
 062
 063
 064
 065
 066
 067
 068
 069
 070
 071
 072
 073
 074
 075
 076
 077
 078
 079
 080
 081
 082
 083
 084
 085
 086
 087
 088
 089
 090
 091
 092
 093
 094
 095
 096
 097
 098
 099
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 
 | package algs15;
import java.util.Arrays;
import stdlib.*;
/* **************************************************************************
 *  Compilation:  javac WeightedUF.java
 *  Execution:    java UF < input.txt
 *  Dependencies: StdIn.java StdOut.java
 *  Data files:   http://algs4.cs.princeton.edu/15uf/tinytxt
 *                http://algs4.cs.princeton.edu/15uf/mediumtxt
 *                http://algs4.cs.princeton.edu/15uf/largetxt
 *
 *  Weighted quick-union (without path compression).
 *
 *  % java UF < tinyUF.txt
 *  4 3
 *  3 8
 *  6 5
 *  9 4
 *  2 1
 *  5 0
 *  7 2
 *  6 1
 *  # components: 2
 *
 *  % java UF < largeUF.txt
 *  UF # components: 6 [4.372000]
 *
 ****************************************************************************/
/**
 *  The {@code UF} class represents a union-find data data structure.
 *  It supports the <em>union</em> and <em>find</em>
 *  operations, along with a method for determining the number of
 *  disjoint sets.
 *  <p>
 *  This implementation uses weighted quick union.
 *  Creating a data structure with N objects takes linear time.
 *  Afterwards, all operations are logarithmic worst-case time.
 *  <p>
 *  For additional documentation, see <a href="http://algs4.cs.princeton.edu/15uf">Section 1.5</a> of
 *  <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
 */
public class WeightedUF implements UF {
  private int[] id;    // id[i] = parent of i
  private int[] sz;    // sz[i] = number of objects in subtree rooted at i
  private int count;   // number of components
  /**
   * Create an empty union find data structure with N isolated sets.
   */
  public WeightedUF(int N) {
    if (N < 0) throw new IllegalArgumentException();
    count = N;
    id = new int[N];
    sz = new int[N];
    for (int i = 0; i < N; i++) {
      id[i] = i;
      sz[i] = 1;
    }
  }
  /**
   * Return the number of disjoint sets.
   */
  public int count() {
    return count;
  }
  /**
   * Are objects p and q in the same set?
   */
  public boolean connected(int p, int q) {
    return find(p) == find(q);
  }
  
  /**
   * Return the id of component corresponding to object p.
   */
  public int find(int p) {
    int root = p;
    while (root != id[root])
      root = id[root];
    return root;
  }
  /**
   * Replace sets containing p and q with their union.
   */
  public void union(int p, int q) {
    int pid = find(p);
    int qid = find(q);
    if (pid == qid) return;
    // make smaller root point to larger one
    // in the case of a tie, p is the champion
    if   (sz[pid] < sz[qid]) { id[pid] = qid; sz[qid] += sz[pid]; }
    else                     { id[qid] = pid; sz[pid] += sz[qid]; }
    count--;
  }
  public String toString() { return Arrays.toString (id); }
  public void toGraphviz() { GraphvizBuilder.ufToFile (id); }
  public static void main(String[] args) {
    boolean print = true;
    StdIn.fromFile ("data/tinyUF.txt"); 
    //StdIn.fromFile ("data/mediumUF.txt"); print = false;
    //StdIn.fromFile ("data/largeUF.txt"); print = false;
    int N = StdIn.readInt();
    WeightedUF uf = new WeightedUF(N);
    if (print) { GraphvizBuilder.ufToFile (uf.id); StdOut.println("   : " + uf); }
    // read in a sequence of pairs of integers (each in the range 0 to N-1),
    // calling find() for each pair: If the members of the pair are not already
    // call union() and print the pair.
    Stopwatch sw = new Stopwatch ();
    while (!StdIn.isEmpty()) {
      int p = StdIn.readInt();
      int q = StdIn.readInt();
      if (uf.connected(p, q)) continue;
      uf.union(p, q);
      if (print) { StdOut.println(p + " " + q + ": " + uf); GraphvizBuilder.ufToFile (uf.id); }
    }
    StdOut.format("WeightedUF # components: %d [%f]\n", uf.count(), sw.elapsedTime ());
  }
}
 |