Пример #1
0
  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;
  }
Пример #2
0
  private void informVertices() {
    if (isSubgraph) superGraph.informVertices();

    for (VertexType v : this) {
      v.informNewSubgraph();
    }
  }
Пример #3
0
 /**
  * Get new id for a new subgraph;
  *
  * @param b
  */
 public int getNewSubgraphIndex() {
   if (isSubgraph) return superGraph.getNewSubgraphIndex();
   return lastSubgraphIndex + 1;
 }
Пример #4
0
 /**
  * Sets the graph as a subgraph.
  *
  * @param b
  */
 public void registerSubgraph(BaseGraph<VertexType, EdgeType> superGraph) {
   isSubgraph = true;
   this.superGraph = superGraph;
   superGraph.informVertices();
 }
Пример #5
0
  static <Vertex extends BaseVertex, Edge extends BaseEdge<Vertex>> Vector<Edge> convertToDag(
      BaseGraph<Vertex, Edge> graph) {
    System.out.println("start convertToDag");
    System.out.println(graph.getEdgesCount());
    // copied from Dag.finACycle
    byte[] mark = new byte[graph.getVerticesCount()];
    BaseVertex[] V = graph.getVertexArray();
    BaseVertex[] parent = new BaseVertex[mark.length];
    BaseVertex[] root = new BaseVertex[mark.length];
    Integer[] height = new Integer[mark.length];
    LibraryUtils.falsifyVertexMarks(graph);

    // start from one vertex and go to it's neighbors [BFS], continue until
    // you meet a MARKed vertex
    HashSet<Vertex> visitedVertices = new HashSet<Vertex>();
    LinkedList<Vertex> bfsStack = new LinkedList<Vertex>();
    Vertex cycleStart = null;
    Vertex cycleEnd = null;

    // visitedVertices.add(current);
    int scan = 0;
    int counter = 0;
    Vertex current;
    // perform a nonrecursive dfs
    for (scan = 0; scan < V.length; scan++) {
      Vertex u = (Vertex) V[scan];
      if (!u.getMark()) {
          /* Start a new search from u */
        bfsStack.addFirst(u);
        root[u.getId()] = u;
        height[u.getId()] = 0;
        while (!bfsStack.isEmpty()) {
          Vertex v = bfsStack.removeFirst();
          if (!v.getMark()) {
            v.setMark(true);
            counter++;
            for (Vertex w : graph.getNeighbors(v))
              if (!w.getMark()) {
                bfsStack.push(w);
                parent[w.getId()] = v;
                root[w.getId()] = u;
                height[w.getId()] = height[v.getId()] + 1;
              }
          }
        }
      }
    }

    Vector<Edge> ret = new Vector<Edge>();
    // try to find backedges
    for (Edge e : graph.edges()) {
      int src = e.source.getId();
      int trg = e.target.getId();
      if (root[src] == root[trg] && height[src] > height[trg]) {
        ret.add(e);
        System.out.println(ret.size());
        // cycleStart = e.target;
        // cycleEnd = e.source;
        //
        //
        // current = cycleEnd;
        // LinkedList<Vertex> ret = new LinkedList<Vertex>();
        // ret.addFirst(current);
        // while (current != null && current != cycleStart) {
        // current = (Vertex) parent[current.getId()];
        // ret.addFirst(current);
        // }
        // if (current != null)
        // return;

      }
    }
    return ret;
  }