Exemple #1
0
 // does the id[] array contain the strongly connected components?
 private boolean check(Digraph G) {
     TransitiveClosure tc = new TransitiveClosure(G);
     for (int v = 0; v < G.V(); v++) {
         for (int w = 0; w < G.V(); w++) {
             if (stronglyConnected(v, w) != (tc.reachable(v, w) && tc.reachable(w, v)))
                 return false;
         }
     }
     return true;
 }
    /**
     * Unit tests the {@code TransitiveClosure} data type.
     *
     * @param args the command-line arguments
     */
    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();
        }
    }