/** * This test checks an implementation dependent feature. It tests that the method {@code addEdge} * will silently add the missing nodes to the graph, then add the edge connecting them. We are not * using the proxy methods here as we want to test {@code addEdge} when the end-points are not * elements of the graph. */ @Test public void addEdge_nodesNotInGraph() { graph.addNode(N1); assertTrue(graph.putEdge(N1, N5)); assertTrue(graph.putEdge(N4, N1)); assertTrue(graph.putEdge(N2, N3)); assertThat(graph.nodes()).containsExactly(N1, N5, N4, N2, N3).inOrder(); assertThat(graph.successors(N1)).containsExactly(N5); assertThat(graph.successors(N2)).containsExactly(N3); assertThat(graph.successors(N3)).isEmpty(); assertThat(graph.successors(N4)).containsExactly(N1); assertThat(graph.successors(N5)).isEmpty(); } }
Set<T> s = graphCopy.nodes().stream() .filter(node -> graphCopy.inDegree(node) == 0) .collect(Collectors.toSet()); while (!s.isEmpty()) for (T m : graphCopy.successors(n)) graphCopy.removeEdge(n, m); if (graphCopy.inDegree(m) == 0) if (!graphCopy.edges().isEmpty())
public void disconnect(Node fromNode, Node toNode) { Preconditions.checkNotNull(fromNode, "fromNode cannot be null!"); Preconditions.checkNotNull(toNode, "toNode cannot be null!"); if (!graph.hasEdgeConnecting(fromNode, toNode)) { logger.warn("Trying to disconnect two nodes that aren't connected, " + fromNode.getUri() + " and " + toNode.getUri()); } graph.removeEdge(fromNode, toNode); }
@After public void validateUndirectedEdges() { for (Integer node : graph.nodes()) { new EqualsTester() .addEqualityGroup( graph.predecessors(node), graph.successors(node), graph.adjacentNodes(node)) .testEquals(); } }
assertThat(graph.nodes()).isEmpty(); assertThat(graph.edges()).isEmpty(); AbstractGraphTest.validateGraph(graph); while (graph.nodes().size() < NUM_NODES) { graph.addNode(gen.nextInt(NODE_POOL_SIZE)); ArrayList<Integer> nodeList = new ArrayList<>(graph.nodes()); while (graph.edges().size() < NUM_EDGES) { graph.putEdge(getRandomElement(nodeList, gen), getRandomElement(nodeList, gen)); ArrayList<EndpointPair<Integer>> edgeList = new ArrayList<>(graph.edges()); assertThat(graph.nodes()).hasSize(NUM_NODES); assertThat(graph.edges()).hasSize(NUM_EDGES); AbstractGraphTest.validateGraph(graph); for (int i = 0; i < numEdgesToRemove; ++i) { EndpointPair<Integer> edge = edgeList.get(i); assertThat(graph.removeEdge(edge.nodeU(), edge.nodeV())).isTrue(); assertThat(graph.nodes()).hasSize(NUM_NODES); assertThat(graph.edges()).hasSize(NUM_EDGES - numEdgesToRemove); AbstractGraphTest.validateGraph(graph); int numNodesToRemove = gen.nextInt(NUM_NODES); for (int i = 0; i < numNodesToRemove; ++i) { assertThat(graph.removeNode(nodeList.get(i))).isTrue();
@SuppressFBWarnings("RV_RETURN_VALUE_IGNORED") public static <T> MutableGraph<T> clone(final MutableGraph<T> graph) { MutableGraph<T> clone = GraphBuilder.from(graph).build(); for (T token : graph.nodes()) { clone.addNode(token); } for (EndpointPair<T> ep : graph.edges()) { clone.putEdge(ep.source(), ep.target()); } return clone; }
@Test public void endpointPair_undirectedGraph() { MutableGraph<Integer> undirectedGraph = GraphBuilder.undirected().allowsSelfLoops(true).build(); undirectedGraph.addNode(N0); undirectedGraph.putEdge(N1, N2); undirectedGraph.putEdge(N2, N1); // does nothing undirectedGraph.putEdge(N1, N3); undirectedGraph.putEdge(N4, N4); containsExactlySanityCheck( undirectedGraph.edges(), EndpointPair.unordered(N1, N2), EndpointPair.unordered(N1, N3), EndpointPair.unordered(N4, N4)); }
@Test public void transpose_directedGraph() { MutableGraph<Integer> directedGraph = GraphBuilder.directed().allowsSelfLoops(true).build(); directedGraph.putEdge(N1, N3); directedGraph.putEdge(N3, N1); directedGraph.putEdge(N1, N2); directedGraph.putEdge(N1, N1); directedGraph.putEdge(N3, N4); MutableGraph<Integer> expectedTranspose = GraphBuilder.directed().allowsSelfLoops(true).build(); expectedTranspose.putEdge(N3, N1); expectedTranspose.putEdge(N1, N3); expectedTranspose.putEdge(N2, N1); expectedTranspose.putEdge(N1, N1); expectedTranspose.putEdge(N4, N3); Graph<Integer> transpose = transpose(directedGraph); assertThat(transpose).isEqualTo(expectedTranspose); assertThat(transpose(transpose)).isSameAs(directedGraph); AbstractGraphTest.validateGraph(transpose); for (Integer node : directedGraph.nodes()) { assertThat(directedGraph.inDegree(node)).isSameAs(transpose.outDegree(node)); assertThat(directedGraph.outDegree(node)).isSameAs(transpose.inDegree(node)); } assertThat(transpose.successors(N1)).doesNotContain(N2); directedGraph.putEdge(N2, N1); // View should be updated. assertThat(transpose.successors(N1)).contains(N2); AbstractGraphTest.validateGraph(transpose); }
@Test public void removeNode_nodeNotPresent() { addNode(N1); ImmutableSet<Integer> nodes = ImmutableSet.copyOf(graph.nodes()); assertThat(graph.removeNode(NODE_NOT_IN_GRAPH)).isFalse(); assertThat(graph.nodes()).containsExactlyElementsIn(nodes); }
@Test public void customComparable() { MutableGraph<ComparableSubClass> graph = GraphBuilder.undirected().nodeOrder(ElementOrder.<ComparableSubClass>natural()).build(); ComparableSubClass node2 = new ComparableSubClass(2); ComparableSubClass node4 = new ComparableSubClass(4); ComparableSubClass node6 = new ComparableSubClass(6); ComparableSubClass node8 = new ComparableSubClass(8); graph.addNode(node4); graph.addNode(node2); graph.addNode(node6); graph.addNode(node8); assertThat(graph.nodeOrder().comparator()).isEqualTo(Ordering.natural()); assertThat(graph.nodes()).containsExactly(node2, node4, node6, node8).inOrder(); }
@Test public void endpointPair_unmodifiableView() { MutableGraph<Integer> directedGraph = GraphBuilder.directed().build(); Set<EndpointPair<Integer>> edges = directedGraph.edges(); directedGraph.putEdge(N1, N2); containsExactlySanityCheck(edges, EndpointPair.ordered(N1, N2)); directedGraph.putEdge(N2, N1); containsExactlySanityCheck(edges, EndpointPair.ordered(N1, N2), EndpointPair.ordered(N2, N1)); directedGraph.removeEdge(N1, N2); directedGraph.removeEdge(N2, N1); containsExactlySanityCheck(edges); try { edges.add(EndpointPair.ordered(N1, N2)); fail("Set returned by edges() should be unmodifiable"); } catch (UnsupportedOperationException expected) { } }
@Test public void removeNode_antiparallelEdges() { putEdge(N1, N2); putEdge(N2, N1); assertThat(graph.removeNode(N1)).isTrue(); assertThat(graph.nodes()).containsExactly(N2); assertThat(graph.edges()).isEmpty(); assertThat(graph.removeNode(N2)).isTrue(); assertThat(graph.nodes()).isEmpty(); assertThat(graph.edges()).isEmpty(); }
@Test public void endpointPair_undirected_contains() { MutableGraph<Integer> undirectedGraph = GraphBuilder.undirected().allowsSelfLoops(true).build(); undirectedGraph.putEdge(N1, N1); undirectedGraph.putEdge(N1, N2); Set<EndpointPair<Integer>> edges = undirectedGraph.edges(); assertThat(edges).hasSize(2); assertThat(edges).contains(EndpointPair.unordered(N1, N1)); assertThat(edges).contains(EndpointPair.unordered(N1, N2)); assertThat(edges).contains(EndpointPair.unordered(N2, N1)); // equal to unordered(N1, N2) // ordered endpoints OK for undirected graph (because ordering is irrelevant) assertThat(edges).contains(EndpointPair.ordered(N1, N2)); assertThat(edges).doesNotContain(EndpointPair.unordered(N2, N2)); // edge not present assertThat(edges).doesNotContain(EndpointPair.unordered(N3, N4)); // nodes not in graph }
@Test public void removeEdge_existingSelfLoopEdge() { putEdge(N1, N1); assertThat(graph.removeEdge(N1, N1)).isTrue(); assertThat(graph.nodes()).containsExactly(N1); assertThat(graph.successors(N1)).isEmpty(); } }
@Test public void removeNode_existingNode() { putEdge(N1, N2); putEdge(N4, N1); assertThat(graph.removeNode(N1)).isTrue(); assertThat(graph.removeNode(N1)).isFalse(); assertThat(graph.nodes()).containsExactly(N2, N4); assertThat(graph.adjacentNodes(N2)).isEmpty(); assertThat(graph.adjacentNodes(N4)).isEmpty(); }
for (Node node : graph.nodes()) { int inDegree = graph.inDegree(node); inDegreeMap.put(node, inDegree); Node currentNode = nodesToExamine.remove(0); for (Node adjacentNode : graph.successors(currentNode)) { int updatedInDegree = inDegreeMap.get(adjacentNode) - 1; inDegreeMap.put(adjacentNode, updatedInDegree);
@Test public void removeEdge_existingSelfLoopEdge() { putEdge(N1, N1); assertThat(graph.removeEdge(N1, N1)).isTrue(); assertThat(graph.nodes()).containsExactly(N1); assertThat(graph.adjacentNodes(N1)).isEmpty(); } }