TheAlgorithms/Java · error · IllegalArgumentException
adjacencyList size must equal vertexCount
Error message
adjacencyList size must equal vertexCount
What it means
TarjanBridges.findBridges requires the adjacencyList size to exactly equal vertexCount because each vertex 0..vertexCount-1 must have a corresponding list entry it can index. A mismatch means the graph representation is internally inconsistent — the algorithm would either skip vertices or hit IndexOutOfBoundsException mid-DFS. The check fails fast up front instead of letting the DFS corrupt state.
Source
Thrown at src/main/java/com/thealgorithms/graph/TarjanBridges.java:57
* 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);
}
}
return bridges;View on GitHub (pinned to fdfb9a395b)
Solutions
- Verify adjacencyList.size() == vertexCount immediately before the call, e.g. assert adjacencyList.size() == vertexCount or an explicit if-check.
- Ensure every vertex in [0, vertexCount) has an entry — add empty lists for isolated vertices (Collections.nCopies(vertexCount, List.of()) as a starter).
- If you build the list by appending, derive vertexCount from the list: findBridges(adjacencyList.size(), adjacencyList).
- If the list legitimately has extra slots (e.g. 1-based indexing), strip the unused slot or shift to 0-based before calling.
Example fix
// before List<List<Integer>> adj = new ArrayList<>(); for (int v = 0; v <= n; v++) adj.add(neighbors(v)); // off-by-one TarjanBridges.findBridges(n, adj); // after List<List<Integer>> adj = new ArrayList<>(); for (int v = 0; v < n; v++) adj.add(neighbors(v)); // exactly n entries TarjanBridges.findBridges(adj.size(), adj);
Defensive patterns
Strategy: validation
Validate before calling
if (adjacencyList == null || adjacencyList.size() != vertexCount) {
throw new IllegalArgumentException(
"adjacencyList size (" + (adjacencyList == null ? "null" : adjacencyList.size())
+ ") must equal vertexCount (" + vertexCount + ")");
}
TarjanBridges.findBridges(vertexCount, adjacencyList); Prevention
- Derive vertexCount from adjacencyList.size() rather than tracking a separate variable that can drift.
- When building the list, add an entry for every vertex index including isolated ones (empty list).
- Use Collections.nCopies(vertexCount, new ArrayList<>()) to pre-size the list correctly before populating edges.
When it happens
Trigger: Calling findBridges(vertexCount, adjacencyList) where adjacencyList.size() != vertexCount — e.g. vertexCount=5 but the list has 4 or 6 entries, or passing a null adjacencyList. Also triggered when a graph builder adds/removes vertices after the count was captured.
Common situations: Building the adjacency list in a loop with an off-by-one (e.g. adding vertices 0..n-1 but initializing the list with n+1 slots), deserializing a graph where isolated vertices were omitted as empty lists, or refactoring vertex indexing (0-based vs 1-based) without updating the count.
Related errors
- Matrix must be square
- Weights matrix must not be null or empty
- Weights matrix must be square
- Weights must be -1 (no edge) or >= 0
- k must be >= 1
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/4bf2f1089ef18fc5.
Report an issue: GitHub.