List of usage examples for com.google.common.graph ValueGraphBuilder build
public <N1 extends N, V1 extends V> MutableValueGraph<N1, V1> build()
From source file:edu.uci.ics.jung.algorithms.transformation.NodePartitionCollapser.java
/** * Creates a new graph whose nodes correspond to the partitions of the supplied graph. Two nodes u * and v in the collapsed graph will be connected if there is an edge between any of the nodes in * u and any of the nodes in v, and u and v are distinct. The value of the edge represents the * number of such edges.// w w w .java 2 s.c o m * * @param partitioning a node partition of a graph * @return the collapsed graph */ public static <N> ValueGraph<Set<N>, Integer> collapseNodePartitions(NodePartition<N> partitioning) { Graph<N> original = partitioning.getGraph(); ValueGraphBuilder<Object, Object> builder = original.isDirected() ? ValueGraphBuilder.directed() : ValueGraphBuilder.undirected(); MutableValueGraph<Set<N>, Integer> collapsed = builder.build(); // create nodes in new graph corresponding to equivalence sets in the original graph for (Set<N> set : partitioning.getNodePartitions()) { collapsed.addNode(set); } // for each pair of endpoints in the original graph, connect the corresponding nodes // (representing partitions) in the collapsed graph if the partitions are different Map<N, Set<N>> nodeToPartition = partitioning.getNodeToPartitionMap(); for (EndpointPair<N> endpoints : original.edges()) { N nodeU = endpoints.nodeU(); N nodeV = endpoints.nodeV(); Set<N> partitionU = nodeToPartition.get(nodeU); Set<N> partitionV = nodeToPartition.get(nodeV); if (nodeU.equals(nodeV) || partitionU.equals(partitionV)) { // we only connect partitions if the partitions are different; // check the nodes first as an optimization continue; } int edgeCount = collapsed.edgeValueOrDefault(partitionU, partitionV, 0); collapsed.putEdgeValue(partitionU, partitionV, edgeCount + 1); } return collapsed; }