TheAlgorithms/Java · error · IllegalArgumentException

Number of nodes must be positive

Error message

Number of nodes must be positive

What it means

Thrown by CentroidDecomposition.buildFromEdges when n is zero or negative. A tree must have at least one node and exactly n-1 edges; the method allocates arrays of size n, so a non-positive n is invalid. The IllegalArgumentException fails fast before any allocation or edge processing.

Source

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

                    sb.append("Parent ").append(parent[i]);
                }
                sb.append("\n");
            }
            return sb.toString();
        }
    }

    /**
     * Creates a centroid tree from an edge list.
     *
     * @param n number of nodes (0-indexed: 0 to n-1)
     * @param edges list of edges where each edge is [u, v]
     * @return CentroidTree object
     * @throws IllegalArgumentException if n <= 0 or edges is invalid
     */
    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];

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure n >= 1 before calling; reject empty graphs at the input boundary.
  2. Compute n from the actual vertex set rather than a separate possibly-wrong counter.
  3. Validate parsed configuration before it reaches the builder.
  4. Handle the n>=1 requirement explicitly in your domain model.

Example fix

// before
CentroidTree ct = CentroidDecomposition.buildFromEdges(n, edges);
// after
if (n <= 0) {
    throw new IllegalArgumentException("Graph must have at least one node");
}
CentroidTree ct = CentroidDecomposition.buildFromEdges(n, edges);
Defensive patterns

Strategy: validation

Validate before calling

if (n > 0) {
    CentroidTree ct = CentroidDecomposition.buildFromEdges(n, edges);
}

Prevention

When it happens

Trigger: Calling buildFromEdges(0, edges) or buildFromEdges with a negative count. Passing a node count derived from an empty input set. Passing a computed n that underflows due to an off-by-one.

Common situations: Graph input where the vertex count is missing or parsed as 0. Test fixtures with degenerate inputs. Logic that computes n = edges + 1 on an empty edge list yielding n=1 but mis-set to 0.

Related errors


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