// Textbook fragment 13.05 /** Execute a depth first search traversal on graph g, starting * from a start vertex s, passing in an information object (in) */ public R execute(Graph g, Vertex s, I in) { graph = g; start = s; info = in; for(Vertex v: graph.vertices()) unVisit(v); // mark vertices as unvisited for(Edge e: graph.edges()) unVisit(e); // mark edges as unvisited setup(); // perform any necessary setup prior to DFS traversal return finalResult(dfsTraversal(start)); } /** Recursive template method for a generic DFS traversal. */ protected R dfsTraversal(Vertex v) { initResult(); if (!isDone()) startVisit(v); if (!isDone()) { visit(v); for (Edge e: graph.incidentEdges(v)) { if (!isVisited(e)) { // found an unexplored edge, explore it visit(e); Vertex w = graph.opposite(v, e); if (!isVisited(w)) { // w is unexplored, this is a discovery edge traverseDiscovery(e, v); if (isDone()) break; visitResult = dfsTraversal(w); // get result from DFS-tree child if (isDone()) break; } else { // w is explored, this is a back edge traverseBack(e, v); if (isDone()) break; } } } } if(!isDone()) finishVisit(v); return result(); }