Package edu.princeton.cs.algs4
Class TransitiveClosure
- Object
-
- edu.princeton.cs.algs4.TransitiveClosure
-
public class TransitiveClosure extends Object
TheTransitiveClosureclass represents a data type for computing the transitive closure of a digraph.This implementation runs depth-first search from each vertex. The constructor takes Θ(V(V + E)) in the worst case, where V is the number of vertices and E is the number of edges. Each instance method takes Θ(1) time. It uses Θ(V2) extra space (not including the digraph).
For large digraphs, you may want to consider a more sophisticated algorithm. Nuutila proposes two algorithm for the problem (based on strong components and an interval representation) that runs in Θ(E + V) time on typical digraphs. For additional documentation, see Section 4.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
- Author:
- Robert Sedgewick, Kevin Wayne
-
-
Constructor Summary
Constructors Constructor Description TransitiveClosure(Digraph digraph)Computes the transitive closure of a digraph.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static voidmain(String[] args)Unit tests theTransitiveClosuredata type.booleanreachable(int v, int w)Is there a directed path from vertexvto vertexwin the digraph?
-
-
-
Constructor Detail
-
TransitiveClosure
public TransitiveClosure(Digraph digraph)
Computes the transitive closure of a digraph.- Parameters:
digraph- the digraph
-
-
Method Detail
-
reachable
public boolean reachable(int v, int w)Is there a directed path from vertexvto vertexwin the digraph?- Parameters:
v- the source vertexw- the target vertex- Returns:
trueif there is a directed path fromvtow,falseotherwise- Throws:
IllegalArgumentException- unless0 <= v < VIllegalArgumentException- unless0 <= w < V
-
main
public static void main(String[] args)
Unit tests theTransitiveClosuredata type.- Parameters:
args- the command-line arguments
-
-