public class DirectedCycle extends java.lang.Object
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.
See 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 |
---|
DirectedCycle(Digraph G)
Determines whether the digraph G has a directed cycle and, if so,
finds such a cycle.
|
Modifier and Type | Method and Description |
---|---|
java.lang.Iterable<java.lang.Integer> |
cycle()
Returns a directed cycle if the digraph has a directed cycle, and null otherwise.
|
boolean |
hasCycle()
Does the digraph have a directed cycle?
|
static void |
main(java.lang.String[] args)
Unit tests the DirectedCycle data type.
|
public DirectedCycle(Digraph G)
G
- the digraphpublic boolean hasCycle()
public java.lang.Iterable<java.lang.Integer> cycle()
public static void main(java.lang.String[] args)