SpanningTreeIterable implements Iterable<Tree<V, E>> { public Iterator<Tree<V, E>> iterator() { return new SpanningTreeIterator(); } SpanningTreeIterable(DirectedGraph<V, E> graph, V root);  SpanningTreeIterable(DirectedGraph<V, E> graph, V root, Comparator<V> vertexComparator); Iterator<Tree<V, E>> iterator();  }
@Test public void testEnumerateDirected() { DirectedGraph<String, Integer> g = new DirectedSparseGraph<String, Integer>(); GraphUtil.addVertices(g, Arrays.asList("A", "B", "C", "D")); g.addEdge(1, "A", "B"); g.addEdge(2, "A", "C"); g.addEdge(3, "B", "D"); g.addEdge(4, "D", "C"); g.addEdge(5, "B", "C"); g.addEdge(6, "C", "B"); Iterator<Tree<String, Integer>> iterator = new SpanningTreeIterable<String, Integer>(g, "A", new StringComparator()).iterator(); assertTrue(iterator.hasNext()); Tree<String, Integer> tree1 = iterator.next(); assertTrue(tree1.containsEdge(1)); assertTrue(tree1.containsEdge(3)); assertTrue(tree1.containsEdge(5)); assertEquals(3, tree1.getEdgeCount()); assertTrue(iterator.hasNext()); Tree<String, Integer> tree2 = iterator.next(); assertTrue(tree2.containsEdge(1)); assertTrue(tree2.containsEdge(3)); assertTrue(tree2.containsEdge(4)); assertEquals(3, tree2.getEdgeCount()); assertTrue(iterator.hasNext()); Tree<String, Integer> tree3 = iterator.next(); assertTrue(tree3.containsEdge(1)); assertTrue(tree3.containsEdge(3)); assertTrue(tree3.containsEdge(2)); assertEquals(3, tree3.getEdgeCount()); assertTrue(iterator.hasNext()); Tree<String, Integer> tree4 = iterator.next(); assertTrue(tree4.containsEdge(2)); assertTrue(tree4.containsEdge(6)); assertTrue(tree4.containsEdge(3)); assertEquals(3, tree4.getEdgeCount()); assertFalse(iterator.hasNext()); }
@Test public void testEnumerateUndirected() { UndirectedGraph<String, Integer> ug = new UndirectedSparseGraph<String, Integer>(); GraphUtil.addVertices(ug, Arrays.asList("A", "B", "C", "D")); ug.addEdge(1, "A", "B"); ug.addEdge(2, "A", "C"); ug.addEdge(3, "A", "D"); ug.addEdge(4, "B", "C"); ug.addEdge(5, "B", "D"); ug.addEdge(6, "C", "D"); DirectedGraph<String, Integer> dg = (DirectedGraph<String, Integer>) DirectionTransformer.toDirected(ug, DirectedSparseGraph.<String, Integer>getFactory(), new Factory<Integer>() { int cnt = 0; public Integer create() { return ++cnt; } }, true); Iterator<Tree<String, Integer>> iterator = new SpanningTreeIterable<String, Integer>(dg, "A").iterator(); int i; for (i = 0; iterator.hasNext(); ++i) { iterator.next(); } assertEquals(16, i); }