private static MutableValueGraph<Integer, String> buildDirectedValueGraph() { MutableValueGraph<Integer, String> directedGraph = ValueGraphBuilder.directed().allowsSelfLoops(true).build(); directedGraph.putEdgeValue(N1, N1, E11); directedGraph.putEdgeValue(N1, N2, E12); directedGraph.putEdgeValue(N2, N1, E21); return directedGraph; }
/** * Returns the subgraph of {@code graph} induced by {@code nodes}. This subgraph is a new graph * that contains all of the nodes in {@code nodes}, and all of the {@link Graph#edges() edges} * (and associated edge values) from {@code graph} for which both nodes are contained by {@code * nodes}. * * @throws IllegalArgumentException if any element in {@code nodes} is not a node in the graph */ public static <N, V> MutableValueGraph<N, V> inducedSubgraph( ValueGraph<N, V> graph, Iterable<? extends N> nodes) { MutableValueGraph<N, V> subgraph = (nodes instanceof Collection) ? ValueGraphBuilder.from(graph).expectedNodeCount(((Collection) nodes).size()).build() : ValueGraphBuilder.from(graph).build(); for (N node : nodes) { subgraph.addNode(node); } for (N node : subgraph.nodes()) { for (N successorNode : graph.successors(node)) { if (subgraph.nodes().contains(successorNode)) { subgraph.putEdgeValue( node, successorNode, graph.edgeValueOrDefault(node, successorNode, null)); } } } return subgraph; }
/** * Returns a {@link ValueGraphBuilder} initialized with all properties queryable from {@code * graph}. * * <p>The "queryable" properties are those that are exposed through the {@link ValueGraph} * interface, such as {@link ValueGraph#isDirected()}. Other properties, such as {@link * #expectedNodeCount(int)}, are not set in the new builder. */ public static <N, V> ValueGraphBuilder<N, V> from(ValueGraph<N, V> graph) { return new ValueGraphBuilder<N, V>(graph.isDirected()) .allowsSelfLoops(graph.allowsSelfLoops()) .nodeOrder(graph.nodeOrder()); }
@Test public void equivalence_considersEdgeValue() { graph = ValueGraphBuilder.undirected().build(); graph.putEdgeValue(1, 2, "valueA"); MutableValueGraph<Integer, String> otherGraph = ValueGraphBuilder.undirected().build(); otherGraph.putEdgeValue(1, 2, "valueA"); assertThat(graph).isEqualTo(otherGraph); otherGraph.putEdgeValue(1, 2, "valueB"); assertThat(graph).isNotEqualTo(otherGraph); // values differ } }
private static MutableValueGraph<Integer, String> buildUndirectedValueGraph() { MutableValueGraph<Integer, String> undirectedGraph = ValueGraphBuilder.undirected().allowsSelfLoops(true).build(); undirectedGraph.putEdgeValue(N1, N1, E11); undirectedGraph.putEdgeValue(N1, N2, E12); undirectedGraph.putEdgeValue(N2, N1, E21); // overwrites E12 return undirectedGraph; }
g.nodes().containsAll(nodes), "Input graph must contain all specified nodes"); ValueGraphBuilder<Object, Object> builder = g.isDirected() ? ValueGraphBuilder.directed() : ValueGraphBuilder.undirected(); MutableValueGraph<N, Set<N>> newGraph = builder.expectedNodeCount(nodes.size()).nodeOrder(g.nodeOrder()).build();
@SuppressWarnings("unchecked") private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { ois.defaultReadObject(); 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); } }
Graph<N> original = partitioning.getGraph(); ValueGraphBuilder<Object, Object> builder = original.isDirected() ? ValueGraphBuilder.directed() : ValueGraphBuilder.undirected(); MutableValueGraph<Set<N>, Integer> collapsed = builder.build();
/** Returns a {@link ValueGraphBuilder} for building undirected graphs. */ public static ValueGraphBuilder<Object, Object> undirected() { return new ValueGraphBuilder<>(false); }
/** Specifies the order of iteration for the elements of {@link Graph#nodes()}. */ public <N1 extends N> ValueGraphBuilder<N1, V> nodeOrder(ElementOrder<N1> nodeOrder) { ValueGraphBuilder<N1, V> newBuilder = cast(); newBuilder.nodeOrder = checkNotNull(nodeOrder); return newBuilder; }
private ImmutableValueGraph(ValueGraph<N, V> graph) { super(ValueGraphBuilder.from(graph), getNodeConnections(graph), graph.edges().size()); }
@Test public void putEdgeValue_undirected_orderMismatch() { graph = ValueGraphBuilder.undirected().build(); assertThat(graph.putEdgeValue(EndpointPair.ordered(1, 2), "irrelevant")).isNull(); }
@Test public void undirectedGraph() { graph = ValueGraphBuilder.undirected().allowsSelfLoops(true).build(); graph.putEdgeValue(1, 2, "valueA"); graph.putEdgeValue(2, 1, "valueB"); // overwrites valueA in undirected case graph.putEdgeValue(2, 3, "valueC"); graph.putEdgeValue(4, 4, "valueD"); assertThat(graph.edgeValueOrDefault(1, 2, null)).isEqualTo("valueB"); assertThat(graph.edgeValueOrDefault(2, 1, null)).isEqualTo("valueB"); assertThat(graph.edgeValueOrDefault(2, 3, null)).isEqualTo("valueC"); assertThat(graph.edgeValueOrDefault(4, 4, null)).isEqualTo("valueD"); assertThat(graph.edgeValueOrDefault(1, 2, DEFAULT)).isEqualTo("valueB"); assertThat(graph.edgeValueOrDefault(2, 1, DEFAULT)).isEqualTo("valueB"); assertThat(graph.edgeValueOrDefault(2, 3, DEFAULT)).isEqualTo("valueC"); assertThat(graph.edgeValueOrDefault(4, 4, DEFAULT)).isEqualTo("valueD"); String toString = graph.toString(); assertThat(toString).doesNotContain("valueA"); assertThat(toString).contains("valueB"); assertThat(toString).contains("valueC"); assertThat(toString).contains("valueD"); }
@SuppressWarnings("unchecked") private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { ois.defaultReadObject(); 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); }
/** Returns a {@link ValueGraphBuilder} for building directed graphs. */ public static ValueGraphBuilder<Object, Object> directed() { return new ValueGraphBuilder<>(true); }
/** Specifies the order of iteration for the elements of {@link Graph#nodes()}. */ public <N1 extends N> ValueGraphBuilder<N1, V> nodeOrder(ElementOrder<N1> nodeOrder) { ValueGraphBuilder<N1, V> newBuilder = cast(); newBuilder.nodeOrder = checkNotNull(nodeOrder); return newBuilder; }
private ImmutableValueGraph(ValueGraph<N, V> graph) { super(ValueGraphBuilder.from(graph), getNodeConnections(graph), graph.edges().size()); }
@Test public void inducedSubgraph_valueGraph() { Set<Integer> nodeSubset = ImmutableSet.of(N1, N2, N4); MutableValueGraph<Integer, String> directedGraph = ValueGraphBuilder.directed().allowsSelfLoops(true).build(); directedGraph.putEdgeValue(N1, N2, E12); directedGraph.putEdgeValue(N2, N1, E21); directedGraph.putEdgeValue(N1, N3, E13); // only incident to one node in nodeSubset directedGraph.putEdgeValue(N4, N4, E44); directedGraph.putEdgeValue(5, 6, "5-6"); // not incident to any node in nodeSubset MutableValueGraph<Integer, String> expectedSubgraph = ValueGraphBuilder.directed().allowsSelfLoops(true).build(); expectedSubgraph.putEdgeValue(N1, N2, E12); expectedSubgraph.putEdgeValue(N2, N1, E21); expectedSubgraph.putEdgeValue(N4, N4, E44); assertThat(inducedSubgraph(directedGraph, nodeSubset)).isEqualTo(expectedSubgraph); }