TheAlgorithms/Java · error · IllegalArgumentException

vertexCount must be non-negative

Error message

vertexCount must be non-negative

What it means

TarjanBridges.findBridges throws this IllegalArgumentException when vertexCount is negative. It is the first guard; a negative vertex count would break array allocation and iteration throughout the bridge-finding DFS. An empty graph (vertexCount == 0) is valid and returns an empty list.

Source

Thrown at src/main/java/com/thealgorithms/graph/TarjanBridges.java:54

     * Finds all bridge edges in an undirected graph.
     *
     * <p>The graph is represented as an adjacency list where each vertex is identified by
     * an integer in the range {@code [0, vertexCount)}. For each undirected edge (u, v),
     * v must appear in {@code adjacencyList.get(u)} and u must appear in
     * {@code adjacencyList.get(v)}.</p>
     *
     * @param vertexCount   the total number of vertices in the graph (must be non-negative)
     * @param adjacencyList the adjacency list representation of the graph; must contain
     *                      exactly {@code vertexCount} entries (one per vertex)
     * @return a list of bridge edges, where each bridge is represented as an {@code int[]}
     *         of length 2 with {@code edge[0] < edge[1]}; returns an empty list if no bridges exist
     * @throws IllegalArgumentException if {@code vertexCount} is negative, or if
     *                                  {@code adjacencyList} is null or its size does not match
     *                                  {@code vertexCount}
     */
    public static List<int[]> findBridges(int vertexCount, List<List<Integer>> adjacencyList) {
        if (vertexCount < 0) {
            throw new IllegalArgumentException("vertexCount must be non-negative");
        }
        if (adjacencyList == null || adjacencyList.size() != vertexCount) {
            throw new IllegalArgumentException("adjacencyList size must equal vertexCount");
        }

        List<int[]> bridges = new ArrayList<>();

        if (vertexCount == 0) {
            return bridges;
        }

        BridgeFinder finder = new BridgeFinder(vertexCount, adjacencyList, bridges);

        // Run DFS from every unvisited vertex to handle disconnected graphs
        for (int i = 0; i < vertexCount; i++) {
            if (!finder.visited[i]) {
                finder.dfs(i, -1);
            }

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Guard: if (vertexCount < 0) handle as an error or return an empty list.
  2. Use 0 (not -1) to represent an empty graph.
  3. Validate parse results before passing to findBridges.

Example fix

// before
List<int[]> bridges = TarjanBridges.findBridges(parsedCount, adj);

// after
if (parsedCount < 0) {
    throw new IllegalStateException("Parse failed: invalid vertex count");
}
List<int[]> bridges = TarjanBridges.findBridges(parsedCount, adj);
Defensive patterns

Strategy: validation

Validate before calling

if (vertexCount < 0) {
    throw new IllegalArgumentException("vertexCount must be >= 0");
}

Type guard

boolean validVertexCount(int vc) { return vc >= 0; }

Prevention

When it happens

Trigger: Calling findBridges(vertexCount, adjacencyList) with vertexCount < 0.

Common situations: Vertex count derived from a failed parse returning -1. Arithmetic that subtracts and underflows. A 'not found' sentinel (-1) passed through without checking.

Related errors


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