Modificato alcune classi

This commit is contained in:
2014-06-01 20:32:07 +02:00
parent 71cea16bcb
commit 01c626d0e0
6 changed files with 36 additions and 54 deletions

View File

@@ -1,5 +1,5 @@
package graph;
//begin#fragment FindCycleDFS
import position.NodePositionList;
import position.Position;
@@ -9,25 +9,26 @@ import position.PositionList;
//end#fragment FindCycleDFS
/* @author Roberto Tamassia, Michael Goodrich, Eric Zamore
*/
//begin#fragment FindCycleDFS
public class FindCycleDFS<V, E>
extends DFS<V, E, Object, Iterable<Position>> {
protected PositionList<Position> cycle; // sequence of edges of the cycle
protected boolean done;
protected Vertex<V> cycleStart;
//end#fragment FindCycleDFS
/**
* Executes the DFS algorithm.
* @return collection containing the vertices and
* edges of a cycle.
*/
//begin#fragment FindCycleDFS
public void setup() {
cycle = new NodePositionList<Position>();
done = false;
}
protected void startVisit(Vertex<V> v) { cycle.addLast(v); }
protected void finishVisit(Vertex<V> v) {
cycle.remove(cycle.last()); // remove v from cycle
if (!cycle.isEmpty()) cycle.remove(cycle.last()); // remove edge into v from cycle
@@ -35,13 +36,15 @@ public class FindCycleDFS<V, E>
protected void traverseDiscovery(Edge<E> e, Vertex<V> from) {
cycle.addLast(e);
}
protected void traverseBack(Edge<E> e, Vertex<V> from) {
cycle.addLast(e); // back edge e creates a cycle
cycleStart = graph.opposite(from, e);
cycle.addLast(cycleStart); // first vertex completes the cycle
done = true;
}
protected boolean isDone() { return done; }
protected boolean isDone() { return done; }
public Iterable<Position> finalResult(Iterable<Position> r) {
// remove the vertices and edges from start to cycleStart
if (!cycle.isEmpty()) {
@@ -54,5 +57,5 @@ public class FindCycleDFS<V, E>
return cycle; // list of the vertices and edges of the cycle
}
}
//end#fragment FindCycleDFS