List of usage examples for com.google.common.graph ValueGraphBuilder directed
public static ValueGraphBuilder<Object, Object> directed()
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 . jav a2 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; }
From source file:no.ssb.vtl.script.operations.hierarchy.HierarchyOperation.java
/** * Create the directed acyclic graph from the dataset. * <p>/*from w w w.j ava2s . c o m*/ * The dataset is required to have the following columns: * from, to, sign. * * @throws IllegalArgumentException if a circular dependency is found. * @throws IllegalArgumentException if from and to are not of the same type. */ private static ValueGraph<VTLObject, Composition> convertToHierarchy(final Dataset hierarchy) { // Checks. final DataStructure structure = checkNotNull(hierarchy).getDataStructure(); Component fromComponent = checkNotNull(structure.get(FROM_COLUMN_NAME), COLUMN_NOT_FOUND, FROM_COLUMN_NAME); Component toComponent = checkNotNull(structure.get(TO_COLUMN_NAME), COLUMN_NOT_FOUND, TO_COLUMN_NAME); Component signComponent = checkNotNull(structure.get(SIGN_COLUMN_NAME), COLUMN_NOT_FOUND, SIGN_COLUMN_NAME); // The graph. MutableValueGraph<VTLObject, Composition> graph = ValueGraphBuilder.directed().allowsSelfLoops(false) .build(); // Add all the points. try (Stream<DataPoint> stream = hierarchy.getData()) { for (DataPoint point : (Iterable<? extends DataPoint>) stream::iterator) { Map<Component, VTLObject> asMap = structure.asMap(point); VTLObject from = asMap.get(fromComponent); VTLObject to = asMap.get(toComponent); VTLObject sign = asMap.get(signComponent); Composition composition = checkNotNull(COMPOSITION_MAP.get(sign.get()), UNKNOWN_SIGN_VALUE, sign); List<List<VTLObject>> paths = findPaths(graph, to, from); checkArgument(paths.isEmpty(), CIRCULAR_DEPENDENCY, from, composition, to, paths); graph.putEdgeValue(from, to, composition); } return graph; } }
From source file:org.jgrapht.graph.guava.ImmutableValueGraphAdapter.java
@SuppressWarnings("unchecked") private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { ois.defaultReadObject();/*from w w w .ja v a 2 s.c o m*/ GraphType type = (GraphType) ois.readObject(); if (type.isMixed() || type.isAllowingMultipleEdges()) { throw new IOException("Graph type not supported"); } MutableValueGraph<V, W> mutableValueGraph = (type.isDirected() ? ValueGraphBuilder.directed() : ValueGraphBuilder.undirected()).allowsSelfLoops(type.isAllowingSelfLoops()).build(); // read vertices int n = ois.readInt(); for (int i = 0; i < n; i++) { V v = (V) ois.readObject(); mutableValueGraph.addNode(v); } // read edges int m = ois.readInt(); for (int i = 0; i < m; i++) { V s = (V) ois.readObject(); V t = (V) ois.readObject(); W w = (W) ois.readObject(); mutableValueGraph.putEdgeValue(s, t, w); } // setup the immutable copy this.valueGraph = ImmutableValueGraph.copyOf(mutableValueGraph); }
From source file:org.jgrapht.graph.guava.MutableValueGraphAdapter.java
@SuppressWarnings("unchecked") private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { ois.defaultReadObject();/*from ww w . jav a2 s .com*/ GraphType type = (GraphType) ois.readObject(); if (type.isMixed() || type.isAllowingMultipleEdges()) { throw new IOException("Graph type not supported"); } valueGraph = (type.isDirected() ? ValueGraphBuilder.directed() : ValueGraphBuilder.undirected()) .allowsSelfLoops(type.isAllowingSelfLoops()).build(); // read vertices int n = ois.readInt(); for (int i = 0; i < n; i++) { V v = (V) ois.readObject(); valueGraph.addNode(v); } // read edges int m = ois.readInt(); for (int i = 0; i < m; i++) { V s = (V) ois.readObject(); V t = (V) ois.readObject(); W w = (W) ois.readObject(); valueGraph.putEdgeValue(s, t, w); } }