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
- Limit the graph to at most 10 nodes before constructing, or pick a different PageRank implementation without a hard cap.
- Ensure numberOfNodes is at least 1; reject empty graphs upstream.
- If you need more than 10 nodes, fork/extend the class to raise MAX_NODES (it is a compile-time constant).
- 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
- This implementation caps at 10 nodes; validate against [1, 10].
- For larger graphs, choose a PageRank without a hard-coded MAX_NODES.
- Reject empty graphs (0 nodes) upstream.
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
- Total nodes must be between 1 and + MAX_NODES
- Node index out of bounds
- Damping factor must be between 0 and 1
- Iterations must be at least 1
- Number of vertices must be positive
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/46df106be3b94e0c.
Report an issue: GitHub.