{"record":{"id":"0ef1e199d9262906","repo":"TheAlgorithms/Java","slug":"invalid-node-in-edge","errorCode":null,"errorMessage":"Invalid node in edge: [{}, {}]","messagePattern":"Invalid node in edge: \\[(.+?), (.+?)\\]","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/trees/CentroidDecomposition.java","lineNumber":208,"sourceCode":"        }\n        if (edges.length != n - 1) {\n            throw new IllegalArgumentException(\"Tree must have exactly n-1 edges\");\n        }\n\n        List<List<Integer>> adj = new ArrayList<>();\n        for (int i = 0; i < n; i++) {\n            adj.add(new ArrayList<>());\n        }\n\n        for (int[] edge : edges) {\n            if (edge.length != 2) {\n                throw new IllegalArgumentException(\"Each edge must have exactly 2 nodes\");\n            }\n            int u = edge[0];\n            int v = edge[1];\n\n            if (u < 0 || u >= n || v < 0 || v >= n) {\n                throw new IllegalArgumentException(\"Invalid node in edge: [\" + u + \", \" + v + \"]\");\n            }\n\n            adj.get(u).add(v);\n            adj.get(v).add(u);\n        }\n\n        return new CentroidTree(adj);\n    }\n}\n","sourceCodeStart":190,"sourceCodeEnd":218,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/trees/CentroidDecomposition.java#L190-L218","documentation":"Thrown by CentroidDecomposition.buildFromEdges when an edge references a node id outside [0, n). The builder indexes adj.get(u) and adj.get(v), so an out-of-range endpoint would throw IndexOutOfBoundsException. The IllegalArgumentException reports both offending endpoints.","triggerScenarios":"Passing edges with 1-based node ids into a 0-based builder. Passing an edge [u, n] where n equals the node count (valid range is 0..n-1). Negative ids from sentinel/uninitialized values.","commonSituations":"Indexing-convention mismatch between input data (often 1-based) and the algorithm (0-based). Node count n set too low relative to the actual max id in edges. Edges built from a different vertex labeling than expected.","solutions":["Convert 1-based external ids to 0-based by subtracting 1 before building edges.","Set n to max(edge ids) + 1 (after conversion) so all endpoints are in range.","Validate every endpoint against [0, n-1] at the input boundary.","Reject self-loops and duplicate edges which often accompany id bugs."],"exampleFix":"// before\nCentroidTree ct = CentroidDecomposition.buildFromEdges(n, edges);\n// after\nfor (int[] e : edges) {\n    if (e[0] < 0 || e[0] >= n || e[1] < 0 || e[1] >= n) {\n        throw new IllegalArgumentException(\"edge out of range: \" + Arrays.toString(e));\n    }\n}\nCentroidTree ct = CentroidDecomposition.buildFromEdges(n, edges);","handlingStrategy":"validation","validationCode":"for (int[] e : edges) {\n    if (e[0] < 0 || e[0] >= n || e[1] < 0 || e[1] >= n) {\n        throw new IllegalArgumentException(\"edge endpoint out of range: \" + Arrays.toString(e));\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Convert 1-based ids to 0-based before building edges.","Set n = maxId + 1 to cover all endpoints.","Validate endpoint ranges at the input boundary."],"tags":["tree","invalid-argument","java","datastructures","centroid-decomposition","indexing"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}