Modificato alcune classi
This commit is contained in:
@@ -1,9 +1,5 @@
|
|||||||
//gioel
|
|
||||||
|
|
||||||
package graph;
|
package graph;
|
||||||
|
|
||||||
import exceptions.InvalidKeyException;
|
|
||||||
|
|
||||||
public class ComponentsBFS<V, E> extends BFS<V, E, Object, Integer> {
|
public class ComponentsBFS<V, E> extends BFS<V, E, Object, Integer> {
|
||||||
protected Integer compNumber;
|
protected Integer compNumber;
|
||||||
protected Object COMPONENT = new Object();
|
protected Object COMPONENT = new Object();
|
||||||
|
|||||||
@@ -1,35 +1,21 @@
|
|||||||
package graph;
|
package graph;
|
||||||
|
|
||||||
//begin#fragment CC
|
|
||||||
|
|
||||||
import exceptions.InvalidKeyException;
|
|
||||||
|
|
||||||
/** This class extends DFS to compute the connected components of a graph. */
|
/** This class extends DFS to compute the connected components of a graph. */
|
||||||
public class ComponentsDFS<V, E> extends DFS<V, E, Object, Integer> {
|
public class ComponentsDFS<V, E> extends DFS<V, E, Object, Integer> {
|
||||||
protected Integer compNumber; // Connected component number
|
protected Integer compNumber; // Connected component number
|
||||||
protected Object COMPONENT = new Object(); // Connected comp. selector
|
protected Object COMPONENT = new Object(); // Connected comp. selector
|
||||||
protected void setup() { compNumber = 1; }
|
protected void setup() { compNumber = 1; }
|
||||||
protected void startVisit(Vertex<V> v) {
|
protected void startVisit(Vertex<V> v) {
|
||||||
try{
|
|
||||||
v.put(COMPONENT, compNumber);
|
v.put(COMPONENT, compNumber);
|
||||||
}
|
|
||||||
catch (InvalidKeyException e){
|
|
||||||
//
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
protected Integer finalResult(Integer dfsResult) {
|
protected Integer finalResult(Integer dfsResult) {
|
||||||
try{
|
|
||||||
for (Vertex<V> v : graph.vertices()) // check for any unvisited vertices
|
for (Vertex<V> v : graph.vertices()) // check for any unvisited vertices
|
||||||
if (v.get(STATUS) == UNVISITED) {
|
if (v.get(STATUS) == UNVISITED) {
|
||||||
compNumber += 1; // we have found another connected component
|
compNumber += 1; // we have found another connected component
|
||||||
dfsTraversal(v); // visit all the vertices of this component
|
dfsTraversal(v); // visit all the vertices of this component
|
||||||
}
|
}
|
||||||
return compNumber;
|
return compNumber;
|
||||||
}
|
|
||||||
catch (InvalidKeyException e){
|
|
||||||
//
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//end#fragment CC
|
|
||||||
|
|||||||
@@ -38,34 +38,33 @@ public class DFS<V, E, I, R> {
|
|||||||
if (!isDone())
|
if (!isDone())
|
||||||
startVisit(v);
|
startVisit(v);
|
||||||
if (!isDone()) {
|
if (!isDone()) {
|
||||||
visit(v);
|
visit(v);
|
||||||
for (Edge<E> e: graph.incidentEdges(v)) {
|
for (Edge<E> e: graph.incidentEdges(v)) {
|
||||||
if (!isVisited(e)) {
|
if (!isVisited(e)) {
|
||||||
// found an unexplored edge, explore it
|
// found an unexplored edge, explore it
|
||||||
visit(e);
|
visit(e);
|
||||||
Vertex<V> w = graph.opposite(v, e);
|
Vertex<V> w = graph.opposite(v, e);
|
||||||
if (!isVisited(w)) {
|
if (!isVisited(w)) {
|
||||||
// w is unexplored, this is a discovery edge
|
// w is unexplored, this is a discovery edge
|
||||||
traverseDiscovery(e, v);
|
traverseDiscovery(e, v);
|
||||||
if (isDone()) break;
|
if (isDone()) break;
|
||||||
visitResult = dfsTraversal(w); // get result from DFS-tree child
|
visitResult = dfsTraversal(w); // get result from DFS-tree child
|
||||||
if (isDone()) break;
|
if (isDone()) break;
|
||||||
}
|
} else {
|
||||||
else {
|
// w is explored, this is a back edge
|
||||||
// w is explored, this is a back edge
|
traverseBack(e, v);
|
||||||
traverseBack(e, v);
|
if (isDone()) break;
|
||||||
if (isDone()) break;
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if(!isDone())
|
if(!isDone())
|
||||||
finishVisit(v);
|
finishVisit(v);
|
||||||
return result();
|
return result();
|
||||||
}
|
}
|
||||||
//end#fragment DFS2
|
|
||||||
|
|
||||||
//begin#fragment decorations
|
|
||||||
|
|
||||||
/** Mark a position (vertex or edge) as visited. */
|
/** Mark a position (vertex or edge) as visited. */
|
||||||
protected void visit(DecorablePosition<?> p) throws InvalidKeyException{
|
protected void visit(DecorablePosition<?> p) throws InvalidKeyException{
|
||||||
p.put(STATUS, VISITED);
|
p.put(STATUS, VISITED);
|
||||||
@@ -100,7 +99,5 @@ public class DFS<V, E, I, R> {
|
|||||||
protected R result() { return null; /* default value */ }
|
protected R result() { return null; /* default value */ }
|
||||||
/** Returns the final result of the DFS execute method. */
|
/** Returns the final result of the DFS execute method. */
|
||||||
protected R finalResult(R r) { return r; /* default value */ }
|
protected R finalResult(R r) { return r; /* default value */ }
|
||||||
//end#fragment auxiliary
|
|
||||||
//begin#fragment Tail
|
}
|
||||||
} // end of DFS class
|
|
||||||
//end#fragment Tail
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
package graph;
|
package graph;
|
||||||
//begin#fragment FindCycleDFS
|
|
||||||
|
|
||||||
import position.NodePositionList;
|
import position.NodePositionList;
|
||||||
import position.Position;
|
import position.Position;
|
||||||
@@ -9,25 +9,26 @@ import position.PositionList;
|
|||||||
//end#fragment FindCycleDFS
|
//end#fragment FindCycleDFS
|
||||||
/* @author Roberto Tamassia, Michael Goodrich, Eric Zamore
|
/* @author Roberto Tamassia, Michael Goodrich, Eric Zamore
|
||||||
*/
|
*/
|
||||||
//begin#fragment FindCycleDFS
|
|
||||||
public class FindCycleDFS<V, E>
|
public class FindCycleDFS<V, E>
|
||||||
extends DFS<V, E, Object, Iterable<Position>> {
|
extends DFS<V, E, Object, Iterable<Position>> {
|
||||||
protected PositionList<Position> cycle; // sequence of edges of the cycle
|
protected PositionList<Position> cycle; // sequence of edges of the cycle
|
||||||
protected boolean done;
|
protected boolean done;
|
||||||
protected Vertex<V> cycleStart;
|
protected Vertex<V> cycleStart;
|
||||||
//end#fragment FindCycleDFS
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes the DFS algorithm.
|
* Executes the DFS algorithm.
|
||||||
* @return collection containing the vertices and
|
* @return collection containing the vertices and
|
||||||
* edges of a cycle.
|
* edges of a cycle.
|
||||||
*/
|
*/
|
||||||
//begin#fragment FindCycleDFS
|
|
||||||
public void setup() {
|
public void setup() {
|
||||||
cycle = new NodePositionList<Position>();
|
cycle = new NodePositionList<Position>();
|
||||||
done = false;
|
done = false;
|
||||||
}
|
}
|
||||||
protected void startVisit(Vertex<V> v) { cycle.addLast(v); }
|
protected void startVisit(Vertex<V> v) { cycle.addLast(v); }
|
||||||
|
|
||||||
protected void finishVisit(Vertex<V> v) {
|
protected void finishVisit(Vertex<V> v) {
|
||||||
cycle.remove(cycle.last()); // remove v from cycle
|
cycle.remove(cycle.last()); // remove v from cycle
|
||||||
if (!cycle.isEmpty()) cycle.remove(cycle.last()); // remove edge into v from cycle
|
if (!cycle.isEmpty()) cycle.remove(cycle.last()); // remove edge into v from cycle
|
||||||
@@ -35,6 +36,7 @@ public class FindCycleDFS<V, E>
|
|||||||
protected void traverseDiscovery(Edge<E> e, Vertex<V> from) {
|
protected void traverseDiscovery(Edge<E> e, Vertex<V> from) {
|
||||||
cycle.addLast(e);
|
cycle.addLast(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void traverseBack(Edge<E> e, Vertex<V> from) {
|
protected void traverseBack(Edge<E> e, Vertex<V> from) {
|
||||||
cycle.addLast(e); // back edge e creates a cycle
|
cycle.addLast(e); // back edge e creates a cycle
|
||||||
cycleStart = graph.opposite(from, e);
|
cycleStart = graph.opposite(from, e);
|
||||||
@@ -42,6 +44,7 @@ public class FindCycleDFS<V, E>
|
|||||||
done = true;
|
done = true;
|
||||||
}
|
}
|
||||||
protected boolean isDone() { return done; }
|
protected boolean isDone() { return done; }
|
||||||
|
|
||||||
public Iterable<Position> finalResult(Iterable<Position> r) {
|
public Iterable<Position> finalResult(Iterable<Position> r) {
|
||||||
// remove the vertices and edges from start to cycleStart
|
// remove the vertices and edges from start to cycleStart
|
||||||
if (!cycle.isEmpty()) {
|
if (!cycle.isEmpty()) {
|
||||||
@@ -54,5 +57,5 @@ public class FindCycleDFS<V, E>
|
|||||||
return cycle; // list of the vertices and edges of the cycle
|
return cycle; // list of the vertices and edges of the cycle
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//end#fragment FindCycleDFS
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package graph;
|
package graph;
|
||||||
|
|
||||||
//begin#fragment FindPathDFS
|
|
||||||
|
|
||||||
import position.NodePositionList;
|
import position.NodePositionList;
|
||||||
import position.Position;
|
import position.Position;
|
||||||
@@ -15,6 +14,7 @@ public class FindPathDFS<V, E>
|
|||||||
extends DFS<V, E, Vertex<V>, Iterable<Position>> {
|
extends DFS<V, E, Vertex<V>, Iterable<Position>> {
|
||||||
protected PositionList<Position> path;
|
protected PositionList<Position> path;
|
||||||
protected boolean done;
|
protected boolean done;
|
||||||
|
|
||||||
/** Setup method to initialize the path. */
|
/** Setup method to initialize the path. */
|
||||||
public void setup() {
|
public void setup() {
|
||||||
path = new NodePositionList<Position>();
|
path = new NodePositionList<Position>();
|
||||||
@@ -40,4 +40,4 @@ public class FindPathDFS<V, E>
|
|||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//end#fragment FindPathDFS
|
|
||||||
|
|||||||
BIN
graph/gioel.rar
BIN
graph/gioel.rar
Binary file not shown.
Reference in New Issue
Block a user