Example #1
0
  public static <VertexType extends BaseVertex, EdgeType extends BaseEdge<VertexType>>
      BaseGraph<VertexType, EdgeType> getEdgeInducedSubgraph(
          BaseGraph<VertexType, EdgeType> graph, AbstractList<EdgeType> inducedEdges) {
    BaseGraph<VertexType, EdgeType> newGraph = graph.createEmptyGraph();
    newGraph.registerSubgraph(graph);
    newGraph.setSubGraphIndex(graph.getNewSubgraphIndex());

    TreeSet<VertexType> vertices =
        new TreeSet<VertexType>(
            new Comparator<VertexType>() {
              public int compare(VertexType o1, VertexType o2) {
                if (o1.getId() < o2.getId()) return -1;

                if (o1.getId() == o2.getId()) return 0;

                return 1;
              }
            });

    // Removing duplicate vertices by adding them to a TreeSet
    for (EdgeType e : inducedEdges) {
      graph.checkVertex(e.source);
      graph.checkVertex(e.target);
      vertices.add(e.source);
      vertices.add(e.target);
    }

    for (VertexType v : vertices) newGraph.insertVertex(v);

    for (EdgeType e : inducedEdges) newGraph.insertEdge(e);

    return newGraph;
  }
Example #2
0
  public static <VertexType extends BaseVertex, EdgeType extends BaseEdge<VertexType>>
      BaseGraph<VertexType, EdgeType> getVertexInducedSubgraph(
          BaseGraph<VertexType, EdgeType> graph, AbstractList<VertexType> inducedVertices) {
    BaseGraph<VertexType, EdgeType> newGraph = graph.createEmptyGraph();
    newGraph.registerSubgraph(graph);
    newGraph.setSubGraphIndex(graph.getNewSubgraphIndex());

    for (VertexType v : inducedVertices) {
      graph.checkVertex(v);
      newGraph.insertVertex(v);
    }

    for (int i = 0; i < inducedVertices.size(); ++i) {
      for (int j = i + 1; j < inducedVertices.size(); ++j) {
        AbstractList<EdgeType> edges =
            graph.getEdges(inducedVertices.get(i), inducedVertices.get(j));

        for (EdgeType edge : edges) newGraph.insertEdge(edge);
      }
    }

    return newGraph;
  }
  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;
  }