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

Code before:
         * 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
        int V2 = 2;

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

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

        Graph graph2 = new Graph(V2, edges);

        /**
         * Adjacency matrix
         *    0  1
         * 0  0  10
         * 1  10  0
         */
        List<BoruvkaAlgorithm.Edge> result2 = BoruvkaAlgorithm.boruvkaMST(graph2);
        // Expected result:
        // Edges: 1
        // Total weight: 10
        assertEquals(1, result2.size());
        assertEquals(10, result2.stream().mapToInt(edge -> edge.weight).sum());
    }

    @Test
    void testCompleteGraphK4() {
        // Test case 3
        int V3 = 4;

        List<BoruvkaAlgorithm.Edge> edges = new ArrayList<>();
        edges.add(new BoruvkaAlgorithm.Edge(0, 1, 7));
        edges.add(new BoruvkaAlgorithm.Edge(0, 2, 2));
        edges.add(new BoruvkaAlgorithm.Edge(0, 3, 5));
        edges.add(new BoruvkaAlgorithm.Edge(1, 2, 3));

Review comment (suggestion):
```suggestion
        List<BoruvkaAlgorithm.Edge> edges = new ArrayList<>();

        edges.add(new BoruvkaAlgorithm.Edge(0, 1, 10));

        Graph graph2 = new Graph(2, edges);
```

Code after:
         * 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
         *    0  1
         * 0  0  10
         * 1  10  0
         */
        final var result = BoruvkaAlgorithm.boruvkaMST(graph);
        assertEquals(1, result.size());
        assertEquals(10, computeTotalWeight(result));
    }

    @Test
    void testCompleteGraphK4() {
        List<BoruvkaAlgorithm.Edge> edges = new ArrayList<>();
        edges.add(new BoruvkaAlgorithm.Edge(0, 1, 7));
        edges.add(new BoruvkaAlgorithm.Edge(0, 2, 2));
        edges.add(new BoruvkaAlgorithm.Edge(0, 3, 5));
        edges.add(new BoruvkaAlgorithm.Edge(1, 2, 3));
        edges.add(new BoruvkaAlgorithm.Edge(1, 3, 4));
        edges.add(new BoruvkaAlgorithm.Edge(2, 3, 1));

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

        /**
         * Adjacency matrix
         *    0  1  2  3
         * 0  0  7  2  5
         * 1  7  0  3  4
         * 2  2  3  0  1
         * 3  5  4  1  0
         */
        final var result = BoruvkaAlgorithm.boruvkaMST(graph);
        assertEquals(3, result.size());
        assertEquals(6, computeTotalWeight(result));