class 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);
            }
        }
    }

}