TheAlgorithms/Java · error · IllegalArgumentException
Edge array must have exactly two elements
Error message
Edge array must have exactly two elements
What it means
Thrown by `WelshPowell.makeGraph` when an inner edge array does not have exactly two elements. Each edge must be a `{src, dest}` pair; arrays of other lengths (1, 3, 0) are malformed and rejected before `addEdge` is called.
Source
Thrown at src/main/java/com/thealgorithms/datastructures/graphs/WelshPowell.java:114
*/
int getNumVertices() {
return adjacencyLists.length;
}
}
/**
* Creates a graph with the specified number of vertices and edges.
*
* @param numberOfVertices the total number of vertices
* @param listOfEdges a 2D array representing edges where each inner array contains two vertex indices
* @return a Graph object representing the created graph
* @throws IllegalArgumentException if the edge array is invalid or vertices are out of bounds
*/
public static Graph makeGraph(int numberOfVertices, int[][] listOfEdges) {
Graph graph = new Graph(numberOfVertices);
for (int[] edge : listOfEdges) {
if (edge.length != 2) {
throw new IllegalArgumentException("Edge array must have exactly two elements");
}
graph.addEdge(edge[0], edge[1]);
}
return graph;
}
/**
* Finds the coloring of the given graph using the Welsh-Powell algorithm.
*
* @param graph the input graph to color
* @return an array of integers where each index represents a vertex and the value represents the color assigned
*/
public static int[] findColoring(Graph graph) {
int[] colors = initializeColors(graph.getNumVertices());
Integer[] sortedVertices = getSortedNodes(graph);
for (int vertex : sortedVertices) {
if (isBlank(colors[vertex])) {
boolean[] usedColors = computeUsedColors(graph, vertex, colors);View on GitHub (pinned to fdfb9a395b)
Solutions
- Strip edge data to exactly two indices per edge (drop the weight column)
- Validate each inner array's length is 2 before calling makeGraph
- Fix the upstream parsing that produced wrong-length arrays
Example fix
// before (weighted triples)
int[][] edges = {{0, 1, 5}, {1, 2, 3}};
// after (weights not used by Welsh-Powell)
int[][] edges = {{0, 1}, {1, 2}}; Defensive patterns
Strategy: validation
Validate before calling
for (int[] e : listOfEdges) {
if (e.length != 2) {
throw new IllegalArgumentException("edge must have length 2: " + Arrays.toString(e));
}
} Try / catch
try {
WelshPowell.makeGraph(n, edges);
} catch (IllegalArgumentException e) {
// malformed edge array
} Prevention
- Validate edge-array shape at the import boundary
- Confirm Welsh-Powell takes unweighted {u,v} pairs, not weighted triples
When it happens
Trigger: Passing `listOfEdges` containing an array of length != 2, e.g. `{{0,1,5}}` (weighted triple), `{{0}}`, or `{}`.
Common situations: Passing weighted edge data `{u,v,w}` to a function that expects unweighted pairs; jagged/malformed input; mis-parsing CSV rows into wrong-length arrays.
Related errors
- Number of vertices cannot be negative
- Self-loops are not allowed
- 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/7cf22029b7ad4ae8.
Report an issue: GitHub.