{"record":{"id":"7cf22029b7ad4ae8","repo":"TheAlgorithms/Java","slug":"edge-array-must-have-exactly-two-elements","errorCode":null,"errorMessage":"Edge array must have exactly two elements","messagePattern":"Edge array must have exactly two elements","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/graphs/WelshPowell.java","lineNumber":114,"sourceCode":"         */\n        int getNumVertices() {\n            return adjacencyLists.length;\n        }\n    }\n\n    /**\n     * Creates a graph with the specified number of vertices and edges.\n     *\n     * @param numberOfVertices the total number of vertices\n     * @param listOfEdges a 2D array representing edges where each inner array contains two vertex indices\n     * @return a Graph object representing the created graph\n     * @throws IllegalArgumentException if the edge array is invalid or vertices are out of bounds\n     */\n    public static Graph makeGraph(int numberOfVertices, int[][] listOfEdges) {\n        Graph graph = new Graph(numberOfVertices);\n        for (int[] edge : listOfEdges) {\n            if (edge.length != 2) {\n                throw new IllegalArgumentException(\"Edge array must have exactly two elements\");\n            }\n            graph.addEdge(edge[0], edge[1]);\n        }\n        return graph;\n    }\n\n    /**\n     * Finds the coloring of the given graph using the Welsh-Powell algorithm.\n     *\n     * @param graph the input graph to color\n     * @return an array of integers where each index represents a vertex and the value represents the color assigned\n     */\n    public static int[] findColoring(Graph graph) {\n        int[] colors = initializeColors(graph.getNumVertices());\n        Integer[] sortedVertices = getSortedNodes(graph);\n        for (int vertex : sortedVertices) {\n            if (isBlank(colors[vertex])) {\n                boolean[] usedColors = computeUsedColors(graph, vertex, colors);","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/graphs/WelshPowell.java#L96-L132","documentation":"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.","triggerScenarios":"Passing `listOfEdges` containing an array of length != 2, e.g. `{{0,1,5}}` (weighted triple), `{{0}}`, or `{}`.","commonSituations":"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.","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"],"exampleFix":"// before (weighted triples)\nint[][] edges = {{0, 1, 5}, {1, 2, 3}};\n// after (weights not used by Welsh-Powell)\nint[][] edges = {{0, 1}, {1, 2}};","handlingStrategy":"validation","validationCode":"for (int[] e : listOfEdges) {\n    if (e.length != 2) {\n        throw new IllegalArgumentException(\"edge must have length 2: \" + Arrays.toString(e));\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    WelshPowell.makeGraph(n, edges);\n} catch (IllegalArgumentException e) {\n    // malformed edge array\n}","preventionTips":["Validate edge-array shape at the import boundary","Confirm Welsh-Powell takes unweighted {u,v} pairs, not weighted triples"],"tags":["graph","graph-coloring","input-validation","data-shape"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}