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

Code before:

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

    private static boolean isAdjacentToColored(Graph graph, int vertex, int[] colors) {
        return graph.getAdjList(vertex).stream().anyMatch(otherVertex -> colors[otherVertex] != -1);
    }

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

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

Review comment (suggestion):
Would you agree on introducing a field `private static final int BLANK_COLOR = -1;`
and a method:
```java
private static boolean isBlank(int color) {
    return color == BLANK_COLOR;
}
```
which could be use instead of direct comparison to `-1`. There are several advantages. For example this makes the code more _maintainable_ (i.e. maybe in the future one would like to color the graph using strings).

Code after:
            }
        }

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

    private static boolean isBlank(int color) {
        return color == BLANK_COLOR;
    }

    private static boolean isAdjacentToColored(Graph graph, int vertex, int[] colors) {
        return graph.getAdjacencyList(vertex).stream().anyMatch(otherVertex -> !isBlank(colors[otherVertex]));
    }

    private static int[] initializeColors(int numberOfVertices) {
        int[] colors = new int[numberOfVertices];