{"record":{"id":"978a801bde77be1f","repo":"TheAlgorithms/Java","slug":"source-vertex-is-out-of-bounds","errorCode":null,"errorMessage":"Source vertex is out of bounds.","messagePattern":"Source vertex is out of bounds\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/graphs/DialsAlgorithm.java","lineNumber":63,"sourceCode":"        public int getWeight() {\n            return weight;\n        }\n    }\n    /**\n     * Finds the shortest paths from a source vertex to all other vertices in a weighted graph.\n     *\n     * @param graph The graph represented as an adjacency list.\n     * @param source The source vertex to start from (0-indexed).\n     * @param maxEdgeWeight The maximum weight of any single edge in the graph.\n     * @return An array of integers where the value at each index `i` is the\n     * shortest distance from the source to vertex `i`. Unreachable vertices\n     * will have a value of Integer.MAX_VALUE.\n     * @throws IllegalArgumentException if the source vertex is out of bounds.\n     */\n    public static int[] run(List<List<Edge>> graph, int source, int maxEdgeWeight) {\n        int numVertices = graph.size();\n        if (source < 0 || source >= numVertices) {\n            throw new IllegalArgumentException(\"Source vertex is out of bounds.\");\n        }\n\n        // Initialize distances array\n        int[] distances = new int[numVertices];\n        Arrays.fill(distances, Integer.MAX_VALUE);\n        distances[source] = 0;\n\n        // The bucket queue. Size is determined by the max possible path length.\n        int maxPathWeight = maxEdgeWeight * (numVertices > 0 ? numVertices - 1 : 0);\n        List<Set<Integer>> buckets = new ArrayList<>(maxPathWeight + 1);\n        for (int i = 0; i <= maxPathWeight; i++) {\n            buckets.add(new HashSet<>());\n        }\n\n        // Add the source vertex to the first bucket\n        buckets.get(0).add(source);\n\n        // Process buckets in increasing order of distance","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/graphs/DialsAlgorithm.java#L45-L81","documentation":"Thrown by `DialsAlgorithm.run` when `source` is negative or `>= graph.size()`. Dial's algorithm is a single-source shortest-path method that indexes distances by vertex, so the source must reference an existing vertex in the adjacency list. The source is 0-indexed to match the list structure.","triggerScenarios":"Calling `run(graph, source, maxEdgeWeight)` with a source outside `[0, graph.size())` — e.g. a 1-based source, or an adjacency list built with fewer entries than expected so `graph.size()` is smaller than `source`.","commonSituations":"1-based vs 0-based confusion; an empty adjacency list (`graph.size()==0`) with source 0; source from config that exceeds vertex count after the graph was filtered.","solutions":["Validate `0 <= source < graph.size()` before calling run","If your source is 1-based, subtract 1","Ensure the adjacency list has one entry per vertex so `graph.size()` matches the real vertex count"],"exampleFix":"// before\nint[] d = DialsAlgorithm.run(graph, source, maxW);\n// after\nif (source < 0 || source >= graph.size()) throw new IllegalArgumentException(\"source\");\nint[] d = DialsAlgorithm.run(graph, source, maxW);","handlingStrategy":"validation","validationCode":"if (source < 0 || source >= graph.size()) {\n    throw new IllegalArgumentException(\"source out of bounds: \" + source);\n}","typeGuard":null,"tryCatchPattern":"try {\n    DialsAlgorithm.run(graph, source, maxW);\n} catch (IllegalArgumentException e) {\n    // handle bad source\n}","preventionTips":["Treat source as 0-indexed and validate against graph.size()","Guard the empty-graph case before calling run"],"tags":["graph","shortest-path","indexing","input-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}