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

Code before:

            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;
    }

    /**
     * Adds the cheapest edges to the result list and performs Union operation on the subsets.
     *
     * @param graph      the graph
     * @param components 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 merge(final Graph graph, final Component[] components, List<Edge> result, final Edge[] cheapest) {
        for (int i = 0; i < graph.vertex; ++i) {
            if (cheapest[i] != null) {
                int set1 = find(components, cheapest[i].src);
                int set2 = find(components, cheapest[i].dest);

                if (set1 != set2) {
                    result.add(cheapest[i]);
                    union(components, 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(final int vertex, final int upperBound) {
        if (vertex < 0 || vertex >= upperBound) {
            throw new IllegalArgumentException("Edge vertex out of range");
        }
    }
}


Review comment (suggestion):
```suggestion
                final var set1 = find(components, cheapest[i].src);
                final var set2 = find(components, cheapest[i].dest);
```

Code after:
     * @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;
    }

    /**
     * Checks if the edge vertices are in a valid range
     *
     * @param vertex     the vertex to check