/** * Checks whether there exists unvisited vertex. * * @return true if there exists unvisited vertex. */ @Override public boolean hasNext() { if (current != null) { return true; } current = advance(); if (current != null && nListeners != 0) { fireVertexTraversed(createVertexTraversalEvent(current)); } return current != null; }
/** * Returns the next vertex in the ordering. * * @return the next vertex in the ordering. */ @Override public V next() { if (!hasNext()) { throw new NoSuchElementException(); } V result = current; current = null; if (nListeners != 0) { fireVertexFinished(createVertexTraversalEvent(result)); } return result; }
/** * Retrieves a vertex from the {@code buckets} with the maximum cardinality and returns it. * * @return vertex retrieved from {@code buckets}. */ private V advance() { if (remainingVertices > 0) { Set<V> bucket = buckets.get(maxCardinality); V vertex = bucket.iterator().next(); removeFromBucket(vertex); if (bucket.isEmpty()) { buckets.set(maxCardinality, null); do { --maxCardinality; } while (maxCardinality >= 0 && buckets.get(maxCardinality) == null); } updateNeighbours(vertex); --remainingVertices; return vertex; } else { return null; } }
/** * Increments the cardinalities of the neighbours of the {@code vertex} by 1. If the maximum * cardinality increases, increments {@code maxCardinality} by 1. * * @param vertex the vertex whose neighbours are to be updated. */ private void updateNeighbours(V vertex) { Set<V> processed = new HashSet<>(); for (E edge : graph.edgesOf(vertex)) { V opposite = Graphs.getOppositeVertex(graph, edge, vertex); if (cardinalityMap.containsKey(opposite) && !processed.contains(opposite)) { processed.add(opposite); addToBucket(opposite, removeFromBucket(opposite) + 1); } } if (maxCardinality < graph.vertexSet().size() && maxCardinality >= 0 && buckets.get(maxCardinality + 1) != null) { ++maxCardinality; } } }
/** * Creates a chordality inspector for {@code graph}, which uses an iterator defined by the * second parameter as an internal iterator. * * @param graph the graph for which a chordality inspector is to be created. * @param iterationOrder the constant, which defines iterator to be used by this * {@code ChordalityInspector}. */ public ChordalityInspector(Graph<V, E> graph, IterationOrder iterationOrder) { Objects.requireNonNull(graph); if (graph.getType().isDirected()) { this.graph = new AsUndirectedGraph<>(graph); } else { this.graph = graph; } this.iterationOrder = iterationOrder; this.hole = null; if (iterationOrder == IterationOrder.MCS) { this.orderIterator = new MaximumCardinalityIterator<>(graph); } else { this.orderIterator = new LexBreadthFirstIterator<>(graph); } }