类 DirectedCycleX
- java.lang.Object
-
- edu.princeton.cs.algs4.DirectedCycleX
-
public class DirectedCycleX extends java.lang.Object
TheDirectedCycleX
class 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 a nonrecursive, queue-based algorithm. 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
DirectedCycle
for a recursive version that uses depth-first search. SeeTopological
orTopologicalX
to compute a topological order when the digraph is acyclic.For additional documentation, see Section 4.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
-
-
构造器概要
构造器 构造器 说明 DirectedCycleX(Digraph G)
-
-
-
构造器详细资料
-
DirectedCycleX
public DirectedCycleX(Digraph G)
-
-
方法详细资料
-
cycle
public java.lang.Iterable<java.lang.Integer> cycle()
Returns a directed cycle if the digraph has a directed cycle, andnull
otherwise.- 返回:
- a directed cycle (as an iterable) if the digraph has a directed cycle,
and
null
otherwise
-
hasCycle
public boolean hasCycle()
Does the digraph have a directed cycle?- 返回:
true
if the digraph has a directed cycle,false
otherwise
-
main
public static void main(java.lang.String[] args)
-
-