BreadthFirstGraphVisitor 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(); for (Vertex<V> vertex : vertices) { vertex.setProperty("color", GraphColor.WHITE); vertex.setProperty("distance", Integer.MAX_VALUE); vertex.setProperty("parent", null); } if (comparator != null) { Collections.sort(vertices, comparator); } for (Vertex<V> vertex : vertices) { if (GraphColor.WHITE.equals(vertex.getProperty("color", GraphColor.class))) { visitSubGraph(copy, visitor, vertex); } } return copy; } BreadthFirstGraphVisitor();  BreadthFirstGraphVisitor(Comparator<Vertex<V>> comparator); @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 testDirectedGraph() throws Exception { final BreadthFirstGraphVisitor<EdgeDetails, VertexDetails> visitor = new BreadthFirstGraphVisitor<>(); final Graph<EdgeDetails, VertexDetails> graph = GraphSamples.directed(); final Graph<EdgeDetails, VertexDetails> result = visitor.visit(graph, 0, new GraphVertexVisitorAdapter<EdgeDetails, VertexDetails>() { }); assertThat(result.get(0).getProperty("distance", Integer.class), is(0)); assertThat(result.get(1).getProperty("distance", Integer.class), is(1)); assertThat(result.get(2).getProperty("distance", Integer.class), is(2)); assertThat(result.get(3).getProperty("distance", Integer.class), is(1)); assertThat(result.get(4).getProperty("distance", Integer.class), is(Integer.MAX_VALUE)); assertThat(result.get(5).getProperty("distance", Integer.class), is(Integer.MAX_VALUE)); 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(0))); assertThat(result.get(4).getProperty("parent", new ParameterizedTypeReference<Vertex<VertexDetails>>() {}), is(nullValue())); assertThat(result.get(5).getProperty("parent", new ParameterizedTypeReference<Vertex<VertexDetails>>() {}), is(nullValue())); }
@Test public void testUndirectedGraph() throws Exception { final BreadthFirstGraphVisitor<EdgeDetails, VertexDetails> visitor = new BreadthFirstGraphVisitor<>(); final Graph<EdgeDetails, VertexDetails> graph = GraphSamples.undirected(); final Graph<EdgeDetails, VertexDetails> result = visitor.visit(graph, 0, new GraphVertexVisitorAdapter<EdgeDetails, VertexDetails>() { }); assertThat(result.get(0).getProperty("distance", Integer.class), is(0)); assertThat(result.get(1).getProperty("distance", Integer.class), is(1)); assertThat(result.get(2).getProperty("distance", Integer.class), is(1)); assertThat(result.get(3).getProperty("distance", Integer.class), is(1)); assertThat(result.get(4).getProperty("distance", Integer.class), is(2)); assertThat(result.get(5).getProperty("distance", Integer.class), is(2)); 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(0))); assertThat(result.get(3).getProperty("parent", new ParameterizedTypeReference<Vertex<VertexDetails>>() {}), is(result.get(0))); 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(1))); }