You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
4.6 KiB
Java

import edu.princeton.cs.introcs.In;
import edu.princeton.cs.introcs.StdOut;
/*************************************************************************
* Compilation: javac DirectedCycle.java
* Execution: java DirectedCycle < input.txt
* Dependencies: Digraph.java Stack.java StdOut.java In.java
* Data files: http://algs4.cs.princeton.edu/42directed/tinyDG.txt
* http://algs4.cs.princeton.edu/42directed/tinyDAG.txt
*
* Finds a directed cycle in a digraph.
* Runs in O(E + V) time.
*
* % java DirectedCycle tinyDG.txt
* Cycle: 3 5 4 3
*
* % java DirectedCycle tinyDAG.txt
* No cycle
*
*************************************************************************/
/**
* The DirectedCycle class represents a data type for
* determining whether a digraph has a directed cycle.
* The hasCycle operation determines whether the digraph has
* a directed cycle and, and of 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 {@link Topological} to compute a topological order if the
* digraph is acyclic.
*
* For additional documentation, see <a href="/algs4/42digraph">Section 4.2</a> of
* Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class DirectedCycle {
private boolean[] marked; // marked[v] = has vertex v been marked?
private int[] edgeTo; // edgeTo[v] = previous vertex on path to v
private boolean[] onStack; // onStack[v] = is vertex on the stack?
private Stack<Integer> cycle; // directed cycle (or null if no such cycle)
/**
* Determines whether the digraph G has a directed cycle and, if so,
* finds such a cycle.
* @param G the digraph
*/
public DirectedCycle(Digraph G) {
marked = new boolean[G.V()];
onStack = new boolean[G.V()];
edgeTo = new int[G.V()];
for (int v = 0; v < G.V(); v++)
if (!marked[v]) dfs(G, v);
}
// check that algorithm computes either the topological order or finds a directed cycle
private void dfs(Digraph G, int v) {
onStack[v] = true;
marked[v] = true;
for (int w : G.adj(v)) {
// short circuit if directed cycle found
if (cycle != null) return;
//found new vertex, so recur
else if (!marked[w]) {
edgeTo[w] = v;
dfs(G, w);
}
// trace back directed cycle
else if (onStack[w]) {
cycle = new Stack<Integer>();
for (int x = v; x != w; x = edgeTo[x]) {
cycle.push(x);
}
cycle.push(w);
cycle.push(v);
}
}
onStack[v] = false;
}
/**
* Does the digraph have a directed cycle?
* @return true if the digraph has a directed cycle, false otherwise
*/
public boolean hasCycle() {
return cycle != null;
}
/**
* Returns a directed cycle if the digraph has a directed cycle, and null otherwise.
* @return a directed cycle (as an iterable) if the digraph has a directed cycle,
* and null otherwise
*/
public Iterable<Integer> cycle() {
return cycle;
}
// certify that digraph is either acyclic or has a directed cycle
private boolean check(Digraph G) {
if (hasCycle()) {
// verify cycle
int first = -1, last = -1;
for (int v : cycle()) {
if (first == -1) first = v;
last = v;
}
if (first != last) {
System.err.printf("cycle begins with %d and ends with %d\n", first, last);
return false;
}
}
return true;
}
/**
* Unit tests the DirectedCycle data type.
*/
public static void main(String[] args) {
In in = new In(args[0]);
Digraph G = new Digraph(in);
DirectedCycle finder = new DirectedCycle(G);
if (finder.hasCycle()) {
StdOut.print("Cycle: ");
for (int v : finder.cycle()) {
StdOut.print(v + " ");
}
StdOut.println();
}
else {
StdOut.println("No cycle");
}
}
}