{"record":{"id":"320b8754861b7fe1","repo":"TheAlgorithms/Java","slug":"vertexcount-must-be-non-negative","errorCode":null,"errorMessage":"vertexCount must be non-negative","messagePattern":"vertexCount must be non-negative","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/graph/TarjanBridges.java","lineNumber":54,"sourceCode":"     * Finds all bridge edges in an undirected graph.\n     *\n     * <p>The graph is represented as an adjacency list where each vertex is identified by\n     * an integer in the range {@code [0, vertexCount)}. For each undirected edge (u, v),\n     * v must appear in {@code adjacencyList.get(u)} and u must appear in\n     * {@code adjacencyList.get(v)}.</p>\n     *\n     * @param vertexCount   the total number of vertices in the graph (must be non-negative)\n     * @param adjacencyList the adjacency list representation of the graph; must contain\n     *                      exactly {@code vertexCount} entries (one per vertex)\n     * @return a list of bridge edges, where each bridge is represented as an {@code int[]}\n     *         of length 2 with {@code edge[0] < edge[1]}; returns an empty list if no bridges exist\n     * @throws IllegalArgumentException if {@code vertexCount} is negative, or if\n     *                                  {@code adjacencyList} is null or its size does not match\n     *                                  {@code vertexCount}\n     */\n    public static List<int[]> findBridges(int vertexCount, List<List<Integer>> adjacencyList) {\n        if (vertexCount < 0) {\n            throw new IllegalArgumentException(\"vertexCount must be non-negative\");\n        }\n        if (adjacencyList == null || adjacencyList.size() != vertexCount) {\n            throw new IllegalArgumentException(\"adjacencyList size must equal vertexCount\");\n        }\n\n        List<int[]> bridges = new ArrayList<>();\n\n        if (vertexCount == 0) {\n            return bridges;\n        }\n\n        BridgeFinder finder = new BridgeFinder(vertexCount, adjacencyList, bridges);\n\n        // Run DFS from every unvisited vertex to handle disconnected graphs\n        for (int i = 0; i < vertexCount; i++) {\n            if (!finder.visited[i]) {\n                finder.dfs(i, -1);\n            }","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/graph/TarjanBridges.java#L36-L72","documentation":"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.","triggerScenarios":"Calling findBridges(vertexCount, adjacencyList) with vertexCount < 0.","commonSituations":"Vertex count derived from a failed parse returning -1. Arithmetic that subtracts and underflows. A 'not found' sentinel (-1) passed through without checking.","solutions":["Guard: if (vertexCount < 0) handle as an error or return an empty list.","Use 0 (not -1) to represent an empty graph.","Validate parse results before passing to findBridges."],"exampleFix":"// before\nList<int[]> bridges = TarjanBridges.findBridges(parsedCount, adj);\n\n// after\nif (parsedCount < 0) {\n    throw new IllegalStateException(\"Parse failed: invalid vertex count\");\n}\nList<int[]> bridges = TarjanBridges.findBridges(parsedCount, adj);","handlingStrategy":"validation","validationCode":"if (vertexCount < 0) {\n    throw new IllegalArgumentException(\"vertexCount must be >= 0\");\n}","typeGuard":"boolean validVertexCount(int vc) { return vc >= 0; }","tryCatchPattern":null,"preventionTips":["Use 0 (not -1) for empty graphs.","Check parse results for -1 sentinels before passing.","Validate adjacencyList size matches vertexCount."],"tags":["graph-algorithm","argument-validation","value-constraint","bridge-finding"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}