TheAlgorithms/Java · error · IllegalArgumentException
Total nodes must be between 1 and + MAX_NODES
Error message
Total nodes must be between 1 and + MAX_NODES
What it means
Thrown by PageRank.validateInputParameters (called during calculation) when totalNodes is outside [1, MAX_NODES] (MAX_NODES = 10). This is the same node-count invariant enforced at construction but applied to the calculate path; it protects the fixed-size internal buffers.
Source
Thrown at src/main/java/com/thealgorithms/others/PageRank.java:185
if (verbose) {
System.out.println("\nFinal PageRank:");
printPageRanks(totalNodes);
}
return pageRankValues.clone();
}
/**
* Validates input parameters for PageRank calculation
*
* @param totalNodes the total number of nodes
* @param dampingFactor the damping factor
* @param iterations number of iterations
* @throws IllegalArgumentException if parameters are invalid
*/
private void validateInputParameters(int totalNodes, double dampingFactor, int iterations) {
if (totalNodes < 1 || totalNodes > MAX_NODES) {
throw new IllegalArgumentException("Total nodes must be between 1 and " + MAX_NODES);
}
if (dampingFactor < 0 || dampingFactor > 1) {
throw new IllegalArgumentException("Damping factor must be between 0 and 1");
}
if (iterations < 1) {
throw new IllegalArgumentException("Iterations must be at least 1");
}
}
/**
* Initializes PageRank values for all nodes
*
* @param totalNodes the total number of nodes
* @param initialPageRank the initial PageRank value
* @param verbose whether to print output
*/
private void initializePageRanks(int totalNodes, double initialPageRank, boolean verbose) {
for (int i = 1; i <= totalNodes; i++) {View on GitHub (pinned to fdfb9a395b)
Solutions
- Keep totalNodes within [1, 10] and consistent with the nodeCount used at construction.
- Source totalNodes from the same graph object used to build the PageRank instance.
- Use a PageRank implementation without a hard cap for larger graphs.
- Validate totalNodes at the boundary before invoking the calculation.
Example fix
// before pageRank.calc(graph.size(), damping, iterations); // graph.size() may be > 10 // after int total = Math.max(1, Math.min(graph.size(), 10)); pageRank.calc(total, damping, iterations);
Defensive patterns
Strategy: validation
Validate before calling
if (totalNodes < 1 || totalNodes > 10) {
throw new IllegalArgumentException("totalNodes must be in [1, 10]; got " + totalNodes);
}
pageRank.calc(totalNodes, dampingFactor, iterations); Type guard
public static boolean isSupportedNodeCount(int n) {
return n >= 1 && n <= 10;
} Try / catch
try {
pageRank.calc(total, damping, iters);
} catch (IllegalArgumentException e) {
total = Math.max(1, Math.min(total, 10));
pageRank.calc(total, damping, iters);
} Prevention
- Keep totalNodes consistent with the constructed nodeCount.
- Source totalNodes from the same graph used at construction.
- Cap graphs at 10 nodes for this implementation.
When it happens
Trigger: Triggering a calculate/step that internally calls validateInputParameters with totalNodes < 1 or totalNodes > 10; mismatch between the constructed nodeCount and a totalNodes argument passed to a calculation entry point.
Common situations: Passing a totalNodes argument inconsistent with the constructed size; loading a graph with more than 10 nodes; calling calculate with parameters derived from a different graph than the one used at construction.
Related errors
- Number of 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/110af38b45ed9714.
Report an issue: GitHub.