TheAlgorithms/Java · error · IllegalArgumentException

Number of nodes must be between 1 and + MAX_NODES

Error message

Number of nodes must be between 1 and  + MAX_NODES

What it means

Thrown by the PageRank constructor when numberOfNodes is outside [1, MAX_NODES] (MAX_NODES = 10). The implementation uses fixed MAX_NODES-sized adjacency and PageRank arrays, so node counts above 10 overflow those buffers and counts below 1 are meaningless; both are rejected.

Source

Thrown at src/main/java/com/thealgorithms/others/PageRank.java:46

    private static final int MAX_NODES = 10;
    private static final double DEFAULT_DAMPING_FACTOR = 0.85;
    private static final int DEFAULT_ITERATIONS = 2;

    private int[][] adjacencyMatrix;
    private double[] pageRankValues;
    private int nodeCount;

    /**
     * Constructor to initialize PageRank with specified number of nodes
     *
     * @param numberOfNodes the number of nodes/pages in the graph
     * @throws IllegalArgumentException if numberOfNodes is less than 1 or greater
     *                                  than MAX_NODES
     */
    public PageRank(int numberOfNodes) {
        if (numberOfNodes < 1 || numberOfNodes > MAX_NODES) {
            throw new IllegalArgumentException("Number of nodes must be between 1 and " + MAX_NODES);
        }
        this.nodeCount = numberOfNodes;
        this.adjacencyMatrix = new int[MAX_NODES][MAX_NODES];
        this.pageRankValues = new double[MAX_NODES];
    }

    /**
     * Default constructor for interactive mode
     */
    public PageRank() {
        this.adjacencyMatrix = new int[MAX_NODES][MAX_NODES];
        this.pageRankValues = new double[MAX_NODES];
    }

    /**
     * Main method for interactive PageRank calculation
     *
     * @param args command line arguments (not used)

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Limit the graph to at most 10 nodes before constructing, or pick a different PageRank implementation without a hard cap.
  2. Ensure numberOfNodes is at least 1; reject empty graphs upstream.
  3. If you need more than 10 nodes, fork/extend the class to raise MAX_NODES (it is a compile-time constant).
  4. Validate the count against [1, 10] at the data-loading boundary.

Example fix

// before
new PageRank(graph.size()); // graph.size() may be 0 or 25

// after
if (graph.size() < 1 || graph.size() > 10) {
    throw new IllegalArgumentException("This PageRank supports 1..10 nodes; got " + graph.size());
}
new PageRank(graph.size());
Defensive patterns

Strategy: validation

Validate before calling

if (numberOfNodes < 1 || numberOfNodes > 10) {
    throw new IllegalArgumentException("node count must be in [1, 10]; got " + numberOfNodes);
}
new PageRank(numberOfNodes);

Type guard

public static boolean isSupportedNodeCount(int n) {
    return n >= 1 && n <= 10;
}

Try / catch

try {
    pr = new PageRank(n);
} catch (IllegalArgumentException e) {
    // cap at 10 or use a different implementation for larger graphs
    throw e;
}

Prevention

When it happens

Trigger: Constructing new PageRank(n) with n < 1 or n > 10 (e.g. n = 0 or n = 25).

Common situations: Passing a graph larger than 10 nodes to this intentionally small implementation; an empty graph (0 nodes) from an empty input; dynamic graph sizes exceeding the hard-coded cap.

Related errors


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