TheAlgorithms/Java · error · IllegalArgumentException
Damping factor must be between 0 and 1
Error message
Damping factor must be between 0 and 1
What it means
Thrown by PageRank.validateInputParameters when dampingFactor is outside [0, 1]. The damping factor weights the random-surfer contribution in the PageRank formula; values below 0 or above 1 break the probabilistic model and the convergence of the iteration.
Source
Thrown at src/main/java/com/thealgorithms/others/PageRank.java:188
}
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++) {
pageRankValues[i] = initialPageRank;
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- Pass dampingFactor as a fraction in [0, 1] (the classic default is 0.85).
- If your input is a percentage, divide by 100 before passing.
- Clamp the value to [0, 1] at the boundary.
- Validate at config-load time so the error surfaces with full context.
Example fix
// before pageRank.calc(totalNodes, 85, iterations); // 85 > 1 -> throws // after double damping = percent / 100.0; // e.g. 85 -> 0.85 pageRank.calc(totalNodes, damping, iterations);
Defensive patterns
Strategy: validation
Validate before calling
double damping = Math.max(0.0, Math.min(1.0, dampingFactor)); pageRank.calc(totalNodes, damping, iterations);
Type guard
public static boolean isValidDamping(double d) {
return d >= 0.0 && d <= 1.0;
} Try / catch
try {
pageRank.calc(total, damping, iters);
} catch (IllegalArgumentException e) {
if (e.getMessage().contains("Damping")) {
pageRank.calc(total, 0.85, iters); // classic default
} else throw e;
} Prevention
- Pass damping as a fraction in [0, 1]; the classic value is 0.85.
- If input is a percentage, divide by 100 first.
- Clamp the value at the config boundary.
When it happens
Trigger: Triggering a calculation that calls validateInputParameters with dampingFactor < 0 or dampingFactor > 1; passing a percentage (e.g. 85) instead of a fraction (0.85).
Common situations: Supplying a percentage (85) instead of a fraction (0.85); a slider/config yielding an out-of-range value; defaulting an unset parameter to 0 and then nudging it negative.
Related errors
- Number of nodes must be between 1 and + MAX_NODES
- Node index out of bounds
- Total nodes must be between 1 and + MAX_NODES
- Iterations must be at least 1
- Incorrect length parameters: minLength must be <= maxLength
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/27458acd279e931f.
Report an issue: GitHub.