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

        // Helper method to check if an edge exists (used for testing)
        boolean isEdgePresent(int v1, int v2) {
            return adjLists[v1].contains(v2);
        }
    }

    public static int[] welshPowellColoring(WPGraph graph) {
        int numVer = graph.getNumVertices();
        int[] colors = new int[numVer];
        for (int i = 0; i < numVer; i++) {
            colors[i] = -1; // Initialize all vertices as uncolored
        }

        LinkedList<Integer> sortedVertices = new LinkedList<>();
        for (int i = 0; i < numVer; i++) {
            sortedVertices.add(i);
        }
        sortedVertices.sort((v1, v2) -> graph.getAdjList(v2).size() - graph.getAdjList(v1).size());

        for (int vertex : sortedVertices) {
            if (colors[vertex] == -1) {
                boolean[] usedColors = new boolean[numVer]; // Track used colors
                for (int neighbor : graph.getAdjList(vertex)) {
                    if (colors[neighbor] != -1) {
                        usedColors[colors[neighbor]] = true; // Mark neighbor's color as used
                    }
                }

                // Assign the first unused color
                for (int color = 0; color < numVer; color++) {
                    if (!usedColors[color]) {
                        colors[vertex] = color;
                        break;
                    }
                }
            }
        }

        return colors; // Return the array of colors for each vertex
    }

    // Public static method to get the adjacency list of a vertex
    public static HashSet<Integer> getAdjList(WPGraph graph, int vertex) {

Review comment (suggestion):
```suggestion
        Integer[] sortedVertices = IntStream.range(0, numVer).boxed().sorted(Comparator.comparingInt(v -> - graph.getAdjList(v).size())).toArray(Integer[] ::new);
```

Make sure to
```java
import java.util.Comparator;
import java.util.stream.IntStream;
```

I think after this change, there is no need to:
```java
import java.util.LinkedList;
```

As above: I would suggest to add a `private` method `getSortedNodes`.

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