private boolean depthFirstSearchRecursive( VertexType vertex, VertexType fromVertex, PreWorkPostWorkHandler<VertexType> handler) throws InvalidVertexException { // TODO:Implementing non-recursive version // int vertexId = vertex.getId(); vertex.setMark(true); if (handler != null) if (handler.doPreWork(fromVertex, vertex)) return true; dispatchEvent(new PreWorkEvent<VertexType, EdgeType>(fromVertex, vertex, graph)); EventUtils.algorithmStep(this, "visit: " + vertex.getId()); VertexType lastInDepthVertex = vertex; for (VertexType i : graph) { if (graph.isEdge(vertex, i)) { if (!i.getMark()) { lastInDepthVertex = i; if (depthFirstSearchRecursive(i, vertex, handler)) return true; } } } dispatchEvent(new PostWorkEvent<VertexType, EdgeType>(lastInDepthVertex, vertex, graph)); EventUtils.algorithmStep(this, "leave: " + vertex.getId()); if (handler != null) if (handler.doPostWork(lastInDepthVertex, vertex)) return true; return false; }
/** * Runs Depth First Search (DFS) algorithm on the graph starting from vertex <I>vertexId</I>. A * reference to a PreWorkPostWorkHandler is supplied that contains implementation of pre-work and * post-work operations that depends on the application of DFS. * * @param vertex Index of the starting vertex of the traversal. * @param handler A reference to a PreWorkPostWorkHandler that contains implementation of pre-work * and post-work operations that depends on the application of DFS. * @param resetMarks If the search should reset vertex visit marks. * @return Whether the traversal has stopped at the middle by the handler. */ public boolean doSearch( VertexType vertex, PreWorkPostWorkHandler<VertexType> handler, boolean resetMarks) throws InvalidVertexException, InvalidGraphException { if (graph == null) throw new InvalidGraphException("Graph object is null."); if (resetMarks) for (VertexType v : graph) v.setMark(false); return depthFirstSearchRecursive(vertex, vertex, handler); }