Example #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();
    }
  }
Example #2
0
 /**
  * Computes the transitive closure of the digraph <tt>G</tt>.
  *
  * @param G the digraph
  */
 public TransitiveClosure(Digraph G) {
   tc = new DirectedDFS[G.V()];
   for (int v = 0; v < G.V(); v++) tc[v] = new DirectedDFS(G, v);
 }