edu.princeton.cs.algs4
Class EdgeWeightedDirectedCycle
- java.lang.Object
-
- edu.princeton.cs.algs4.EdgeWeightedDirectedCycle
-
public class EdgeWeightedDirectedCycle extends java.lang.ObjectTheEdgeWeightedDirectedCycleclass represents a data type for determining whether an edge-weighted digraph has a directed cycle. The hasCycle operation determines whether the edge-weighted digraph has a 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.
See
Topologicalto 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 Summary
Constructors Constructor and Description EdgeWeightedDirectedCycle(EdgeWeightedDigraph G)Determines whether the edge-weighted digraphGhas a directed cycle and, if so, finds such a cycle.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description java.lang.Iterable<DirectedEdge>cycle()Returns a directed cycle if the edge-weighted digraph has a directed cycle, andnullotherwise.booleanhasCycle()Does the edge-weighted digraph have a directed cycle?static voidmain(java.lang.String[] args)Unit tests theEdgeWeightedDirectedCycledata type.
-
-
-
Constructor Detail
-
EdgeWeightedDirectedCycle
public EdgeWeightedDirectedCycle(EdgeWeightedDigraph G)
Determines whether the edge-weighted digraphGhas a directed cycle and, if so, finds such a cycle.- Parameters:
G- the edge-weighted digraph
-
-
Method Detail
-
hasCycle
public boolean hasCycle()
Does the edge-weighted digraph have a directed cycle?- Returns:
trueif the edge-weighted digraph has a directed cycle,falseotherwise
-
cycle
public java.lang.Iterable<DirectedEdge> cycle()
Returns a directed cycle if the edge-weighted digraph has a directed cycle, andnullotherwise.- Returns:
- a directed cycle (as an iterable) if the edge-weighted digraph
has a directed cycle, and
nullotherwise
-
main
public static void main(java.lang.String[] args)
Unit tests theEdgeWeightedDirectedCycledata type.- Parameters:
args- the command-line arguments
-
-
DataMelt 3.0 © DataMelt by jWork.ORG