TheAlgorithms/Java · error · IllegalArgumentException
Source vertex is out of bounds.
Error message
Source vertex is out of bounds.
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/datastructures/graphs/DialsAlgorithm.java:63
public int getWeight() {
return weight;
}
}
/**
* Finds the shortest paths from a source vertex to all other vertices in a weighted graph.
*
* @param graph The graph represented as an adjacency list.
* @param source The source vertex to start from (0-indexed).
* @param maxEdgeWeight The maximum weight of any single edge in the graph.
* @return An array of integers where the value at each index `i` is the
* shortest distance from the source to vertex `i`. Unreachable vertices
* will have a value of Integer.MAX_VALUE.
* @throws IllegalArgumentException if the source vertex is out of bounds.
*/
public static int[] run(List<List<Edge>> graph, int source, int maxEdgeWeight) {
int numVertices = graph.size();
if (source < 0 || source >= numVertices) {
throw new IllegalArgumentException("Source vertex is out of bounds.");
}
// Initialize distances array
int[] distances = new int[numVertices];
Arrays.fill(distances, Integer.MAX_VALUE);
distances[source] = 0;
// The bucket queue. Size is determined by the max possible path length.
int maxPathWeight = maxEdgeWeight * (numVertices > 0 ? numVertices - 1 : 0);
List<Set<Integer>> buckets = new ArrayList<>(maxPathWeight + 1);
for (int i = 0; i <= maxPathWeight; i++) {
buckets.add(new HashSet<>());
}
// Add the source vertex to the first bucket
buckets.get(0).add(source);
// Process buckets in increasing order of distanceView on GitHub (pinned to fdfb9a395b)
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
Example fix
// before
int[] d = DialsAlgorithm.run(graph, source, maxW);
// after
if (source < 0 || source >= graph.size()) throw new IllegalArgumentException("source");
int[] d = DialsAlgorithm.run(graph, source, maxW); Defensive patterns
Strategy: validation
Validate before calling
if (source < 0 || source >= graph.size()) {
throw new IllegalArgumentException("source out of bounds: " + source);
} Try / catch
try {
DialsAlgorithm.run(graph, source, maxW);
} catch (IllegalArgumentException e) {
// handle bad source
} Prevention
- Treat source as 0-indexed and validate against graph.size()
- Guard the empty-graph case before calling run
When it happens
Trigger: 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`.
Common situations: 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.
Related errors
- Edge vertex out of range
- Incorrect source
- Vertex {vertex} is out of bounds
- Number of vertices must be positive
- Edges list must not be null or empty
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/978a801bde77be1f.
Report an issue: GitHub.