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

Code before:
import java.util.LinkedList;

/*
 *  The Welsh-Powell algorithm is a graph coloring algorithm
 *  used for coloring a graph with the minimum number of colors.
 *  https://en.wikipedia.org/wiki/Graph_coloring
 */
public class WelshPowell {

    public static class WPGraph {

        private int numVer; // Number of vertices in the graph
        public LinkedList<Integer>[] adjLists; // Adjacency lists for the graph

        // Constructor to initialize the graph with a certain number of vertices
        public WPGraph(int vertices) {
            numVer = vertices;
            adjLists = new LinkedList[vertices];

            for (int i = 0; i < vertices; i++) {
                adjLists[i] = new LinkedList<>();
            }
        }

        // Method to add an edge to the graph (undirected)
        public void addEdge(int src, int dest) {
            // Check to prevent duplicate edges
            if (!adjLists[src].contains(dest)) {
                adjLists[src].add(dest);
                adjLists[dest].add(src); // For undirected graphs, add the reverse edge as well
            }
        }

        // Method implementing Welsh Powell coloring algorithm
        public int[] welshPowellColoring() {
            int[] colors = new int[numVer]; // Array to store color of each vertex
            for (int i = 0; i < numVer; i++) {
                colors[i] = -1; // Initialize all vertices as uncolored
            }

            // Creating a list of vertices sorted by descending degree
            LinkedList<Integer> sortedVertices = new LinkedList<>();
            for (int i = 0; i < numVer; i++) {
                sortedVertices.add(i);
            }
            sortedVertices.sort((v1, v2) -> adjLists[v2].size() - adjLists[v1].size());

            // Coloring vertices
            for (int vertex : sortedVertices) {
                if (colors[vertex] == -1) {
                    boolean[] usedColors = new boolean[numVer]; // Track used colors

Review comment (suggestion):
I would suggest to rename this arguments. Current names somehow suggest that you add _directed edges_.

```suggestion
        public void addEdge(int nodeA, int nodeB) {
```

Code after:
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.stream.IntStream;

/*
 *  The Welsh-Powell algorithm is a graph coloring algorithm
 *  used for coloring a graph with the minimum number of colors.
 *  https://en.wikipedia.org/wiki/Graph_coloring
 */

public final class WelshPowell {
    private static final int BLANK_COLOR = -1; // Representing uncolored state

    private WelshPowell() {
    }

    static class Graph {
        private HashSet<Integer>[] adjacencyLists;

        private Graph(int vertices) {
            if (vertices < 0) {
                throw new IllegalArgumentException("Number of vertices cannot be negative");
            }

            adjacencyLists = new HashSet[vertices];
            Arrays.setAll(adjacencyLists, i -> new HashSet<>());
        }

        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;