/** Creates an iterable over the vertices in the given graph in a depth-first * iteration order. */ public static <V, E extends DefaultEdge> Iterable<V> of( DirectedGraph<V, E> graph, V start) { // Doesn't actually return a DepthFirstIterator, but a list with the same // contents, which is more efficient. return buildList(graph, start); }
/** Populates a collection with the nodes reachable from a given node. */ public static <V, E extends DefaultEdge> void reachable(Collection<V> list, final DirectedGraph<V, E> graph, final V start) { buildListRecurse(list, new HashSet<>(), graph, start); }
/** Unit test for {@link DepthFirstIterator}. */ @Test public void testDepthFirst() { final DefaultDirectedGraph<String, DefaultEdge> graph = createDag(); final List<String> list = new ArrayList<String>(); for (String s : DepthFirstIterator.of(graph, "A")) { list.add(s); } assertThat(list.toString(), equalTo("[A, B, C, D, E, C, D, F]")); list.clear(); DepthFirstIterator.reachable(list, graph, "A"); assertThat(list.toString(), equalTo("[A, B, C, D, E, C, D, F]")); }
case ARBITRARY: case DEPTH_FIRST: return DepthFirstIterator.of(graph, start).iterator();
/** Unit test for {@link DepthFirstIterator}. */ @Test public void testDepthFirst() { final DefaultDirectedGraph<String, DefaultEdge> graph = createDag(); final List<String> list = new ArrayList<String>(); for (String s : DepthFirstIterator.of(graph, "A")) { list.add(s); } assertThat(list.toString(), equalTo("[A, B, C, D, E, C, D, F]")); list.clear(); DepthFirstIterator.reachable(list, graph, "A"); assertThat(list.toString(), equalTo("[A, B, C, D, E, C, D, F]")); }
case ARBITRARY: case DEPTH_FIRST: return DepthFirstIterator.of(graph, start).iterator();
/** Populates a collection with the nodes reachable from a given node. */ public static <V, E extends DefaultEdge> void reachable(Collection<V> list, final DirectedGraph<V, E> graph, final V start) { buildListRecurse(list, new HashSet<>(), graph, start); }
/** Creates an iterable over the vertices in the given graph in a depth-first * iteration order. */ public static <V, E extends DefaultEdge> Iterable<V> of( DirectedGraph<V, E> graph, V start) { // Doesn't actually return a DepthFirstIterator, but a list with the same // contents, which is more efficient. return buildList(graph, start); }
private static <V, E extends DefaultEdge> List<V> buildList( DirectedGraph<V, E> graph, V start) { final List<V> list = new ArrayList<>(); buildListRecurse(list, new HashSet<>(), graph, start); return list; }
public DepthFirstIterator(DirectedGraph<V, E> graph, V start) { // Dumb implementation that builds the list first. iterator = buildList(graph, start).iterator(); }
private static <V, E extends DefaultEdge> List<V> buildList( DirectedGraph<V, E> graph, V start) { final List<V> list = new ArrayList<>(); buildListRecurse(list, new HashSet<>(), graph, start); return list; }
public DepthFirstIterator(DirectedGraph<V, E> graph, V start) { // Dumb implementation that builds the list first. iterator = buildList(graph, start).iterator(); }
private static <V, E extends DefaultEdge> void buildListRecurse( Collection<V> list, Set<V> activeVertices, DirectedGraph<V, E> graph, V start) { if (!activeVertices.add(start)) { return; } list.add(start); List<E> edges = graph.getOutwardEdges(start); for (E edge : edges) { //noinspection unchecked buildListRecurse(list, activeVertices, graph, (V) edge.target); } activeVertices.remove(start); }
private static <V, E extends DefaultEdge> void buildListRecurse( Collection<V> list, Set<V> activeVertices, DirectedGraph<V, E> graph, V start) { if (!activeVertices.add(start)) { return; } list.add(start); List<E> edges = graph.getOutwardEdges(start); for (E edge : edges) { //noinspection unchecked buildListRecurse(list, activeVertices, graph, (V) edge.target); } activeVertices.remove(start); }