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

Code before:

import java.util.HashSet;
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
        private HashSet<Integer>[] adjLists; // Adjacency lists for the graph using HashSet

        public WPGraph(int vertices) {
            numVer = vertices;
            adjLists = new HashSet[vertices];
            for (int i = 0; i < vertices; i++) {
                adjLists[i] = new HashSet<>();
            }
        }

        public void addEdge(int nodeA, int nodeB) {
            adjLists[nodeA].add(nodeB);
            adjLists[nodeB].add(nodeA);
        }

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

Review comment (suggestion):
I would suggest at lest that:
```suggestion
            assert (0 <= nodeA && nodeA <= numVer);
            assert (0 <= nodeB && nodeB <= numVer);
            adjLists[nodeA].add(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() {