public class EdgeWeightedDirectedCycle 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 edge-weighted
digraph is acyclic.
For additional documentation, see Section 4.4 of Algorithms, 4th Edition, by Robert Sedgewick and Kevin Wayne.
Constructor and Description |
---|
EdgeWeightedDirectedCycle(EdgeWeightedDigraph G)
Determines whether the edge-weighted digraph G has a directed cycle and,
if so, finds such a cycle.
|
Modifier and Type | Method and Description |
---|---|
java.lang.Iterable<DirectedEdge> |
cycle()
Returns a directed cycle if the edge-weighted digraph has a directed cycle,
and null otherwise.
|
boolean |
hasCycle()
Does the edge-weighted digraph have a directed cycle?
|
static void |
main(java.lang.String[] args)
Unit tests the EdgeWeightedDirectedCycle data type.
|
public EdgeWeightedDirectedCycle(EdgeWeightedDigraph G)
G
- the edge-weighted digraphpublic boolean hasCycle()
public java.lang.Iterable<DirectedEdge> cycle()
public static void main(java.lang.String[] args)