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; }
/** * A wrapper for getting vertex Id's which supports multiple vertex owners. * * @param v Vertex which the caller intends to get its Id. * @return The Id. */ protected int getId(VertexType v) { if (subgraphIndex != 0) { return v.getSubgraphId(subgraphIndex); } return v.getId(); }
public Vector<VertexType> acyclicSP(BaseGraph<VertexType, EdgeType> g, VertexType v) throws InvalidVertexException { BaseGraph<VertexType, EdgeType> gcopy = g.copy(gc); final Integer dist[] = new Integer[g.getVerticesCount()]; Vector<VertexType> prev = new Vector<VertexType>(); Queue<VertexType> Q = new LinkedList<VertexType>(); HashMap<VertexType, VertexType> gcopy2g = new HashMap<VertexType, VertexType>(); HashMap<Integer, VertexType> t = new HashMap<Integer, VertexType>(); for (VertexType type : gcopy) t.put(type.getId(), type); for (VertexType type : g) gcopy2g.put(t.get(type.getId()), type); for (int i = 0; i < dist.length; i++) dist[i] = Integer.MAX_VALUE; dist[v.getId()] = 0; for (VertexType u : gcopy) { if (gcopy.getInDegree(u) == 0) { Q.add(u); } } while (!Q.isEmpty()) { VertexType u = Q.poll(); Iterator<EdgeType> iet = gcopy.edgeIterator(u); while (iet.hasNext()) { EdgeType e = iet.next(); if ((dist[u.getId()] + e.getWeight()) < dist[e.target.getId()]) { dist[e.target.getId()] = dist[u.getId()] + e.getWeight(); prev.add(e.target.getId(), u); dispatchEvent( new PreWorkEvent<VertexType, EdgeType>(gcopy2g.get(e.target), gcopy2g.get(u), gcopy)); // System.out.println(e.target.getId()); } if (gcopy.getInDegree((VertexType) e.target) == 1) Q.add((VertexType) e.target); } gcopy.removeVertex(u); } return prev; }