GraphUtil { public static <V, E> void removeIsolatedVertices(UndirectedGraph<V, E> graph) { Objects.requireNonNull(graph, "Graph is null."); TIntSet connectedVertices = new TIntHashSet(); for (int e : graph.getEdges()) { connectedVertices.add(graph.getEdgeVertex1(e)); connectedVertices.add(graph.getEdgeVertex2(e)); } for (int v : graph.getVertices()) { if (!connectedVertices.contains(v) && graph.getVertexObject(v) == null) { graph.removeVertex(v); } } } private  GraphUtil(); static ConnectedComponentsComputationResult computeConnectedComponents(TIntArrayList[] adjacencyList); static void removeIsolatedVertices(UndirectedGraph<V, E> graph);  }
@Test public void removeIsolatedVertices() throws Exception { UndirectedGraph<Object, Object> graph = new UndirectedGraphImpl<>(); graph.addVertex(); graph.addVertex(); graph.addVertex(); graph.addEdge(1, 2, null); graph.setVertexObject(0, new Object()); assertEquals(3, graph.getVertexCount()); GraphUtil.removeIsolatedVertices(graph); assertEquals(3, graph.getVertexCount()); graph.setVertexObject(0, null); GraphUtil.removeIsolatedVertices(graph); assertEquals(2, graph.getVertexCount()); }