TheAlgorithms/Java · error · IllegalArgumentException
Incorrect source
Error message
Incorrect source
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/datastructures/graphs/DijkstraAlgorithm.java:50
public int compareTo(Node other) {
return Integer.compare(this.distance, other.distance);
}
}
/**
* Executes Dijkstra's algorithm on the provided graph to find the shortest paths from the source vertex to all other vertices.
*
* The graph is represented as an adjacency matrix where {@code graph[i][j]} represents the weight of the edge from vertex {@code i}
* to vertex {@code j}. A value of 0 indicates no edge exists between the vertices.
*
* @param graph The graph represented as an adjacency matrix.
* @param source The source vertex.
* @return An array where the value at each index {@code i} represents the shortest distance from the source vertex to vertex {@code i}.
* @throws IllegalArgumentException if the source vertex is out of range.
*/
public int[] run(int[][] graph, int source) {
if (source < 0 || source >= vertexCount) {
throw new IllegalArgumentException("Incorrect source");
}
int[] distances = new int[vertexCount];
boolean[] processed = new boolean[vertexCount];
PriorityQueue<Node> unprocessed = new PriorityQueue<>();
Arrays.fill(distances, Integer.MAX_VALUE);
distances[source] = 0;
unprocessed.add(new Node(source, 0));
while (!unprocessed.isEmpty()) {
Node current = unprocessed.poll();
int u = current.id;
if (processed[u]) {
continue;
}
processed[u] = true;View on GitHub (pinned to fdfb9a395b)
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
Example fix
// before DijkstraAlgorithm d = new DijkstraAlgorithm(5); d.run(matrix, 6); // throws // after DijkstraAlgorithm d = new DijkstraAlgorithm(matrix.length); d.run(matrix, source); // source validated to be in [0, matrix.length)
Defensive patterns
Strategy: validation
Validate before calling
if (source < 0 || source >= vertexCount) {
throw new IllegalArgumentException("source out of range");
} Try / catch
try {
dijkstra.run(matrix, source);
} catch (IllegalArgumentException e) {
// handle bad source
} Prevention
- Keep vertexCount consistent with the matrix dimension
- Do not reuse a DijkstraAlgorithm instance across differently-sized graphs
When it happens
Trigger: 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.
Common situations: Mismatch between the constructor's `vertexCount` and the matrix dimensions; 1-based source; reusing one DijkstraAlgorithm instance for graphs of different sizes.
Related errors
- Number of vertices must be positive
- Source vertex is out of bounds.
- Edges list must not be null or empty
- Edge vertex out of range
- Graph contains a negative weight cycle
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/929d0126979a3dfd.
Report an issue: GitHub.