TheAlgorithms/Java · error · IllegalArgumentException
Invalid number of vertices or root
Error message
Invalid number of vertices or root
What it means
Edmonds.findMinimumSpanningArborescence throws this IllegalArgumentException when the root vertex is outside [0, numVertices). Despite the message mentioning 'number of vertices', the guard only checks root bounds; a numVertices of 0 combined with root 0 is the typical trigger. The method then delegates to the recursive MSA computation only if root is valid and numVertices > 1.
Source
Thrown at src/main/java/com/thealgorithms/graph/Edmonds.java:72
this.to = to;
this.weight = weight;
}
}
/**
* Computes the total weight of the Minimum Spanning Arborescence of a directed,
* weighted graph from a given root.
*
* @param numVertices the number of vertices, labeled {@code 0..numVertices-1}
* @param edges list of directed edges in the graph
* @param root the root vertex
* @return the total weight of the MSA. Returns -1 if not all vertices are reachable
* from the root or if a valid arborescence cannot be formed.
* @throws IllegalArgumentException if {@code numVertices <= 0} or {@code root} is out of range.
*/
public static long findMinimumSpanningArborescence(int numVertices, List<Edge> edges, int root) {
if (root < 0 || root >= numVertices) {
throw new IllegalArgumentException("Invalid number of vertices or root");
}
if (numVertices == 1) {
return 0;
}
return findMSARecursive(numVertices, edges, root);
}
/**
* Recursive helper method for finding MSA.
*/
private static long findMSARecursive(int n, List<Edge> edges, int root) {
long[] minWeightEdge = new long[n];
int[] predecessor = new int[n];
Arrays.fill(minWeightEdge, Long.MAX_VALUE);
Arrays.fill(predecessor, -1);
for (Edge edge : edges) {View on GitHub (pinned to fdfb9a395b)
Solutions
- Ensure root is in [0, numVertices - 1] and numVertices > 0 before calling.
- If the graph is empty (numVertices == 0), skip the call or handle it explicitly instead of relying on the algorithm.
- Convert external 1-based root labels to 0-based.
- Validate that edge endpoints also fall within [0, numVertices).
Example fix
// before
long w = Edmonds.findMinimumSpanningArborescence(0, edges, 0); // throws
// after
if (numVertices <= 0) {
return 0;
}
long w = Edmonds.findMinimumSpanningArborescence(numVertices, edges, root); Defensive patterns
Strategy: validation
Validate before calling
if (numVertices <= 0 || root < 0 || root >= numVertices) {
throw new IllegalArgumentException("numVertices must be > 0 and root in [0, numVertices-1]");
} Prevention
- Treat numVertices == 0 as a special case before calling.
- Convert 1-based root labels to 0-based.
- Validate edge endpoints are within [0, numVertices) as well.
When it happens
Trigger: Calling findMinimumSpanningArborescence(numVertices, edges, root) with root < 0, root >= numVertices, or numVertices <= 0 (since root 0 >= numVertices 0 triggers the same guard).
Common situations: Passing numVertices = 0 for an empty graph with root 0 (0 >= 0 is true). Off-by-one on vertex count. Using a root id sourced from external 1-based labeling without conversion.
Related errors
- Source and sink must be valid vertex indices
- Source and sink must be valid vertex indices
- Source and sink must be valid vertex indices
- Capacity matrix must not be null or empty
- Capacity matrix must be square
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/d482d63cd1273154.
Report an issue: GitHub.