public class Cycle 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.
For additional documentation, see Section 4.1 of Algorithms, 4th Edition, by Robert Sedgewick and Kevin Wayne.
Constructor and Description |
---|
Cycle(Graph G)
Determines whether the undirected graph G has a cycle and, if so,
finds such a cycle.
|
public Cycle(Graph G)
G
- the graphpublic boolean hasCycle()
public java.lang.Iterable<java.lang.Integer> cycle()
public static void main(java.lang.String[] args)