import edu.princeton.cs.introcs.In; import edu.princeton.cs.introcs.StdOut; /************************************************************************* * Compilation: javac TransitiveClosure.java * Execution: java TransitiveClosure filename.txt * Dependencies: Digraph.java DepthFirstDirectedPaths.java In.java StdOut.java * Data files: http://algs4.cs.princeton.edu/42directed/tinyDG.txt * * Compute transitive closure of a digraph and support * reachability queries. * * Preprocessing time: O(V(E + V)) time. * Query time: O(1). * Space: O(V^2). * * % java TransitiveClosure tinyDG.txt * 0 1 2 3 4 5 6 7 8 9 10 11 12 * -------------------------------------------- * 0: T T T T T T * 1: T * 2: T T T T T T * 3: T T T T T T * 4: T T T T T T * 5: T T T T T T * 6: T T T T T T T T T T T * 7: T T T T T T T T T T T T T * 8: T T T T T T T T T T T T T * 9: T T T T T T T T T T * 10: T T T T T T T T T T * 11: T T T T T T T T T T * 12: T T T T T T T T T T * *************************************************************************/ /** * The TransitiveClosure class represents a data type for * computing the transitive closure of a digraph. * * This implementation runs depth-first search from each vertex. * The constructor takes time proportional to V ( V + E ) * (in the worst case) and uses space proportional to V 2, * where V is the number of vertices and E is the number of edges. * * For additional documentation, see Section 4.2 of * Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne. * * @author Robert Sedgewick * @author Kevin Wayne */ public class TransitiveClosure { private DirectedDFS[] tc; // tc[v] = reachable from v /** * Computes the transitive closure of the digraph G . * @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); } /** * Is there a directed path from vertex v to vertex w in the digraph? * @param v the source vertex * @param w the target vertex * @return true if there is a directed path from v to w , * false otherwise */ public boolean reachable(int v, int w) { return tc[v].marked(w); } /** * Unit tests the TransitiveClosure 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(); } } }