GraphUtil { public static <V, E> boolean isWeaklyConnected(Hypergraph<V, E> graph) { if (graph.getVertexCount() == 0) { return true; } else { HashSet<V> visited = new HashSet<V>(); LinkedList<V> fringe = new LinkedList<V>(); fringe.add(graph.getVertices().iterator().next()); while (!fringe.isEmpty()) { V v = fringe.removeFirst(); if (!visited.contains(v)) { visited.add(v); fringe.addAll(graph.getNeighbors(v)); } if (visited.size() == graph.getVertexCount()) { return true; } } return false; } }  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 testIsWeaklyConnected() { Graph<String, Integer> graph = new SparseMultigraph<String, Integer>(); graph.addEdge(1, "A", "B", EdgeType.DIRECTED); graph.addVertex("C"); assertFalse(GraphUtil.isWeaklyConnected(graph)); graph.addEdge(2, "A", "C"); assertTrue(GraphUtil.isWeaklyConnected(graph)); graph.addEdge(3, "D", "E"); assertFalse(GraphUtil.isWeaklyConnected(graph)); }