Skip to content

Commit 5b721b2

Browse files
committed
Add Kruskal's Minimum Spanning Tree algorithm Test
1 parent 9839130 commit 5b721b2

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.thealgorithms.graph;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
import org.junit.jupiter.api.Test;
10+
11+
/**
12+
* Test cases for Kruskal's Minimum Spanning Tree algorithm.
13+
*/
14+
class KruskalMSTTest {
15+
16+
@Test
17+
void testFindMSTWithSimpleGraph() {
18+
final int vertices = 4;
19+
20+
final List<Edge> edges = new ArrayList<>();
21+
edges.add(new Edge(0, 1, 10));
22+
edges.add(new Edge(0, 2, 6));
23+
edges.add(new Edge(0, 3, 5));
24+
edges.add(new Edge(1, 3, 15));
25+
edges.add(new Edge(2, 3, 4));
26+
27+
final List<Edge> mst = KruskalMST.findMST(vertices, edges);
28+
29+
assertNotNull(mst, "MST should not be null");
30+
assertEquals(vertices - 1, mst.size(), "MST should contain V-1 edges");
31+
32+
int totalWeight = 0;
33+
for (final Edge edge : mst) {
34+
totalWeight += edge.weight;
35+
}
36+
37+
assertEquals(19, totalWeight, "Total weight of MST is incorrect");
38+
}
39+
40+
@Test
41+
void testFindMSTWithSingleVertex() {
42+
final int vertices = 1;
43+
final List<Edge> edges = new ArrayList<>();
44+
45+
final List<Edge> mst = KruskalMST.findMST(vertices, edges);
46+
47+
assertNotNull(mst, "MST should not be null");
48+
assertEquals(0, mst.size(), "MST of single vertex graph should be empty");
49+
}
50+
}

0 commit comments

Comments
 (0)