{"record":{"id":"6ee9256975891668","repo":"TheAlgorithms/Java","slug":"number-of-vertices-cannot-be-negative","errorCode":null,"errorMessage":"Number of vertices cannot be negative","messagePattern":"Number of vertices cannot be negative","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/graphs/WelshPowell.java","lineNumber":46,"sourceCode":"\n    private WelshPowell() {\n    }\n\n    /**\n     * Represents a graph using an adjacency list.\n     */\n    static final class Graph {\n        private final HashSet<Integer>[] adjacencyLists;\n\n        /**\n         * Initializes a graph with a specified number of vertices.\n         *\n         * @param vertices the number of vertices in the graph\n         * @throws IllegalArgumentException if the number of vertices is negative\n         */\n        private Graph(int vertices) {\n            if (vertices < 0) {\n                throw new IllegalArgumentException(\"Number of vertices cannot be negative\");\n            }\n\n            adjacencyLists = new HashSet[vertices];\n            Arrays.setAll(adjacencyLists, i -> new HashSet<>());\n        }\n\n        /**\n         * Adds an edge between two vertices in the graph.\n         *\n         * @param nodeA one end of the edge\n         * @param nodeB the other end of the edge\n         * @throws IllegalArgumentException if the vertices are out of bounds or if a self-loop is attempted\n         */\n        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\");","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/graphs/WelshPowell.java#L28-L64","documentation":"Thrown by the WelshPowell.Graph constructor when `vertices` is negative. The constructor allocates `HashSet[vertices]`, so a negative count is invalid. Zero is allowed (an empty coloring domain). Reached via `WelshPowell.makeGraph(numberOfVertices, edges)`.","triggerScenarios":"Calling `WelshPowell.makeGraph(numberOfVertices, edges)` (which constructs `new Graph(numberOfVertices)`) with a negative vertex count.","commonSituations":"Negative vertex count from unchecked input parsing or underflowing arithmetic; reading the count from malformed config.","solutions":["Validate `vertices >= 0` before building the graph","Fix the upstream computation producing the negative value","Reject the input early at the parse boundary"],"exampleFix":"// before\nGraph g = WelshPowell.makeGraph(n - 1, edges);\n// after\nif (n < 0) throw new IllegalArgumentException(\"n >= 0\");\nGraph g = WelshPowell.makeGraph(n, edges);","handlingStrategy":"validation","validationCode":"if (numberOfVertices < 0) {\n    throw new IllegalArgumentException(\"vertices must be >= 0\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    WelshPowell.makeGraph(n, edges);\n} catch (IllegalArgumentException e) {\n    // handle negative count\n}","preventionTips":["Validate vertex counts at the input boundary","Guard subtraction that could produce a negative count"],"tags":["graph","graph-coloring","input-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}