/** * Returns an immutable undirected graph, containing only the specified node. * * @param node The single node in the returned graph * @param <N> The class of the nodes * @return an immutable undirected graph with a single node */ public static <N> ImmutableGraph<N> singletonUndirectedGraph(N node) { final MutableGraph<N> graph = GraphBuilder.undirected().build(); graph.addNode(node); return ImmutableGraph.copyOf(graph); }
@Test public void undirectedGraph() { testGraphMutation(GraphBuilder.undirected()); }
@Override public MutableGraph<Integer> createGraph() { return GraphBuilder.undirected().allowsSelfLoops(false).build(); }
@Override public MutableGraph<Integer> createGraph() { return GraphBuilder.undirected().allowsSelfLoops(true).build(); }
@Test public void transpose_undirectedGraph() { MutableGraph<Integer> undirectedGraph = GraphBuilder.undirected().build(); undirectedGraph.putEdge(N1, N2); assertThat(transpose(undirectedGraph)).isSameAs(undirectedGraph); }
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); } }
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 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 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 }
@Before public void init() { MutableGraph<Integer> mutableDirectedGraph = GraphBuilder.directed().allowsSelfLoops(true).build(); MutableGraph<Integer> mutableUndirectedGraph = GraphBuilder.undirected().allowsSelfLoops(true).build(); graphsToTest = ImmutableList.of(mutableDirectedGraph, mutableUndirectedGraph); directedGraph = mutableDirectedGraph; undirectedGraph = mutableUndirectedGraph; MutableNetwork<Integer, String> mutableDirectedNetwork = NetworkBuilder.directed().allowsParallelEdges(true).allowsSelfLoops(true).build(); MutableNetwork<Integer, String> mutableUndirectedNetwork = NetworkBuilder.undirected().allowsParallelEdges(true).allowsSelfLoops(true).build(); networksToTest = ImmutableList.of(mutableDirectedNetwork, mutableUndirectedNetwork); directedNetwork = mutableDirectedNetwork; undirectedNetwork = mutableUndirectedNetwork; }
@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 transitiveClosure_undirectedGraph() { MutableGraph<Integer> undirectedGraph = GraphBuilder.undirected().allowsSelfLoops(false).build(); undirectedGraph.putEdge(N1, N2); undirectedGraph.putEdge(N1, N3); undirectedGraph.putEdge(N2, N3); undirectedGraph.addNode(N4); MutableGraph<Integer> expectedClosure = GraphBuilder.undirected().allowsSelfLoops(true).build(); expectedClosure.putEdge(N1, N1); expectedClosure.putEdge(N1, N2); expectedClosure.putEdge(N1, N3); expectedClosure.putEdge(N2, N2); expectedClosure.putEdge(N2, N3); expectedClosure.putEdge(N3, N3); expectedClosure.putEdge(N4, N4); checkTransitiveClosure(undirectedGraph, expectedClosure); }
@Test public void transitiveClosure_undirectedPathGraph() { MutableGraph<Integer> undirectedGraph = GraphBuilder.undirected().allowsSelfLoops(false).build(); undirectedGraph.putEdge(N1, N2); undirectedGraph.putEdge(N2, N3); undirectedGraph.putEdge(N3, N4); MutableGraph<Integer> expectedClosure = GraphBuilder.undirected().allowsSelfLoops(true).build(); expectedClosure.putEdge(N1, N1); expectedClosure.putEdge(N1, N2); expectedClosure.putEdge(N1, N3); expectedClosure.putEdge(N1, N4); expectedClosure.putEdge(N2, N2); expectedClosure.putEdge(N2, N3); expectedClosure.putEdge(N2, N4); expectedClosure.putEdge(N3, N3); expectedClosure.putEdge(N3, N4); expectedClosure.putEdge(N4, N4); checkTransitiveClosure(undirectedGraph, expectedClosure); }
@Test public void transitiveClosure_undirectedCycleGraph() { MutableGraph<Integer> undirectedGraph = GraphBuilder.undirected().allowsSelfLoops(false).build(); undirectedGraph.putEdge(N1, N2); undirectedGraph.putEdge(N2, N3); undirectedGraph.putEdge(N3, N4); undirectedGraph.putEdge(N4, N1); MutableGraph<Integer> expectedClosure = GraphBuilder.undirected().allowsSelfLoops(true).build(); expectedClosure.putEdge(N1, N1); expectedClosure.putEdge(N1, N2); expectedClosure.putEdge(N1, N3); expectedClosure.putEdge(N1, N4); expectedClosure.putEdge(N2, N2); expectedClosure.putEdge(N2, N3); expectedClosure.putEdge(N2, N4); expectedClosure.putEdge(N3, N3); expectedClosure.putEdge(N3, N4); expectedClosure.putEdge(N4, N4); checkTransitiveClosure(undirectedGraph, expectedClosure); }
@Test public void customComparator() { Comparator<NonComparableSuperClass> comparator = new Comparator<NonComparableSuperClass>() { @Override public int compare(NonComparableSuperClass left, NonComparableSuperClass right) { return left.value.compareTo(right.value); } }; MutableGraph<NonComparableSuperClass> graph = GraphBuilder.undirected().nodeOrder(ElementOrder.sorted(comparator)).build(); NonComparableSuperClass node1 = new NonComparableSuperClass(1); NonComparableSuperClass node3 = new NonComparableSuperClass(3); NonComparableSuperClass node5 = new NonComparableSuperClass(5); NonComparableSuperClass node7 = new NonComparableSuperClass(7); graph.addNode(node1); graph.addNode(node7); graph.addNode(node5); graph.addNode(node3); assertThat(graph.nodeOrder().comparator()).isEqualTo(comparator); assertThat(graph.nodes()).containsExactly(node1, node3, node5, node7).inOrder(); }
@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)); }
@Override public MutableGraph<Integer> createGraph() { return GraphBuilder.undirected().allowsSelfLoops(false).build(); }
@Test public void transpose_undirectedGraph() { MutableGraph<Integer> undirectedGraph = GraphBuilder.undirected().build(); undirectedGraph.putEdge(N1, N2); assertThat(transpose(undirectedGraph)).isSameAs(undirectedGraph); }
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); } }
@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) { } }