public class DirectedCycle extends Object
DirectedCycleclass represents a data type for determining whether a digraph has a directed cycle. The hasCycle operation determines whether the digraph has a simple directed cycle and, if so, the cycle operation returns one.
This implementation uses depth-first search. The constructor takes time proportional to V + E (in the worst case), where V is the number of vertices and E is the number of edges. Afterwards, the hasCycle operation takes constant time; the cycle operation takes time proportional to the length of the cycle.
Topological to compute a topological order if the
digraph is acyclic.
For additional documentation, see Section 4.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
|Constructor and Description|
Determines whether the digraph
|Modifier and Type||Method and Description|
Returns a directed cycle if the digraph has a directed cycle, and
Does the digraph have a directed cycle?
Unit tests the
public DirectedCycle(Digraph G)
Ghas a directed cycle and, if so, finds such a cycle.
G- the digraph
public boolean hasCycle()
trueif the digraph has a directed cycle,
public static void main(String args)
args- the command-line arguments