Java Code Examples for org.jgrapht.DirectedGraph#containsVertex()

The following examples show how to use org.jgrapht.DirectedGraph#containsVertex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FindAttributesWithLabeledNulls.java    From Llunatic with GNU General Public License v3.0 5 votes vote down vote up
private boolean isReachable(AttributeRef attribute, Set<AttributeRef> initialAttributes, DirectedGraph<AttributeRef, ExtendedEdge> dependencyGraph) {
    for (AttributeRef initialAttribute : initialAttributes) {
        if (logger.isTraceEnabled()) logger.trace("Checking reachability of " + attribute + " from " + initialAttribute);
        if (!dependencyGraph.containsVertex(initialAttribute)) {
            continue;
        }
        List path = DijkstraShortestPath.findPathBetween(dependencyGraph, initialAttribute, attribute);
        if (path != null) {
            if (logger.isTraceEnabled()) logger.trace("Found!");
            return true;
        }
    }
    return false;
}
 
Example 2
Source File: GraphUtils.java    From Nicobar with Apache License 2.0 5 votes vote down vote up
/**
 * replace the vertices in the graph with an alternate set of vertices.
 * @param graph graph to be mutated
 * @param alternates alternate vertices to insert into the graph. map of vertices to their direct dependents.
 */
public static <V> void swapVertices(DirectedGraph<V, DefaultEdge> graph, Map<V, Set<V>> alternates) {
    Objects.requireNonNull(graph,"graph");
    Objects.requireNonNull(alternates, "alternates");
    // add all of the new vertices to prep for linking
    addAllVertices(graph, alternates.keySet());

    for (Entry<V, Set<V>> entry : alternates.entrySet()) {
        V alternateVertex = entry.getKey();
        Set<V> dependencies = entry.getValue();
        // make sure we can satisfy all dependencies before continuing
        // TODO: this should probably done outside so it can be handled better.
        if (!graph.vertexSet().containsAll(dependencies)) {
            continue;
        }

        // figure out which vertices depend on the incumbent vertex
        Set<V> dependents = Collections.emptySet();
        if (graph.containsVertex(alternateVertex)) {
            dependents = getIncomingVertices(graph, alternateVertex);
            graph.removeVertex(alternateVertex);
        }
        // (re)insert the vertex and re-link the dependents
        graph.addVertex(alternateVertex);
        addIncomingEdges(graph, alternateVertex, dependents);

        // create the dependencies
        addOutgoingEdges(graph, alternateVertex, dependencies);
    }
}
 
Example 3
Source File: GraphUtils.java    From Nicobar with Apache License 2.0 5 votes vote down vote up
/**
 * Add dependencies to the given source vertex. Whether duplicates are created is dependent
 * on the underlying {@link DirectedGraph} implementation.
 * @param graph graph to be mutated
 * @param source source vertex of the new edges
 * @param targets target vertices for the new edges
 */
public static <V> void addOutgoingEdges(DirectedGraph<V, DefaultEdge> graph, V source, Set<V> targets) {
    if (!graph.containsVertex(source)) {
        graph.addVertex(source);
    }
    for (V target : targets) {
        if (!graph.containsVertex(target)) {
            graph.addVertex(target);
        }
        graph.addEdge(source, target);
    }
}
 
Example 4
Source File: GraphUtils.java    From Nicobar with Apache License 2.0 5 votes vote down vote up
/**
 * Add dependents on the give target vertex. Whether duplicates are created is dependent
 * on the underlying {@link DirectedGraph} implementation.
 * @param graph graph to be mutated
 * @param target target vertex for the new edges
 * @param sources source vertices for the new edges
 */
public static <N> void addIncomingEdges(DirectedGraph<N, DefaultEdge> graph, N target, Set<N> sources) {
    if (!graph.containsVertex(target)) {
        graph.addVertex(target);
    }
    for (N source : sources) {
        if (!graph.containsVertex(source)) {
            graph.addVertex(source);
        }
        graph.addEdge(source, target);
    }
}
 
Example 5
Source File: GraphUtils.java    From Nicobar with Apache License 2.0 5 votes vote down vote up
/**
 * Removes vertices from graph
 * @param graph raph to mutate
 * @param vertices vertices to remove
 */
public static <V> void removeVertices(DirectedGraph<V, DefaultEdge> graph, Set<V> vertices) {
    for (V vertex : vertices) {
        if (graph.containsVertex(vertex)) {
            graph.removeVertex(vertex);
        }
    }
}
 
Example 6
Source File: GraphSerializationTest.java    From dkpro-jwpl with Apache License 2.0 4 votes vote down vote up
/**
   * Compares the given graph with the expected graph. Returns true only if both<br>
   * graphs are identical.
   * @param graph
   */
  private void testGraph(DirectedGraph<Integer,DefaultEdge> graph){
      //make sure all vertices are there
      for(int i=1;i<16;i++){
          if(!graph.containsVertex(i)) {
              fail("Graph does not contain vertex " + i);
          }
      }
      if(!graph.containsVertex(30)) {
          fail("Graph does not contain vertex " + 200);
      }
      if(!graph.containsVertex(200)) {
          fail("Graph does not contain vertex " + 200);
      }

      //make sure there are no supplemental vertices
      assertEquals(17, graph.vertexSet().size());

      //make sure all edges are there
      if(!graph.containsEdge(1,200)) {
	fail("Graph does not contain edge");
}
      if(!graph.containsEdge(1,2)) {
	fail("Graph does not contain edge");
}
      if(!graph.containsEdge(1,4)) {
	fail("Graph does not contain edge");
}
      if(!graph.containsEdge(1,3)) {
	fail("Graph does not contain edge");
}
      if(!graph.containsEdge(1,5)) {
	fail("Graph does not contain edge");
}
      if(!graph.containsEdge(3,6)) {
	fail("Graph does not contain edge");
}
      if(!graph.containsEdge(4,9)) {
	fail("Graph does not contain edge");
}
      if(!graph.containsEdge(5,8)) {
	fail("Graph does not contain edge");
}
      if(!graph.containsEdge(6,9)) {
	fail("Graph does not contain edge");
}
      if(!graph.containsEdge(6,8)) {
	fail("Graph does not contain edge");
}
      if(!graph.containsEdge(6,7)) {
	fail("Graph does not contain edge");
}
      if(!graph.containsEdge(7,11)) {
	fail("Graph does not contain edge");
}
      if(!graph.containsEdge(7,10)) {
	fail("Graph does not contain edge");
}
      if(!graph.containsEdge(8,15)) {
	fail("Graph does not contain edge");
}
      if(!graph.containsEdge(8,13)) {
	fail("Graph does not contain edge");
}
      if(!graph.containsEdge(8,14)) {
	fail("Graph does not contain edge");
}
      if(!graph.containsEdge(8,12)) {
	fail("Graph does not contain edge");
}

      //make sure there no supplemental edges
      assertEquals(17, graph.edgeSet().size());
  }