GraphUtil { public static <V, E> void copyGraph(Graph<V, E> source, Graph<V, E> target) { addVertices(target, source.getVertices()); for (E e : source.getEdges()) { target.addEdge(e, source.getIncidentVertices(e), source.getEdgeType(e)); } }  static void addVertices(Graph<V, E> graph, Collection<V> vertices); static void copyGraph(Graph<V, E> source, Graph<V, E> target); static void copyTree(Tree<V, E> source, Tree<V, E> target); static boolean isDescendant(Tree<V, E> t, V v, V w); static boolean isWeaklyConnected(Hypergraph<V, E> graph); static boolean areVerticesWeaklyConnected(Hypergraph<V, E> studyGraph, V first, V second); static boolean isSimpleCycle(UndirectedGraph<V, E> graph); static V findCommonAncestor(Tree<V, E> tree, V u, V v); static List<V> findPath(Tree<V, E> tree, V u, V v);  }
@Test public void testCopyGraph() { Graph<String, Integer> graph = new SparseMultigraph<String, Integer>(); graph.addEdge(1, "A", "B", EdgeType.DIRECTED); graph.addEdge(2, "A", "B", EdgeType.DIRECTED); graph.addEdge(4, "D", "E", EdgeType.UNDIRECTED); graph.addEdge(5, "E", "D", EdgeType.DIRECTED); graph.addVertex("C"); Graph<String, Integer> copy = new SparseMultigraph<String, Integer>(); GraphUtil.copyGraph(graph, copy); assertEquals(graph.getVertexCount(), copy.getVertexCount()); assertEquals(graph.getEdgeCount(), copy.getEdgeCount()); assertTrue(copy.containsVertex("A")); assertTrue(copy.containsVertex("B")); assertTrue(copy.containsVertex("C")); assertTrue(copy.containsVertex("D")); assertTrue(copy.containsVertex("E")); assertTrue(copy.containsEdge(1)); assertTrue(copy.containsEdge(2)); assertTrue(copy.containsEdge(4)); assertTrue(copy.containsEdge(5)); assertEquals(EdgeType.DIRECTED, copy.getEdgeType(1)); assertEquals(EdgeType.DIRECTED, copy.getEdgeType(2)); assertEquals(EdgeType.UNDIRECTED, copy.getEdgeType(4)); assertEquals(EdgeType.DIRECTED, copy.getEdgeType(5)); assertEquals(new Pair<String>("A", "B"), copy.getEndpoints(1)); assertEquals(new Pair<String>("A", "B"), copy.getEndpoints(2)); assertEquals(new Pair<String>("D", "E"), copy.getEndpoints(4)); assertEquals(new Pair<String>("E", "D"), copy.getEndpoints(5)); }