TheAlgorithms/Java · error · IllegalArgumentException

Neighbor index out of bounds: {v}

Error message

Neighbor index out of bounds: {v}

What it means

Thrown by BronKerbosch.findMaximalCliques when a neighbor index v is < 0 or >= n (the adjacency list size). Vertex indices must reference valid vertices; an out-of-range index would cause ArrayIndexOutOfBounds later. Message: 'Neighbor index out of bounds: <v>'.

Source

Thrown at src/main/java/com/thealgorithms/graph/BronKerbosch.java:46

     * @throws IllegalArgumentException if the adjacency list is {@code null}, contains {@code null}
     *         entries, or references invalid vertices
     */
    public static List<Set<Integer>> findMaximalCliques(List<Set<Integer>> adjacency) {
        if (adjacency == null) {
            throw new IllegalArgumentException("Adjacency list must not be null");
        }

        int n = adjacency.size();
        List<Set<Integer>> graph = new ArrayList<>(n);
        for (int u = 0; u < n; u++) {
            Set<Integer> neighbors = adjacency.get(u);
            if (neighbors == null) {
                throw new IllegalArgumentException("Adjacency list must not contain null sets");
            }
            Set<Integer> copy = new HashSet<>();
            for (int v : neighbors) {
                if (v < 0 || v >= n) {
                    throw new IllegalArgumentException("Neighbor index out of bounds: " + v);
                }
                if (v != u) {
                    copy.add(v);
                }
            }
            graph.add(copy);
        }

        Set<Integer> r = new HashSet<>();
        Set<Integer> p = new HashSet<>();
        Set<Integer> x = new HashSet<>();
        for (int v = 0; v < n; v++) {
            p.add(v);
        }

        List<Set<Integer>> cliques = new ArrayList<>();
        bronKerboschPivot(r, p, x, graph, cliques);
        return cliques;

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure all neighbor indices are in [0, adjacency.size()).
  2. Convert external/1-based ids to internal 0-based indices before building adjacency.
  3. Validate the adjacency list in a helper before calling findMaximalCliques.

Example fix

// before
adj.get(0).add(5); // but n == 3

// after
for (int u = 0; u < adj.size(); u++) {
    adj.get(u).removeIf(v -> v < 0 || v >= adj.size());
}
Defensive patterns

Strategy: validation

Validate before calling

int n = adjacency.size();
for (int u = 0; u < n; u++) {
    final int fu = u;
    adjacency.get(u).removeIf(v -> v < 0 || v >= n);
}

Prevention

When it happens

Trigger: A neighbor set referencing a vertex id not in [0, n); 1-based vertex ids passed to a 0-based graph; a stale adjacency list after vertices were removed.

Common situations: External vertex ids that are 1-based; graph mutation after adjacency was built; merging graphs of different sizes.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/5dab68d16b57e3a4. Report an issue: GitHub.