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

Code before:
            subsets[yroot].parent = xroot;
        } else {
            subsets[yroot].parent = xroot;
            subsets[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(Graph graph) {
        List<Edge> result = new ArrayList<>();

        // Initialize subsets for Union-Find
        Subset[] subsets = initializeSubsets(graph);

        // Continue until the number of edges in the MST is V-1
        while (result.size() < graph.vertex - 1) {
            // Array to store the cheapest edge for each subset
            Edge[] cheapest = new Edge[graph.vertex];

            // Iterate through all edges and update the cheapest edge for each subset
            updateCheapestEdges(graph, subsets, cheapest);

            // Add the cheapest edges to the result and perform Union operation
            addCheapestEdgesAndUnion(graph, subsets, result, cheapest);
        }
        return result;
    }

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

Review comment (suggestion):
This part should look something like this:
```java
final var cheapest = computeCheapestEdges(graph, subset);
```

Code after:
                    }
                }
            }
        }

        /**
         * Checks if there are more edges to add to the result list
         *
         * @return true if there are more edges to add, false otherwise
         */
        boolean hasMoreEdgesToAdd() {
            return result.size() < graph.vertex - 1;
        }

        /**
         * Computes the cheapest edges for each subset in the Union-Find structure.
         *
         * @return an array containing the cheapest edge for each subset.
         */
        private Edge[] computeCheapestEdges() {
            Edge[] cheapest = new Edge[graph.vertex];
            for (final var edge : graph.edges) {
                final var set1 = find(components, edge.src);
                final var set2 = find(components, 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;
                    }
                }
            }
            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;
        }
    }