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

Code before:
        final int Vertex;
        final int Edge;
        List<Edge> edges;

        /**
         * Constructor for the graph
         *
         * @param Vertex number of vertices
         * @param Edge number of edges
         */
        Graph(int Vertex, int Edge) {
            this.Vertex = Vertex;
            this.Edge = Edge;
            this.edges = new ArrayList<>();
        }

        /**
         * Add an edge to the graph
         *
         * @param src    source vertex
         * @param dest   destination vertex
         * @param weight weight of the edge
         */
        void addEdge(int src, int dest, int weight) {
            edges.add(new Edge(src, dest, weight));
        }
    }

    /**
     * Represents a subset for Union-Find operations
     */
    static class Subset {
        int parent;
        int rank;

        public Subset(int parent, int rank) {
            this.parent = parent;
            this.rank = rank;
        }
    }

    /**
     * Finds the parent of the subset using path compression
     *
     * @param subsets array of subsets
     * @param i       index of the subset
     * @return the parent of the subset
     */
    static int find(Subset[] subsets, int i) {
        if (subsets[i].parent != i) {
            subsets[i].parent = find(subsets, subsets[i].parent);

Review comment (suggestion):
This is strange. Why do the `edges` do not have the correct size from the very beginning? If I understand correctly, the edges should be added in the _correct_ order.

I would suggest change the constructor in such a way, that it takes the array of edges (and performs all needed checks).

Code after:
    static class Graph {
        final int vertex;
        final List<Edge> edges;

        /**
         * Constructor for the graph
         *
         * @param vertex number of vertices
         * @param edges  list of edges
         */
        Graph(final int vertex, final List<Edge> edges) {
            if (vertex < 0) {
                throw new IllegalArgumentException("Number of vertices must be positive");
            }
            if (edges == null || edges.isEmpty()) {
                throw new IllegalArgumentException("Edges list must not be null or empty");
            }
            for (final var edge : edges) {
                checkEdgeVertices(edge.src, vertex);
                checkEdgeVertices(edge.dest, vertex);
            }

            this.vertex = vertex;
            this.edges = edges;
        }
    }

    /**
     * Represents a subset for Union-Find operations
     */
    private static class Component {
        int parent;
        int rank;

        Component(final int parent, final int rank) {
            this.parent = parent;
            this.rank = rank;
        }
    }

    /**
     * Represents the state of Union-Find components and the result list
     */
    private static class BoruvkaState {
        List<Edge> result;
        Component[] components;
        final Graph graph;

        BoruvkaState(final Graph graph) {
            this.result = new ArrayList<>();
            this.components = initializeComponents(graph);