{"record":{"id":"5dab68d16b57e3a4","repo":"TheAlgorithms/Java","slug":"neighbor-index-out-of-bounds-v","errorCode":null,"errorMessage":"Neighbor index out of bounds: {v}","messagePattern":"Neighbor index out of bounds: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/graph/BronKerbosch.java","lineNumber":46,"sourceCode":"     * @throws IllegalArgumentException if the adjacency list is {@code null}, contains {@code null}\n     *         entries, or references invalid vertices\n     */\n    public static List<Set<Integer>> findMaximalCliques(List<Set<Integer>> adjacency) {\n        if (adjacency == null) {\n            throw new IllegalArgumentException(\"Adjacency list must not be null\");\n        }\n\n        int n = adjacency.size();\n        List<Set<Integer>> graph = new ArrayList<>(n);\n        for (int u = 0; u < n; u++) {\n            Set<Integer> neighbors = adjacency.get(u);\n            if (neighbors == null) {\n                throw new IllegalArgumentException(\"Adjacency list must not contain null sets\");\n            }\n            Set<Integer> copy = new HashSet<>();\n            for (int v : neighbors) {\n                if (v < 0 || v >= n) {\n                    throw new IllegalArgumentException(\"Neighbor index out of bounds: \" + v);\n                }\n                if (v != u) {\n                    copy.add(v);\n                }\n            }\n            graph.add(copy);\n        }\n\n        Set<Integer> r = new HashSet<>();\n        Set<Integer> p = new HashSet<>();\n        Set<Integer> x = new HashSet<>();\n        for (int v = 0; v < n; v++) {\n            p.add(v);\n        }\n\n        List<Set<Integer>> cliques = new ArrayList<>();\n        bronKerboschPivot(r, p, x, graph, cliques);\n        return cliques;","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/graph/BronKerbosch.java#L28-L64","documentation":"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>'.","triggerScenarios":"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.","commonSituations":"External vertex ids that are 1-based; graph mutation after adjacency was built; merging graphs of different sizes.","solutions":["Ensure all neighbor indices are in [0, adjacency.size()).","Convert external/1-based ids to internal 0-based indices before building adjacency.","Validate the adjacency list in a helper before calling findMaximalCliques."],"exampleFix":"// before\nadj.get(0).add(5); // but n == 3\n\n// after\nfor (int u = 0; u < adj.size(); u++) {\n    adj.get(u).removeIf(v -> v < 0 || v >= adj.size());\n}","handlingStrategy":"validation","validationCode":"int n = adjacency.size();\nfor (int u = 0; u < n; u++) {\n    final int fu = u;\n    adjacency.get(u).removeIf(v -> v < 0 || v >= n);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Convert external/1-based vertex ids to 0-based internal indices.","Validate neighbor ranges when building adjacency.","Rebuild adjacency after vertex removal."],"tags":["input-validation","index-out-of-bounds","graph"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}