TheAlgorithms/Java · error · IllegalArgumentException

Each edge must have exactly 2 nodes

Error message

Each edge must have exactly 2 nodes

What it means

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].

Source

Thrown at src/main/java/com/thealgorithms/datastructures/trees/CentroidDecomposition.java:202

    public static CentroidTree buildFromEdges(int n, int[][] edges) {
        if (n <= 0) {
            throw new IllegalArgumentException("Number of nodes must be positive");
        }
        if (edges == null) {
            throw new IllegalArgumentException("Edges cannot be null");
        }
        if (edges.length != n - 1) {
            throw new IllegalArgumentException("Tree must have exactly n-1 edges");
        }

        List<List<Integer>> adj = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            adj.add(new ArrayList<>());
        }

        for (int[] edge : edges) {
            if (edge.length != 2) {
                throw new IllegalArgumentException("Each edge must have exactly 2 nodes");
            }
            int u = edge[0];
            int v = edge[1];

            if (u < 0 || u >= n || v < 0 || v >= n) {
                throw new IllegalArgumentException("Invalid node in edge: [" + u + ", " + v + "]");
            }

            adj.get(u).add(v);
            adj.get(v).add(u);
        }

        return new CentroidTree(adj);
    }
}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Normalize every edge to exactly two integers [u, v] before calling.
  2. If your data carries weights, strip them: map [u,v,w] to new int[]{u,v}.
  3. Validate row lengths at parse time and reject/log malformed rows.
  4. Use a dedicated Edge class internally and convert to int[2] at the boundary.

Example fix

// before
int[][] edges = weightedEdges; // each is [u, v, w]
// after
int[][] edges = Arrays.stream(weightedEdges)
    .map(e -> new int[]{e[0], e[1]})
    .toArray(int[][]::new);
Defensive patterns

Strategy: validation

Validate before calling

for (int[] e : edges) {
    if (e.length != 2) {
        throw new IllegalArgumentException("edge must have 2 nodes: " + Arrays.toString(e));
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/9f03f8f582b1640d. Report an issue: GitHub.