/** * Extracts the weak components from a graph. * @param graph the graph whose weak components are to be extracted * @return the list of weak components */ public Set<Set<V>> transform(Graph<V,E> graph) { Set<Set<V>> clusterSet = new HashSet<Set<V>>(); HashSet<V> unvisitedVertices = new HashSet<V>(graph.getVertices()); while (!unvisitedVertices.isEmpty()) { Set<V> cluster = new HashSet<V>(); V root = unvisitedVertices.iterator().next(); unvisitedVertices.remove(root); cluster.add(root); Buffer<V> queue = new UnboundedFifoBuffer<V>(); queue.add(root); while (!queue.isEmpty()) { V currentVertex = queue.remove(); Collection<V> neighbors = graph.getNeighbors(currentVertex); for(V neighbor : neighbors) { if (unvisitedVertices.contains(neighbor)) { queue.add(neighbor); unvisitedVertices.remove(neighbor); cluster.add(neighbor); } } } clusterSet.add(cluster); } return clusterSet; }