{"record":{"id":"9f03f8f582b1640d","repo":"TheAlgorithms/Java","slug":"each-edge-must-have-exactly-2-nodes","errorCode":null,"errorMessage":"Each edge must have exactly 2 nodes","messagePattern":"Each edge must have exactly 2 nodes","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/trees/CentroidDecomposition.java","lineNumber":202,"sourceCode":"    public static CentroidTree buildFromEdges(int n, int[][] edges) {\n        if (n <= 0) {\n            throw new IllegalArgumentException(\"Number of nodes must be positive\");\n        }\n        if (edges == null) {\n            throw new IllegalArgumentException(\"Edges cannot be null\");\n        }\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":184,"sourceCodeEnd":218,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/trees/CentroidDecomposition.java#L184-L218","documentation":"Thrown by CentroidDecomposition.buildFromEdges when an individual edge array does not have exactly two elements. Each undirected edge is represented as [u, v]; an edge with 1 or 3+ elements is malformed and cannot be unpacked. The IllegalArgumentException rejects the edge before reading edge[0]/edge[1].","triggerScenarios":"Passing an edge like {1} (missing endpoint). Passing an edge like {1, 2, 3} (extra element). Malformed serialization where an edge row has the wrong column count. Using a weighted-edge triple [u,v,w] where the builder expects unweighted pairs.","commonSituations":"CSV/matrix input with ragged rows. Mixing weighted and unweighted edge representations. Copy-paste errors in test data. Off-by-one in column slicing during parsing.","solutions":["Normalize every edge to exactly two integers [u, v] before calling.","If your data carries weights, strip them: map [u,v,w] to new int[]{u,v}.","Validate row lengths at parse time and reject/log malformed rows.","Use a dedicated Edge class internally and convert to int[2] at the boundary."],"exampleFix":"// before\nint[][] edges = weightedEdges; // each is [u, v, w]\n// after\nint[][] edges = Arrays.stream(weightedEdges)\n    .map(e -> new int[]{e[0], e[1]})\n    .toArray(int[][]::new);","handlingStrategy":"validation","validationCode":"for (int[] e : edges) {\n    if (e.length != 2) {\n        throw new IllegalArgumentException(\"edge must have 2 nodes: \" + Arrays.toString(e));\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Normalize every edge to exactly [u, v].","Strip weights from [u,v,w] triples before passing.","Validate row lengths at parse time."],"tags":["tree","invalid-argument","java","datastructures","centroid-decomposition","data-format"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}