Code Fragment: FindPathDFS



template <typename Graph>
class FindPathDFS : public DFS<Graph> {			// find a path by DFS
protected: 						// local types
  typedef typename Graph::Vertex	    Vertex;
  typedef typename Graph::VertexSequence    VertexSequence;
private: 						// local data
  Vertex	    target;				// the target vertex
  bool		    pathDetected;			// is target found?
  VertexSequence    path;				// path storage
protected: 						// overridden functions
  virtual void startVisit(const Vertex& v) {		// visit vertex
    path.insertLast(v); 				// insert into path
    if (v == target)					// target vertex seen?
      pathDetected = true;				// path is detected
  }
  virtual void finishVisit(const Vertex& v) {		// done with vertex
    if (!pathDetected)
      path.remove(path.last());				// remove if not on path
  }
  virtual bool isDone() const 				// are we done yet?
    { return pathDetected; }
public:
  FindPathDFS(const Graph& g)				// constructor
    : DFS<Graph>(g), path() { }
							// find path from s to t
  VertexSequence run(const Vertex& s, const Vertex& t) {
    initialize();					// initialize DFS
    target = t;						// t is the target
    path = VertexSequence();				// reset path sequence
    pathDetected = false;
    dfsTraversal(s);					// do the search
    return path;					// return the path
  }
};