DepthFirstGraphVisitor implements GraphVisitor<E, V> { @Override public Graph<E, V> visit(Graph<E, V> graph, GraphVertexVisitor<E, V> visitor) { final Graph<E, V> copy = GraphUtils.copy(graph); final List<Vertex<V>> vertices = copy.getVertices(); if (comparator != null) { Collections.sort(vertices, comparator); } for (Vertex<V> vertex : vertices) { vertex.setProperty("color", GraphColor.WHITE); vertex.setProperty("parent", null); vertex.setProperty("distance", 0); } final AtomicInteger time = new AtomicInteger(0); for (Vertex<V> vertex : vertices) { if (GraphColor.WHITE.equals(vertex.getProperty("color", GraphColor.class))) { visitSubGraph(copy, vertex, time, visitor); } } return copy; } DepthFirstGraphVisitor(Comparator<Vertex<V>> comparator);  DepthFirstGraphVisitor(); @Override Graph<E, V> visit(Graph<E, V> graph, GraphVertexVisitor<E, V> visitor); @Override Graph<E, V> visit(Graph<E, V> graph, int start, GraphVertexVisitor<E, V> visitor);  }
@Test public void testDirected() throws Exception { final Graph<EdgeDetails, VertexDetails> graph = GraphSamples.directed(); final DepthFirstGraphVisitor<EdgeDetails, VertexDetails> visitor = new DepthFirstGraphVisitor<>(); final Graph<EdgeDetails, VertexDetails> result = visitor.visit(graph, new GraphVertexVisitorAdapter<EdgeDetails, VertexDetails>() { }); assertThat(result.get(0).getProperty("discovery", Integer.class), is(1)); assertThat(result.get(1).getProperty("discovery", Integer.class), is(2)); assertThat(result.get(2).getProperty("discovery", Integer.class), is(4)); assertThat(result.get(3).getProperty("discovery", Integer.class), is(3)); assertThat(result.get(4).getProperty("discovery", Integer.class), is(9)); assertThat(result.get(5).getProperty("discovery", Integer.class), is(10)); assertThat(result.get(0).getProperty("finish", Integer.class), is(8)); assertThat(result.get(1).getProperty("finish", Integer.class), is(7)); assertThat(result.get(2).getProperty("finish", Integer.class), is(5)); assertThat(result.get(3).getProperty("finish", Integer.class), is(6)); assertThat(result.get(4).getProperty("finish", Integer.class), is(12)); assertThat(result.get(5).getProperty("finish", Integer.class), is(11)); assertThat(result.get(0).getProperty("parent", new ParameterizedTypeReference<Vertex<VertexDetails>>() {}), is(nullValue())); assertThat(result.get(1).getProperty("parent", new ParameterizedTypeReference<Vertex<VertexDetails>>() {}), is(result.get(0))); assertThat(result.get(2).getProperty("parent", new ParameterizedTypeReference<Vertex<VertexDetails>>() {}), is(result.get(3))); assertThat(result.get(3).getProperty("parent", new ParameterizedTypeReference<Vertex<VertexDetails>>() {}), is(result.get(1))); assertThat(result.get(4).getProperty("parent", new ParameterizedTypeReference<Vertex<VertexDetails>>() {}), is(nullValue())); assertThat(result.get(5).getProperty("parent", new ParameterizedTypeReference<Vertex<VertexDetails>>() {}), is(result.get(4))); }
@Test public void testUndirected() throws Exception { final Graph<EdgeDetails, VertexDetails> graph = GraphSamples.undirected(); final DepthFirstGraphVisitor<EdgeDetails, VertexDetails> visitor = new DepthFirstGraphVisitor<>(); final Graph<EdgeDetails, VertexDetails> result = visitor.visit(graph, new GraphVertexVisitorAdapter<EdgeDetails, VertexDetails>() { }); assertThat(result.get(0).getProperty("discovery", Integer.class), is(1)); assertThat(result.get(1).getProperty("discovery", Integer.class), is(2)); assertThat(result.get(2).getProperty("discovery", Integer.class), is(3)); assertThat(result.get(3).getProperty("discovery", Integer.class), is(4)); assertThat(result.get(4).getProperty("discovery", Integer.class), is(5)); assertThat(result.get(5).getProperty("discovery", Integer.class), is(6)); assertThat(result.get(0).getProperty("finish", Integer.class), is(12)); assertThat(result.get(1).getProperty("finish", Integer.class), is(11)); assertThat(result.get(2).getProperty("finish", Integer.class), is(10)); assertThat(result.get(3).getProperty("finish", Integer.class), is(9)); assertThat(result.get(4).getProperty("finish", Integer.class), is(8)); assertThat(result.get(5).getProperty("finish", Integer.class), is(7)); assertThat(result.get(0).getProperty("parent", new ParameterizedTypeReference<Vertex<VertexDetails>>() {}), is(nullValue())); assertThat(result.get(1).getProperty("parent", new ParameterizedTypeReference<Vertex<VertexDetails>>() {}), is(result.get(0))); assertThat(result.get(2).getProperty("parent", new ParameterizedTypeReference<Vertex<VertexDetails>>() {}), is(result.get(1))); assertThat(result.get(3).getProperty("parent", new ParameterizedTypeReference<Vertex<VertexDetails>>() {}), is(result.get(2))); assertThat(result.get(4).getProperty("parent", new ParameterizedTypeReference<Vertex<VertexDetails>>() {}), is(result.get(3))); assertThat(result.get(5).getProperty("parent", new ParameterizedTypeReference<Vertex<VertexDetails>>() {}), is(result.get(4))); }