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
- 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.
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
- Use 0 (not -1) for empty graphs.
- Check parse results for -1 sentinels before passing.
- Validate adjacencyList size matches vertexCount.
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
- Capacities must be non-negative
- Capacities must be non-negative
- Costs must be non-negative
- Capacities must be non-negative
- Source and sink must be valid vertex indices
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/320b8754861b7fe1.
Report an issue: GitHub.