Package edu.princeton.cs.algs4
Class Cycle
- Object
-
- edu.princeton.cs.algs4.Cycle
-
public class Cycle extends Object
TheCycle
class represents a data type for determining whether an undirected graph has a simple cycle. The hasCycle operation determines whether the graph has a cycle and, if so, the cycle operation returns one.This implementation uses depth-first search. The constructor takes Θ(V + E) time in the worst case, where V is the number of vertices and E is the number of edges. (The depth-first search part takes only O(V) time; however, checking for self-loops and parallel edges takes Θ(V + E) time in the worst case. Each instance method takes Θ(1) time. It uses Θ(V) extra space (not including the graph).
For additional documentation, see Section 4.1 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
- Author:
- Robert Sedgewick, Kevin Wayne
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Iterable<Integer>
cycle()
Returns a cycle in the graphG
.boolean
hasCycle()
Returns true if the graphG
has a cycle.static void
main(String[] args)
Unit tests theCycle
data type.
-
-
-
Constructor Detail
-
Cycle
public Cycle(Graph G)
Determines whether the undirected graphG
has a cycle and, if so, finds such a cycle.- Parameters:
G
- the undirected graph
-
-
Method Detail
-
hasCycle
public boolean hasCycle()
Returns true if the graphG
has a cycle.- Returns:
true
if the graph has a cycle;false
otherwise
-
cycle
public Iterable<Integer> cycle()
Returns a cycle in the graphG
.- Returns:
- a cycle if the graph
G
has a cycle, andnull
otherwise
-
main
public static void main(String[] args)
Unit tests theCycle
data type.- Parameters:
args
- the command-line arguments
-
-