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

Code before:
     */
    private static void updateCheapestEdges(Graph graph, Subset[] subsets, Edge[] cheapest) {
        for (Edge edge : graph.edges) {
            int set1 = find(subsets, edge.src);
            int set2 = find(subsets, edge.dest);

            if (set1 != set2) {
                if (cheapest[set1] == null || edge.weight < cheapest[set1].weight) {
                    cheapest[set1] = edge;
                }
                if (cheapest[set2] == null || edge.weight < cheapest[set2].weight) {
                    cheapest[set2] = edge;
                }
            }
        }
    }

    /**
     * Adds the cheapest edges to the result list and performs Union operation on the subsets.
     *
     * @param graph    the graph
     * @param subsets  Array of subsets used for Union-Find operations.
     * @param result   List to store the edges of the Minimum Spanning Tree.
     * @param cheapest Array containing the cheapest edge for each subset.
     */
    private static void addCheapestEdgesAndUnion(Graph graph, Subset[] subsets, List<Edge> result, Edge[] cheapest) {
        for (int i = 0; i < graph.vertex; ++i) {
            if (cheapest[i] != null) {
                int set1 = find(subsets, cheapest[i].src);
                int set2 = find(subsets, cheapest[i].dest);

                if (set1 != set2) {
                    result.add(cheapest[i]);
                    union(subsets, set1, set2);
                }
            }
        }
    }

    /**
     * Checks if the edge vertices are in a valid range
     *
     * @param vertex     the vertex to check
     * @param upperBound the upper bound for the vertex range
     */
    private static void checkEdgeVertices(int vertex, int upperBound) {
        if (vertex < 0 || vertex >= upperBound) {
            throw new IllegalArgumentException("Edge vertex out of range");
        }
    }
}

Review comment (suggestion):
```suggestion
    private static void addCheapestEdgesAndUnion(final Graph graph, final Subset[] subsets, List<Edge> result, final Edge[] cheapest) {
```

Code after:
     * Finds the parent of the subset using path compression
     *
     * @param components array of subsets
     * @param i          index of the subset
     * @return the parent of the subset
     */
    static int find(final Component[] components, final int i) {
        if (components[i].parent != i) {
            components[i].parent = find(components, components[i].parent);
        }
        return components[i].parent;
    }

    /**
     * Performs the Union operation for Union-Find
     *
     * @param components array of subsets
     * @param x          index of the first subset
     * @param y          index of the second subset
     */
    static void union(Component[] components, final int x, final int y) {
        final int xroot = find(components, x);
        final int yroot = find(components, y);

        if (components[xroot].rank < components[yroot].rank) {
            components[xroot].parent = yroot;
        } else if (components[xroot].rank > components[yroot].rank) {
            components[yroot].parent = xroot;
        } else {
            components[yroot].parent = xroot;
            components[xroot].rank++;
        }
    }

    /**
     * Boruvka's algorithm to find the Minimum Spanning Tree
     *
     * @param graph the graph
     * @return list of edges in the Minimum Spanning Tree
     */
    static List<Edge> boruvkaMST(final Graph graph) {
        var boruvkaState = new BoruvkaState(graph);

        while (boruvkaState.hasMoreEdgesToAdd()) {
            final var cheapest = boruvkaState.computeCheapestEdges();
            boruvkaState.merge(cheapest);
        }
        return boruvkaState.result;
    }

    /**