Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/datastructures/graphs/WelshPowell.java (Java)

Code before:
        }

        public HashSet<Integer> getAdjList(int vertex) {
            return adjLists[vertex];
        }

        public int getNumVertices() {
            return numVer;
        }
    }

    public static int[] welshPowellColoring(WPGraph graph) {
        int numVer = graph.getNumVertices();
        int[] colors = initializeColors(numVer); // Use helper method to initialize color array
        Integer[] sortedVertices = getSortedNodes(graph, numVer); // Use helper method to get sorted vertices

        for (int vertex : sortedVertices) {
            if (colors[vertex] != -1) {
                continue;
            }
            boolean[] usedColors = computeUsedColors(graph, vertex, colors, numVer); // Use helper method to compute used colors
            colors[vertex] = firstUnusedColor(usedColors, numVer); // Use helper method to find first unused color
        }

        return colors;
    }

    private static int[] initializeColors(int numVer) {
        int[] colors = new int[numVer];
        Arrays.fill(colors, -1);
        return colors;
    }

    private static Integer[] getSortedNodes(WPGraph graph, int numVer) {
        return IntStream.range(0, numVer).boxed().sorted(Comparator.comparingInt(v -> - graph.getAdjList(v).size())).toArray(Integer[] ::new);
    }

    private static boolean[] computeUsedColors(WPGraph graph, int vertex, int[] colors, int numVer) {
        boolean[] usedColors = new boolean[numVer];
        graph.getAdjList(vertex).stream().map(neighbor -> colors[neighbor]).filter(color -> color != -1).forEach(color -> usedColors[color] = true);
        return usedColors;
    }

    private static int firstUnusedColor(boolean[] usedColors, int numVer) {
        return IntStream.range(0, numVer).filter(color -> !usedColors[color]).findFirst().getAsInt();
    }

    // Public static methods for testing purpose
    public static WPGraph createGraph(int vertices) {
        return new WPGraph(vertices);
    }

Review comment (suggestion):
```suggestion
    public static int[] welshPowellColoring(WPGraph graph) {
        int[] colors = initializeColors(graph.getNumVertices());
        final Integer[] sortedVertices = getSortedNodes(graph);
        for (int vertex : sortedVertices) {
            if (colors[vertex] == -1) {
                boolean[] usedColors = computeUsedColors(graph, vertex, colors);
                colors[vertex] = firstUnusedColor(usedColors);
            }
        }

        return colors;
    }
```

Code after:

        private void addEdge(int nodeA, int nodeB) {
            validateVertex(nodeA);
            validateVertex(nodeB);
            if (nodeA == nodeB) {
                throw new IllegalArgumentException("Self-loops are not allowed");
            }
            adjacencyLists[nodeA].add(nodeB);
            adjacencyLists[nodeB].add(nodeA);
        }

        private void validateVertex(int vertex) {
            if (vertex < 0 || vertex >= getNumVertices()) {
                throw new IllegalArgumentException("Vertex " + vertex + " is out of bounds");
            }
        }

        HashSet<Integer> getAdjacencyList(int vertex) {
            return adjacencyLists[vertex];
        }

        int getNumVertices() {
            return adjacencyLists.length;
        }
    }

    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;
    }

    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);
                final var newColor = firstUnusedColor(usedColors);
                colors[vertex] = newColor;
                Arrays.stream(sortedVertices).forEach(otherVertex -> {
                    if (isBlank(colors[otherVertex]) && !isAdjacentToColored(graph, otherVertex, colors)) {
                        colors[otherVertex] = newColor;
                    }
                });
            }