private static MutableGraph<Integer> createGraph(EdgeType edgeType) { switch (edgeType) { case UNDIRECTED: return GraphBuilder.undirected().allowsSelfLoops(true).build(); case DIRECTED: return GraphBuilder.directed().allowsSelfLoops(true).build(); default: throw new IllegalStateException("Unexpected edge type: " + edgeType); } }
@Override public MutableGraph<Integer> createGraph() { return GraphBuilder.directed().allowsSelfLoops(true).build(); }
@Override public MutableGraph<Integer> createGraph() { return GraphBuilder.directed().allowsSelfLoops(false).build(); }
@Override public MutableGraph<Integer> createGraph() { return GraphBuilder.undirected().allowsSelfLoops(false).build(); }
/** 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; }
private static <N> ImmutableGraph<N> graphWithNode(N node) { MutableGraph<N> graph = GraphBuilder.directed().build(); graph.addNode(node); return ImmutableGraph.copyOf(graph); }
private static ImmutableGraph<Character> createSingleRootGraph() { MutableGraph<Character> graph = GraphBuilder.directed().build(); graph.addNode('a'); return ImmutableGraph.copyOf(graph); }
@Test public void transpose_undirectedGraph() { MutableGraph<Integer> undirectedGraph = GraphBuilder.undirected().build(); undirectedGraph.putEdge(N1, N2); assertThat(transpose(undirectedGraph)).isSameAs(undirectedGraph); }
@Test public void copyOfImmutableGraph_optimized() { Graph<String> graph1 = ImmutableGraph.copyOf(GraphBuilder.directed().<String>build()); Graph<String> graph2 = ImmutableGraph.copyOf(graph1); assertThat(graph2).isSameAs(graph1); }
@Test public void immutableGraph() { MutableGraph<String> mutableGraph = GraphBuilder.directed().build(); mutableGraph.addNode("A"); ImmutableGraph<String> immutableGraph = ImmutableGraph.copyOf(mutableGraph); assertThat(immutableGraph).isNotInstanceOf(MutableValueGraph.class); assertThat(immutableGraph).isEqualTo(mutableGraph); mutableGraph.addNode("B"); assertThat(immutableGraph).isNotEqualTo(mutableGraph); }
@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 }
private static MutableGraph<Integer> buildDirectedGraph() { MutableGraph<Integer> directedGraph = GraphBuilder.directed().allowsSelfLoops(true).build(); directedGraph.putEdge(N1, N1); directedGraph.putEdge(N1, N2); directedGraph.putEdge(N2, N1); return directedGraph; }
@Test public void equivalent_propertiesDiffer() { graph.putEdge(N1, N2); MutableGraph<Integer> g2 = GraphBuilder.from(graph).allowsSelfLoops(!graph.allowsSelfLoops()).build(); g2.putEdge(N1, N2); assertThat(graph).isEqualTo(g2); }
@Test public void nodeOrder_default() { MutableGraph<Integer> graph = GraphBuilder.directed().build(); addNodes(graph); assertThat(graph.nodeOrder()).isEqualTo(insertion()); assertThat(graph.nodes()).containsExactly(3, 1, 4).inOrder(); }
private static MutableGraph<Integer> buildUndirectedGraph() { MutableGraph<Integer> undirectedGraph = GraphBuilder.undirected().allowsSelfLoops(true).build(); undirectedGraph.putEdge(N1, N1); undirectedGraph.putEdge(N1, N2); undirectedGraph.putEdge(N2, N1); return undirectedGraph; }
@Test public void nodeOrder_sorted() { MutableGraph<Integer> graph = GraphBuilder.directed() .nodeOrder(ElementOrder.sorted(Ordering.<Integer>natural().reverse())) .build(); addNodes(graph); assertThat(graph.nodeOrder()) .isEqualTo(ElementOrder.sorted(Ordering.<Integer>natural().reverse())); assertThat(graph.nodes()).containsExactly(4, 3, 1).inOrder(); }
@Test public void nodeOrder_none() { MutableGraph<Integer> graph = GraphBuilder.directed().nodeOrder(unordered()).build(); assertThat(graph.nodeOrder()).isEqualTo(unordered()); }
@Test public void nodeOrder_natural() { MutableGraph<Integer> graph = GraphBuilder.directed().nodeOrder(ElementOrder.<Integer>natural()).build(); addNodes(graph); assertThat(graph.nodeOrder()).isEqualTo(ElementOrder.sorted(Ordering.<Integer>natural())); assertThat(graph.nodes()).containsExactly(1, 3, 4).inOrder(); }
@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) { } }
@Test public void nodeOrder_insertion() { MutableGraph<Integer> graph = GraphBuilder.directed().nodeOrder(insertion()).build(); addNodes(graph); assertThat(graph.nodeOrder()).isEqualTo(insertion()); assertThat(graph.nodes()).containsExactly(3, 1, 4).inOrder(); }