{"record":{"id":"9798d55ce489d86b","repo":"TheAlgorithms/Java","slug":"edge-vertex-out-of-range","errorCode":null,"errorMessage":"Edge vertex out of range","messagePattern":"Edge vertex out of range","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithm.java","lineNumber":214,"sourceCode":"    static List<Edge> boruvkaMST(final Graph graph) {\n        var boruvkaState = new BoruvkaState(graph);\n\n        while (boruvkaState.hasMoreEdgesToAdd()) {\n            final var cheapest = boruvkaState.computeCheapestEdges();\n            boruvkaState.merge(cheapest);\n        }\n        return boruvkaState.result;\n    }\n\n    /**\n     * Checks if the edge vertices are in a valid range\n     *\n     * @param vertex     the vertex to check\n     * @param upperBound the upper bound for the vertex range\n     */\n    private static void checkEdgeVertices(final int vertex, final int upperBound) {\n        if (vertex < 0 || vertex >= upperBound) {\n            throw new IllegalArgumentException(\"Edge vertex out of range\");\n        }\n    }\n}\n","sourceCodeStart":196,"sourceCodeEnd":218,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithm.java#L196-L218","documentation":"Thrown by `checkEdgeVertices` during Graph construction when any edge's `src` or `dest` is negative or `>= vertex`. Every edge endpoint must index a real vertex in `[0, vertex)`. This catches malformed or off-by-one edge definitions before they corrupt the MST computation.","triggerScenarios":"An Edge whose `src`/`dest` equals `vertex` (1-based index passed to a 0-based graph), is negative, or references a vertex that does not exist because the vertex count was set too low.","commonSituations":"Mixing 1-based file data with the 0-based API; vertex count smaller than the max referenced vertex; an edge list built for a larger graph but a smaller `vertex` passed.","solutions":["Make vertex indices 0-based and ensure all src/dest are in [0, vertex)","If your data is 1-based, subtract 1 from every endpoint before building Edges","Set `vertex` to at least `max(src,dest)+1` across all edges"],"exampleFix":"// before (1-based data passed directly)\nedges.add(new Edge(1, vertex, w));\n// after (convert 1-based to 0-based)\nedges.add(new Edge(src - 1, dest - 1, w));","handlingStrategy":"validation","validationCode":"for (Edge e : edges) {\n    if (e.src < 0 || e.src >= vertex || e.dest < 0 || e.dest >= vertex) {\n        throw new IllegalArgumentException(\"bad edge \" + e.src + \"-\" + e.dest);\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    new BoruvkaAlgorithm.Graph(v, edges);\n} catch (IllegalArgumentException e) {\n    // log the offending edge\n}","preventionTips":["Normalize external edge data to 0-based at the boundary","Derive the vertex count from the maximum referenced endpoint, do not guess it"],"tags":["graph","indexing","input-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}