UndirectedGraphImpl implements UndirectedGraph<V, E> { @Override public void traverse(int v, Traverser traverser, boolean[] encountered) { checkVertex(v); Objects.requireNonNull(traverser); Objects.requireNonNull(encountered); if (encountered.length < vertices.size()) { throw new PowsyblException("Encountered array is too small"); } TIntArrayList[] adjacencyList = getAdjacencyList(); TIntArrayList adjacentEdges = adjacencyList[v]; encountered[v] = true; for (int i = 0; i < adjacentEdges.size(); i++) { int e = adjacentEdges.getQuick(i); Edge<E> edge = edges.get(e); int v1 = edge.getV1(); int v2 = edge.getV2(); if (!encountered[v1]) { if (traverser.traverse(v2, e, v1) == TraverseResult.CONTINUE) { encountered[v1] = true; traverse(v1, traverser, encountered); } } else if (!encountered[v2] && traverser.traverse(v1, e, v2) == TraverseResult.CONTINUE) { encountered[v2] = true; traverse(v2, traverser, encountered); } } }  @Override int addVertex(); @Override void addVertexIfNotPresent(int v); @Override boolean vertexExists(int v); @Override V removeVertex(int v); @Override int getVertexCount(); @Override void removeAllVertices(); @Override int addEdge(int v1, int v2, E obj); @Override E removeEdge(int e); @Override void removeAllEdges(); @Override int getEdgeCount(); @Override int[] getVertices(); @Override int[] getEdges(); @Override int getVertexCapacity(); @Override Iterable<V> getVerticesObj(); @Override Stream<V> getVertexObjectStream(); @Override V getVertexObject(int v); @Override void setVertexObject(int v, V obj); @Override int getEdgeVertex1(int e); @Override int getEdgeVertex2(int e); @Override Iterable<E> getEdgesObject(); @Override Stream<E> getEdgeObjectStream(); @Override E getEdgeObject(int e); @Override List<E> getEdgeObjects(int v1, int v2); @Override void traverse(int v, Traverser traverser, boolean[] encountered); @Override void traverse(int v, Traverser traverser); @Override List<TIntArrayList> findAllPaths(int from, Function<V, Boolean> pathComplete, Function<E, Boolean> pathCancelled); @Override void addListener(UndirectedGraphListener l); @Override void removeListener(UndirectedGraphListener l); @Override void print(PrintStream out, Function<V, String> vertexToString, Function<E, String> edgeToString);  }
@Test public void testTraverse() { graph.addVertex(); graph.addVertex(); graph.addVertex(); graph.addVertex(); graph.addVertex(); graph.addVertex(); graph.setVertexObject(5, new Vertex("end")); graph.addEdge(0, 1, null); graph.addEdge(0, 2, null); graph.addEdge(0, 3, null); graph.addEdge(1, 4, null); graph.addEdge(2, 4, null); graph.addEdge(4, 5, null); graph.addEdge(3, 5, null); Traverser traverser = Mockito.mock(Traverser.class); Mockito.when(traverser.traverse(Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt())).thenReturn(TraverseResult.CONTINUE); Mockito.when(traverser.traverse(4, 3, 1)).thenReturn(TraverseResult.TERMINATE); Mockito.when(traverser.traverse(4, 4, 2)).thenReturn(TraverseResult.TERMINATE); Mockito.when(traverser.traverse(5, 6, 3)).thenReturn(TraverseResult.TERMINATE); boolean[] encountered = new boolean[graph.getVertexCount()]; Arrays.fill(encountered, false); graph.traverse(5, traverser, encountered); assertArrayEquals(new boolean[] {false, false, false, false, true, true}, encountered); graph.traverse(4, traverser); }