GraphUtil { public static <V,E> boolean areVerticesWeaklyConnected(Hypergraph<V, E> studyGraph, V first, V second) { Set<V> visited = new HashSet<V>(); LinkedList<V> fringe = new LinkedList<V>(); fringe.add(first); while (!fringe.isEmpty()) { V v = fringe.removeFirst(); if (v.equals(second)) { return true; } visited.add(v); HashSet<V> neighbors = new HashSet<V>(studyGraph.getNeighbors(v)); neighbors.removeAll(visited); neighbors.removeAll(fringe); fringe.addAll(neighbors); } 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 testAreVerticesWeaklyConnected() { Graph<String, Integer> graph = new SparseMultigraph<String, Integer>(); graph.addEdge(1, "A", "B", EdgeType.DIRECTED); graph.addVertex("C"); assertFalse(GraphUtil.areVerticesWeaklyConnected(graph, "A", "C")); assertTrue(GraphUtil.areVerticesWeaklyConnected(graph, "A", "B")); assertTrue(GraphUtil.areVerticesWeaklyConnected(graph, "B", "A")); graph.addEdge(2, "B", "C"); assertTrue(GraphUtil.areVerticesWeaklyConnected(graph, "A", "C")); graph.addEdge(3, "D", "E"); assertTrue(GraphUtil.areVerticesWeaklyConnected(graph, "A", "C")); graph.addEdge(4, "C", "A"); assertTrue(GraphUtil.areVerticesWeaklyConnected(graph, "A", "C")); assertFalse(GraphUtil.areVerticesWeaklyConnected(graph, "A", "D")); }