{"record":{"id":"ef80edc3196cbfaa","repo":"TheAlgorithms/Java","slug":"vertex-vertex-is-out-of-bounds","errorCode":null,"errorMessage":"Vertex {vertex} is out of bounds","messagePattern":"Vertex (.+?) is out of bounds","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/graphs/WelshPowell.java","lineNumber":78,"sourceCode":"        private void addEdge(int nodeA, int nodeB) {\n            validateVertex(nodeA);\n            validateVertex(nodeB);\n            if (nodeA == nodeB) {\n                throw new IllegalArgumentException(\"Self-loops are not allowed\");\n            }\n            adjacencyLists[nodeA].add(nodeB);\n            adjacencyLists[nodeB].add(nodeA);\n        }\n\n        /**\n         * Validates that the vertex index is within the bounds of the graph.\n         *\n         * @param vertex the index of the vertex to validate\n         * @throws IllegalArgumentException if the vertex is out of bounds\n         */\n        private void validateVertex(int vertex) {\n            if (vertex < 0 || vertex >= getNumVertices()) {\n                throw new IllegalArgumentException(\"Vertex \" + vertex + \" is out of bounds\");\n            }\n        }\n\n        /**\n         * Returns the adjacency list for a specific vertex.\n         *\n         * @param vertex the index of the vertex\n         * @return the set of adjacent vertices\n         */\n        HashSet<Integer> getAdjacencyList(int vertex) {\n            return adjacencyLists[vertex];\n        }\n\n        /**\n         * Returns the number of vertices in the graph.\n         *\n         * @return the number of vertices\n         */","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/graphs/WelshPowell.java#L60-L96","documentation":"Thrown by `WelshPowell.Graph.validateVertex` when a vertex index is `< 0` or `>= getNumVertices()`. Every vertex passed to `addEdge` must index an existing vertex; the guard runs for both endpoints. It protects the `adjacencyLists` array from out-of-bounds access.","triggerScenarios":"Calling `makeGraph` (or `addEdge`) with a vertex `>= numberOfVertices` or negative — e.g. a 1-based index, or an edge referencing a vertex beyond the declared count.","commonSituations":"1-based vs 0-based confusion; vertex count smaller than the largest referenced vertex; edges built for a larger graph.","solutions":["Ensure all vertex indices are in `[0, numberOfVertices)`","Convert 1-based data to 0-based","Set `numberOfVertices` to at least `max(all endpoints) + 1`"],"exampleFix":"// before (index 4 exceeds count 3)\nWelshPowell.makeGraph(3, new int[][]{{0, 4}});\n// after (count covers index 4)\nWelshPowell.makeGraph(5, new int[][]{{0, 4}});","handlingStrategy":"validation","validationCode":"for (int[] e : edges) {\n    for (int v : e) {\n        if (v < 0 || v >= numberOfVertices) {\n            throw new IllegalArgumentException(\"vertex \" + v + \" out of bounds\");\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    WelshPowell.makeGraph(n, edges);\n} catch (IllegalArgumentException e) {\n    // out-of-bounds vertex\n}","preventionTips":["Derive vertex count from the maximum referenced endpoint + 1","Normalize imported data to 0-based indices"],"tags":["graph","graph-coloring","indexing","input-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}