List of usage examples for com.google.common.graph MutableValueGraph addNode
@CanIgnoreReturnValue
boolean addNode(N node);
From source file:io.acoia.graphs.MinimumSpanningTree.java
/** * An implementation of Kruskal's minimum spanning tree algorithm. * // www .j a v a 2s. c o m * Description from Wiki: Kruskal's algorithm is a minimum-spanning-tree algorithm which finds an * edge of the least possible weight that connects any two trees in the forest. It is a greedy * algorithm in graph theory as it finds a minimum spanning tree for a connected weighted graph * adding increasing cost arcs at each step. This means it finds a subset of the edges that forms * a tree that includes every vertex, where the total weight of all the edges in the tree is * minimized. If the graph is not connected, then it finds a minimum spanning forest (a minimum * spanning tree for each connected component). * * This implementation uses io.acoia.sets.UnionFind and expects a sorted list of edges, and thus * runs in O(E (V)) time. */ public static <E, N> ValueGraph<N, E> kruskals(ValueGraph<N, E> g, List<EndpointPair<N>> sortedEdges) { if (g.isDirected()) throw new IllegalArgumentException("Kruskal's does not support directed graphs"); // Set up a Graph to store the result. MutableValueGraph<N, E> tree = ValueGraphBuilder.undirected().allowsSelfLoops(false) .expectedNodeCount(g.nodes().size()).build(); for (N n : g.nodes()) { tree.addNode(n); } // Now we start kruskals.. most of the work is done by the UnionFind really... UnionFind<N> uf = new UnionFind<>(g.nodes()); for (EndpointPair<N> e : sortedEdges) { N nodeU = e.nodeU(); N nodeV = e.nodeV(); if (uf.findRoot(nodeU) != uf.findRoot(nodeV)) { tree.putEdgeValue(nodeU, nodeV, g.edgeValue(nodeU, nodeV)); uf.join(nodeU, nodeV); } } return tree; }
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.//from ww w . ja va 2s . c om * * @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:org.jgrapht.graph.guava.ImmutableValueGraphAdapter.java
@SuppressWarnings("unchecked") private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { ois.defaultReadObject();//from w w w . ja va 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); }