/**
   * Check if the graph is chordal.
   *
   * @return true if the graph is chordal, false otherwise.
   */
  public boolean isChordal() {
    if (chordalGraph == null) {
      computeMinimalTriangulation();
    }

    return (chordalGraph.edgeSet().size() == graph.edgeSet().size());
  }
  public void testUndirectedGraphGnp3() {
    GraphGenerator<Integer, DefaultEdge, Integer> gen =
        new GnpRandomBipartiteGraphGenerator<>(4, 4, 0.0, SEED);
    UndirectedGraph<Integer, DefaultEdge> g = new SimpleGraph<>(DefaultEdge.class);
    gen.generateGraph(g, new IntegerVertexFactory(), null);

    assertEquals(4 + 4, g.vertexSet().size());
    assertEquals(0, g.edgeSet().size());
  }
  public void testUndirectedGraphGnp1() {
    GraphGenerator<Integer, DefaultEdge, Integer> gen =
        new GnpRandomBipartiteGraphGenerator<>(4, 4, 0.5, SEED);
    UndirectedGraph<Integer, DefaultEdge> g = new SimpleGraph<>(DefaultEdge.class);
    gen.generateGraph(g, new IntegerVertexFactory(), null);

    int[][] edges = {{1, 6}, {1, 7}, {1, 8}, {2, 5}, {2, 7}, {3, 5}, {3, 8}, {4, 6}, {4, 7}};

    assertEquals(4 + 4, g.vertexSet().size());
    for (int[] e : edges) {
      assertTrue(g.containsEdge(e[0], e[1]));
    }
    assertEquals(edges.length, g.edgeSet().size());
  }
  /**
   * Create a copy of a graph for internal use.
   *
   * @param graph the graph to copy.
   * @return A copy of the graph projected to a SimpleGraph.
   */
  private static <V, E> UndirectedGraph<V, E> copyAsSimpleGraph(UndirectedGraph<V, E> graph) {
    UndirectedGraph<V, E> copy = new SimpleGraph<>(graph.getEdgeFactory());

    if (graph instanceof SimpleGraph) {
      Graphs.addGraph(copy, graph);
    } else {
      // project graph to SimpleGraph
      Graphs.addAllVertices(copy, graph.vertexSet());
      for (E e : graph.edgeSet()) {
        V v1 = graph.getEdgeSource(e);
        V v2 = graph.getEdgeTarget(e);
        if ((v1 != v2) && !copy.containsEdge(e)) {
          copy.addEdge(v1, v2);
        }
      }
    }
    return copy;
  }
示例#5
0
  /**
   * This method will return a list of vertices which represents the Eulerian circuit of the graph.
   *
   * @param g The graph to find an Eulerian circuit
   * @return null if no Eulerian circuit exists, or a list of vertices representing the Eulerian
   *     circuit if one does exist
   */
  public static <V, E> List<V> getEulerianCircuitVertices(UndirectedGraph<V, E> g) {
    // If the graph is not Eulerian then just return a null since no
    // Eulerian circuit exists
    if (!isEulerian(g)) {
      return null;
    }

    // The circuit will be represented by a linked list
    List<V> path = new LinkedList<V>();
    UndirectedGraph<V, E> sg = new UndirectedSubgraph<V, E>(g, null, null);
    path.add(sg.vertexSet().iterator().next());

    // Algorithm for finding an Eulerian circuit Basically this will find an
    // arbitrary circuit, then it will find another arbitrary circuit until
    // every edge has been traversed
    while (sg.edgeSet().size() > 0) {
      V v = null;

      // Find a vertex which has an edge that hasn't been traversed yet,
      // and keep its index position in the circuit list
      int index = 0;
      for (Iterator<V> iter = path.iterator(); iter.hasNext(); index++) {
        v = iter.next();
        if (sg.degreeOf(v) > 0) {
          break;
        }
      }

      // Finds an arbitrary circuit of the current vertex and
      // appends this into the circuit list
      while (sg.degreeOf(v) > 0) {
        for (Iterator<V> iter = sg.vertexSet().iterator(); iter.hasNext(); ) {
          V temp = iter.next();
          if (sg.containsEdge(v, temp)) {
            path.add(index, temp);
            sg.removeEdge(v, temp);
            v = temp;
            break;
          }
        }
      }
    }
    return path;
  }