Repository: TheAlgorithms/Java
File: src/test/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithmTest.java (Java)

Code before:
import org.junit.jupiter.api.Test;

public class BoruvkaAlgorithmTest {
    @Test
    public void testBoruvkaMSTV9E14() {
        // Test case 1
        int V1 = 9;

        List<BoruvkaAlgorithm.Edge> edges = new ArrayList<>();

        edges.add(new BoruvkaAlgorithm.Edge(0, 1, 10));
        edges.add(new BoruvkaAlgorithm.Edge(0, 2, 12));
        edges.add(new BoruvkaAlgorithm.Edge(1, 2, 9));
        edges.add(new BoruvkaAlgorithm.Edge(1, 3, 8));
        edges.add(new BoruvkaAlgorithm.Edge(2, 4, 3));
        edges.add(new BoruvkaAlgorithm.Edge(2, 5, 1));
        edges.add(new BoruvkaAlgorithm.Edge(4, 5, 3));
        edges.add(new BoruvkaAlgorithm.Edge(4, 3, 7));
        edges.add(new BoruvkaAlgorithm.Edge(3, 6, 8));
        edges.add(new BoruvkaAlgorithm.Edge(3, 7, 5));
        edges.add(new BoruvkaAlgorithm.Edge(5, 7, 6));
        edges.add(new BoruvkaAlgorithm.Edge(6, 7, 9));
        edges.add(new BoruvkaAlgorithm.Edge(6, 8, 2));
        edges.add(new BoruvkaAlgorithm.Edge(7, 8, 11));

        Graph graph1 = new Graph(V1, edges);
        /**
         * Adjacency matrix
         *    0   1   2   3   4   5   6   7   8
         * 0  0  10  12   0   0   0   0   0   0
         * 1 10   0   9   8   0   0   0   0   0
         * 2 12   9   0   0   3   1   0   0   0
         * 3  0   8   0   0   7   0   8   5   0
         * 4  0   0   3   7   0   3   0   0   0
         * 5  0   0   1   0   3   0   0   6   0
         * 6  0   0   0   8   0   0   0   9   2
         * 7  0   0   0   5   0   6   9   0  11
         * 8  0   0   0   0   0   0   2  11   0
         */

        List<BoruvkaAlgorithm.Edge> result1 = BoruvkaAlgorithm.boruvkaMST(graph1);
        // Expected result:
        // Edges: 8
        // Total weight: 43
        assertEquals(8, result1.size());
        assertEquals(43, result1.stream().mapToInt(edge -> edge.weight).sum());
    }

    @Test
    void testBoruvkaMSTV2E1() {
        // Test case 2

Review comment (suggestion):
```suggestion
        List<BoruvkaAlgorithm.Edge> edges = new ArrayList<>();

        edges.add(new BoruvkaAlgorithm.Edge(0, 1, 10));
        edges.add(new BoruvkaAlgorithm.Edge(0, 2, 12));
        edges.add(new BoruvkaAlgorithm.Edge(1, 2, 9));
        edges.add(new BoruvkaAlgorithm.Edge(1, 3, 8));
        edges.add(new BoruvkaAlgorithm.Edge(2, 4, 3));
        edges.add(new BoruvkaAlgorithm.Edge(2, 5, 1));
        edges.add(new BoruvkaAlgorithm.Edge(4, 5, 3));
        edges.add(new BoruvkaAlgorithm.Edge(4, 3, 7));
        edges.add(new BoruvkaAlgorithm.Edge(3, 6, 8));
        edges.add(new BoruvkaAlgorithm.Edge(3, 7, 5));
        edges.add(new BoruvkaAlgorithm.Edge(5, 7, 6));
        edges.add(new BoruvkaAlgorithm.Edge(6, 7, 9));
        edges.add(new BoruvkaAlgorithm.Edge(6, 8, 2));
        edges.add(new BoruvkaAlgorithm.Edge(7, 8, 11));

        Graph graph1 = new Graph(9, edges);
```

Code after:
import org.junit.jupiter.api.Test;

public class BoruvkaAlgorithmTest {
    @Test
    public void testBoruvkaMSTV9E14() {
        List<BoruvkaAlgorithm.Edge> edges = new ArrayList<>();

        edges.add(new BoruvkaAlgorithm.Edge(0, 1, 10));
        edges.add(new BoruvkaAlgorithm.Edge(0, 2, 12));
        edges.add(new BoruvkaAlgorithm.Edge(1, 2, 9));
        edges.add(new BoruvkaAlgorithm.Edge(1, 3, 8));
        edges.add(new BoruvkaAlgorithm.Edge(2, 4, 3));
        edges.add(new BoruvkaAlgorithm.Edge(2, 5, 1));
        edges.add(new BoruvkaAlgorithm.Edge(4, 5, 3));
        edges.add(new BoruvkaAlgorithm.Edge(4, 3, 7));
        edges.add(new BoruvkaAlgorithm.Edge(3, 6, 8));
        edges.add(new BoruvkaAlgorithm.Edge(3, 7, 5));
        edges.add(new BoruvkaAlgorithm.Edge(5, 7, 6));
        edges.add(new BoruvkaAlgorithm.Edge(6, 7, 9));
        edges.add(new BoruvkaAlgorithm.Edge(6, 8, 2));
        edges.add(new BoruvkaAlgorithm.Edge(7, 8, 11));

        final var graph = new Graph(9, edges);
        /**
         * Adjacency matrix
         *    0   1   2   3   4   5   6   7   8
         * 0  0  10  12   0   0   0   0   0   0
         * 1 10   0   9   8   0   0   0   0   0
         * 2 12   9   0   0   3   1   0   0   0
         * 3  0   8   0   0   7   0   8   5   0
         * 4  0   0   3   7   0   3   0   0   0
         * 5  0   0   1   0   3   0   0   6   0
         * 6  0   0   0   8   0   0   0   9   2
         * 7  0   0   0   5   0   6   9   0  11
         * 8  0   0   0   0   0   0   2  11   0
         */
        final var result = BoruvkaAlgorithm.boruvkaMST(graph);
        assertEquals(8, result.size());
        assertEquals(43, computeTotalWeight(result));
    }

    @Test
    void testBoruvkaMSTV2E1() {
        List<BoruvkaAlgorithm.Edge> edges = new ArrayList<>();

        edges.add(new BoruvkaAlgorithm.Edge(0, 1, 10));

        final var graph = new Graph(2, edges);

        /**
         * Adjacency matrix