示例#1
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();
    }
  }
示例#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();
 }