{"record":{"id":"81e46100b994c631","repo":"TheAlgorithms/Java","slug":"self-loops-are-not-allowed","errorCode":null,"errorMessage":"Self-loops are not allowed","messagePattern":"Self-loops are not allowed","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/graphs/WelshPowell.java","lineNumber":64,"sourceCode":"                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\");\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        /**","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/graphs/WelshPowell.java#L46-L82","documentation":"Thrown by `WelshPowell.Graph.addEdge` when both endpoints are equal (`nodeA == nodeB`). Welsh-Powell graph coloring assumes a simple graph; a self-loop has no meaningful color assignment, so it is rejected. Vertices are validated for bounds first, then the self-loop check runs.","triggerScenarios":"Calling `makeGraph` with an edge `[v, v]`, or `addEdge(v, v)` directly — both endpoints identical.","commonSituations":"Data where a vertex is accidentally linked to itself (diagonal in an adjacency source); un-deduplicated edge endpoints; off-by-one that makes two distinct values coincide.","solutions":["Filter out self-loop edges `[v, v]` before calling makeGraph","Fix the data source so endpoints differ","If self-loops are meaningful in your model, use a graph type that supports them"],"exampleFix":"// before\nint[][] edges = {{0, 0}, {0, 1}};\n// after (removed self-loop)\nint[][] edges = {{0, 1}};","handlingStrategy":"validation","validationCode":"for (int[] e : edges) {\n    if (e[0] == e[1]) {\n        throw new IllegalArgumentException(\"self-loop at \" + e[0]);\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    WelshPowell.makeGraph(n, edges);\n} catch (IllegalArgumentException e) {\n    // self-loop or bad vertex\n}","preventionTips":["Sanitize edge data to remove self-loops before construction","Validate symmetry/diagonal entries when importing adjacency data"],"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"}