Exemplo n.º 1
0
 /**
  * Reads in a an integer <tt>N</tt> and a sequence of pairs of integers (between <tt>0</tt> and
  * <tt>N-1</tt>) from standard input, where each integer in the pair represents some site; if the
  * sites are in different components, merge the two components and print the pair to standard
  * output.
  */
 public static void main(String[] args) {
   for (int N = 250; N <= 10000000; N *= 1.25) {
     double time = quickUnion(N);
     StdOut.printf("%7d %5.1f\n", N, time);
     FileOut.print(N);
     FileOut.print("\t");
     FileOut.println(time);
   }
 }
Exemplo n.º 2
0
 private static double quickUnion(int n) {
   Stopwatch timer = new Stopwatch();
   UFQuickUnion uf = new UFQuickUnion(n);
   for (int i = 0; i < n; i++) {
     int p = StdRandom.uniform(n);
     int q = StdRandom.uniform(n);
     if (uf.connected(p, q)) continue;
     uf.union(p, q);
     // StdOut.println(p + " " + q);
   }
   StdOut.println(uf.count() + " components");
   return timer.elapsedTime();
 }
Exemplo n.º 3
0
  /** Unit tests the <tt>TransitiveClosure</tt> data type. */
  public static void main(String[] args) {
    In in = new In(args[0]);
    Digraph G = new Digraph(in);

    TransitiveClosure tc = new TransitiveClosure(G);

    // print header
    StdOut.print("     ");
    for (int v = 0; v < G.V(); v++) StdOut.printf("%3d", v);
    StdOut.println();
    StdOut.println("--------------------------------------------");

    // print transitive closure
    for (int v = 0; v < G.V(); v++) {
      StdOut.printf("%3d: ", v);
      for (int w = 0; w < G.V(); w++) {
        if (tc.reachable(v, w)) StdOut.printf("  T");
        else StdOut.printf("   ");
      }
      StdOut.println();
    }
  }