TheAlgorithms/Java · error · IllegalArgumentException

Iterations must be at least 1

Error message

Iterations must be at least 1

What it means

Thrown by PageRank.validateInputParameters when iterations < 1. PageRank approximates ranks by repeated relaxation; zero or negative iterations would skip the computation entirely and leave ranks uninitialized, so at least one iteration is required.

Source

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

    }

    /**
     * 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;
        }

        if (verbose) {
            System.out.println("\nInitial PageRank Values, 0th Step");
            printPageRanks(totalNodes);

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Pass iterations >= 1 (a few dozen is typical for convergence).
  2. Default iterations to a positive value when unset (e.g. 20-100).
  3. Clamp the computed iteration count to a minimum of 1.
  4. Validate at the boundary where the parameter is parsed.

Example fix

// before
pageRank.calc(totalNodes, damping, iterations); // iterations may be 0

// after
int iters = Math.max(1, iterations);
pageRank.calc(totalNodes, damping, iters);
Defensive patterns

Strategy: validation

Validate before calling

int safeIterations = Math.max(1, iterations);
pageRank.calc(totalNodes, dampingFactor, safeIterations);

Type guard

public static boolean isPositiveIterations(int iters) {
    return iters >= 1;
}

Try / catch

try {
    pageRank.calc(total, damping, iters);
} catch (IllegalArgumentException e) {
    pageRank.calc(total, damping, Math.max(1, iters));
}

Prevention

When it happens

Trigger: Triggering a calculation with iterations = 0 or negative; an iterations count derived from a precision/epsilon setting that rounds down to 0.

Common situations: A 'precision' setting mapped to 0 iterations; default value unset resolving to 0; logic that decrements iterations until 0.

Related errors


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