{"record":{"id":"929d0126979a3dfd","repo":"TheAlgorithms/Java","slug":"incorrect-source","errorCode":null,"errorMessage":"Incorrect source","messagePattern":"Incorrect source","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/graphs/DijkstraAlgorithm.java","lineNumber":50,"sourceCode":"        public int compareTo(Node other) {\n            return Integer.compare(this.distance, other.distance);\n        }\n    }\n\n    /**\n     * Executes Dijkstra's algorithm on the provided graph to find the shortest paths from the source vertex to all other vertices.\n     *\n     * The graph is represented as an adjacency matrix where {@code graph[i][j]} represents the weight of the edge from vertex {@code i}\n     * to vertex {@code j}. A value of 0 indicates no edge exists between the vertices.\n     *\n     * @param graph The graph represented as an adjacency matrix.\n     * @param source The source vertex.\n     * @return An array where the value at each index {@code i} represents the shortest distance from the source vertex to vertex {@code i}.\n     * @throws IllegalArgumentException if the source vertex is out of range.\n     */\n    public int[] run(int[][] graph, int source) {\n        if (source < 0 || source >= vertexCount) {\n            throw new IllegalArgumentException(\"Incorrect source\");\n        }\n\n        int[] distances = new int[vertexCount];\n        boolean[] processed = new boolean[vertexCount];\n        PriorityQueue<Node> unprocessed = new PriorityQueue<>();\n\n        Arrays.fill(distances, Integer.MAX_VALUE);\n        distances[source] = 0;\n        unprocessed.add(new Node(source, 0));\n\n        while (!unprocessed.isEmpty()) {\n            Node current = unprocessed.poll();\n            int u = current.id;\n\n            if (processed[u]) {\n                continue;\n            }\n            processed[u] = true;","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/graphs/DijkstraAlgorithm.java#L32-L68","documentation":"Thrown by `DijkstraAlgorithm.run` when `source` is negative or `>= vertexCount`. Here `vertexCount` is the value passed to the `DijkstraAlgorithm(int)` constructor, NOT the dimensions of the `int[][] graph` matrix. The check ensures the source indexes a valid vertex before allocating the distance array.","triggerScenarios":"Constructing `new DijkstraAlgorithm(n)` with one vertex count, then calling `run(matrix, source)` with `source >= n` or a negative source. Also when `vertexCount` and the matrix's actual row count disagree and `source` falls between them.","commonSituations":"Mismatch between the constructor's `vertexCount` and the matrix dimensions; 1-based source; reusing one DijkstraAlgorithm instance for graphs of different sizes.","solutions":["Pass the same vertex count to the constructor as the matrix dimension, and keep `0 <= source < vertexCount`","Construct a fresh DijkstraAlgorithm per graph size","Validate source against `graph.length` before calling run"],"exampleFix":"// before\nDijkstraAlgorithm d = new DijkstraAlgorithm(5);\nd.run(matrix, 6); // throws\n// after\nDijkstraAlgorithm d = new DijkstraAlgorithm(matrix.length);\nd.run(matrix, source); // source validated to be in [0, matrix.length)","handlingStrategy":"validation","validationCode":"if (source < 0 || source >= vertexCount) {\n    throw new IllegalArgumentException(\"source out of range\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    dijkstra.run(matrix, source);\n} catch (IllegalArgumentException e) {\n    // handle bad source\n}","preventionTips":["Keep vertexCount consistent with the matrix dimension","Do not reuse a DijkstraAlgorithm instance across differently-sized graphs"],"tags":["graph","shortest-path","input-validation","argument-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}