// Textbook fragment 13.08 /** Class specializing DFS to find a path between a start vertex and a target * vertex. It assumes the target vertex is passed as the info object to the * execute method. It returns an iterable list of the vertices and edges * comprising the path from start to info. The returned path is empty if * info is unreachable from start. */ public class FindPathDFS extends DFS, Iterable> { protected PositionList path; protected boolean done; /** Setup method to initialize the path. */ public void setup() { path = new NodePositionList(); done = false; } protected void startVisit(Vertex v) { path.addLast(v); // add vertex v to path if (v == info) done = true; } protected void finishVisit(Vertex v) { path.remove(path.last()); // remove v from path if(!path.isEmpty()) // if v is not the start vertex path.remove(path.last()); // remove discovery edge into v from path } protected void traverseDiscovery(Edge e, Vertex from) { path.addLast(e); // add edge e to the path } protected boolean isDone() { return done; } public Iterable finalResult(Iterable r) { return path; } }