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

Code before:

    /**
     * Initializes subsets for Union-Find
     *
     * @param graph the graph
     * @return the initialized subsets
     */
    private static Subset[] initializeSubsets(Graph graph) {
        Subset[] subsets = new Subset[graph.vertex];
        for (int v = 0; v < graph.vertex; ++v) {
            subsets[v] = new Subset(v, 0);
        }
        return subsets;
    }

    /**
     * Updates the cheapest edge for each subset based on the given graph and subsets
     *
     * @param graph    the graph
     * @param subsets  array of subsets
     * @param cheapest array to store the cheapest edge for each subset
     */
    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);

Review comment (suggestion):
```suggestion
    private static void updateCheapestEdges(final Graph graph, final Subset[] subsets, Edge[] cheapest) {
        for (final var edge : graph.edges) {
            final var set1 = find(subsets, edge.src);
            final var set2 = find(subsets, edge.dest);
```

Code after:
                }
            }
            return cheapest;
        }

        /**
         * Initializes subsets for Union-Find
         *
         * @param graph the graph
         * @return the initialized subsets
         */
        private static Component[] initializeComponents(final Graph graph) {
            Component[] components = new Component[graph.vertex];
            for (int v = 0; v < graph.vertex; ++v) {
                components[v] = new Component(v, 0);
            }
            return components;
        }
    }

    /**
     * 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;