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

Code before:
     */
    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) {
        List<Edge> result = new ArrayList<>();

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

        // Continue until the number of edges in the MST is V-1
        while (result.size() < graph.vertex - 1) {
            // Compute the cheapest edge for each subset
            final var cheapest = computeCheapestEdges(graph, components);

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

    /**
     * Initializes subsets for Union-Find
     *
     * @param graph the graph
     * @return the initialized subsets
     */
    private static Component[] initializeSubsets(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;
    }

Review comment (suggestion):
(not a must - just thinking loudly) Does it make sense to have some [`Hashtable`](https://docs.oracle.com/javase/8/docs/api/java/util/Hashtable.html), which maps vertices into components (i.e. `our_hashtabe[some_vertex]` is the number of component containing `some_vertex`).

Code after:
            for (int i = 0; i < graph.vertex; ++i) {
                if (cheapest[i] != null) {
                    final var component1 = find(components, cheapest[i].src);
                    final var component2 = find(components, cheapest[i].dest);

                    if (component1 != component2) {
                        result.add(cheapest[i]);
                        union(components, component1, component2);
                    }
                }
            }
        }

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