@Test public void equivalent_edgeAddOrdersDiffer() { GraphBuilder<Integer> builder = GraphBuilder.from(graph); MutableGraph<Integer> g1 = builder.build(); MutableGraph<Integer> g2 = builder.build(); // for g1, add 1->2 first, then 3->1 g1.putEdge(N1, N2); g1.putEdge(N3, N1); // for g2, add 3->1 first, then 1->2 g2.putEdge(N3, N1); g2.putEdge(N1, N2); assertThat(g1).isEqualTo(g2); }
@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)); }
/** Creates a mutable copy of {@code graph} with the same nodes and edges. */ public static <N> MutableGraph<N> copyOf(Graph<N> graph) { MutableGraph<N> copy = GraphBuilder.from(graph).expectedNodeCount(graph.nodes().size()).build(); for (N node : graph.nodes()) { copy.addNode(node); } for (EndpointPair<N> edge : graph.edges()) { copy.putEdge(edge.nodeU(), edge.nodeV()); } return copy; }
@Test public void hasCycle_oneEdge() { for (MutableGraph<Integer> graph : graphsToTest) { graph.putEdge(1, 2); } assertThat(hasCycle(directedGraph)).isFalse(); assertThat(hasCycle(undirectedGraph)).isFalse(); }
@Test public void hasCycle_selfLoopEdge() { for (MutableGraph<Integer> graph : graphsToTest) { graph.putEdge(1, 1); } assertThat(hasCycle(directedGraph)).isTrue(); assertThat(hasCycle(undirectedGraph)).isTrue(); }
@Test public void transpose_undirectedGraph() { MutableGraph<Integer> undirectedGraph = GraphBuilder.undirected().build(); undirectedGraph.putEdge(N1, N2); assertThat(transpose(undirectedGraph)).isSameAs(undirectedGraph); }
@Test @SuppressWarnings("CheckReturnValue") public void forTree_acceptsDirectedGraph() throws Exception { MutableGraph<String> graph = GraphBuilder.directed().build(); graph.putEdge("a", "b"); Traverser.forTree(graph); // Does not throw }
@CanIgnoreReturnValue protected boolean putEdge(EndpointPair<Integer> endpoints) { graph.addNode(endpoints.nodeU()); graph.addNode(endpoints.nodeV()); return graph.putEdge(endpoints); }
@Test public void forTree_withUndirectedGraph_throws() throws Exception { MutableGraph<String> graph = GraphBuilder.undirected().build(); graph.putEdge("a", "b"); try { Traverser.forTree(graph); fail("Expected exception"); } catch (IllegalArgumentException expected) { } }