{"record":{"id":"d482d63cd1273154","repo":"TheAlgorithms/Java","slug":"invalid-number-of-vertices-or-root","errorCode":null,"errorMessage":"Invalid number of vertices or root","messagePattern":"Invalid number of vertices or root","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/graph/Edmonds.java","lineNumber":72,"sourceCode":"            this.to = to;\n            this.weight = weight;\n        }\n    }\n\n    /**\n     * Computes the total weight of the Minimum Spanning Arborescence of a directed,\n     * weighted graph from a given root.\n     *\n     * @param numVertices the number of vertices, labeled {@code 0..numVertices-1}\n     * @param edges list of directed edges in the graph\n     * @param root the root vertex\n     * @return the total weight of the MSA. Returns -1 if not all vertices are reachable\n     *         from the root or if a valid arborescence cannot be formed.\n     * @throws IllegalArgumentException if {@code numVertices <= 0} or {@code root} is out of range.\n     */\n    public static long findMinimumSpanningArborescence(int numVertices, List<Edge> edges, int root) {\n        if (root < 0 || root >= numVertices) {\n            throw new IllegalArgumentException(\"Invalid number of vertices or root\");\n        }\n        if (numVertices == 1) {\n            return 0;\n        }\n\n        return findMSARecursive(numVertices, edges, root);\n    }\n\n    /**\n     * Recursive helper method for finding MSA.\n     */\n    private static long findMSARecursive(int n, List<Edge> edges, int root) {\n        long[] minWeightEdge = new long[n];\n        int[] predecessor = new int[n];\n        Arrays.fill(minWeightEdge, Long.MAX_VALUE);\n        Arrays.fill(predecessor, -1);\n\n        for (Edge edge : edges) {","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/graph/Edmonds.java#L54-L90","documentation":"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.","triggerScenarios":"Calling findMinimumSpanningArborescence(numVertices, edges, root) with root < 0, root >= numVertices, or numVertices <= 0 (since root 0 >= numVertices 0 triggers the same guard).","commonSituations":"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.","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)."],"exampleFix":"// before\nlong w = Edmonds.findMinimumSpanningArborescence(0, edges, 0); // throws\n\n// after\nif (numVertices <= 0) {\n    return 0;\n}\nlong w = Edmonds.findMinimumSpanningArborescence(numVertices, edges, root);","handlingStrategy":"validation","validationCode":"if (numVertices <= 0 || root < 0 || root >= numVertices) {\n    throw new IllegalArgumentException(\"numVertices must be > 0 and root in [0, numVertices-1]\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["graph-algorithm","argument-validation","index-bounds","minimum-spanning-arborescence"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}