-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathkruskal_test.go
85 lines (81 loc) · 1.75 KB
/
kruskal_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package graph
import (
"fmt"
"testing"
)
func TestKruskalMST(t *testing.T) {
// Define test cases with inputs, expected outputs, and sample graphs
var testCases = []struct {
n int
graph []Edge
cost int
}{
// Test Case 1
{
n: 5,
graph: []Edge{
{Start: 0, End: 1, Weight: 4},
{Start: 0, End: 2, Weight: 13},
{Start: 0, End: 3, Weight: 7},
{Start: 0, End: 4, Weight: 7},
{Start: 1, End: 2, Weight: 9},
{Start: 1, End: 3, Weight: 3},
{Start: 1, End: 4, Weight: 7},
{Start: 2, End: 3, Weight: 10},
{Start: 2, End: 4, Weight: 14},
{Start: 3, End: 4, Weight: 4},
},
cost: 20,
},
// Test Case 2
{
n: 3,
graph: []Edge{
{Start: 0, End: 1, Weight: 12},
{Start: 0, End: 2, Weight: 18},
{Start: 1, End: 2, Weight: 6},
},
cost: 18,
},
// Test Case 3
{
n: 4,
graph: []Edge{
{Start: 0, End: 1, Weight: 2},
{Start: 0, End: 2, Weight: 1},
{Start: 0, End: 3, Weight: 2},
{Start: 1, End: 2, Weight: 1},
{Start: 1, End: 3, Weight: 2},
{Start: 2, End: 3, Weight: 3},
},
cost: 4,
},
// Test Case 4
{
n: 2,
graph: []Edge{
{Start: 0, End: 1, Weight: 4000000},
},
cost: 4000000,
},
// Test Case 5
{
n: 1,
graph: []Edge{
{Start: 0, End: 0, Weight: 0},
},
cost: 0,
},
}
// Iterate through the test cases and run the tests
for i, testCase := range testCases {
t.Run(fmt.Sprintf("Test Case %d", i), func(t *testing.T) {
// Execute KruskalMST for the current test case
_, computed := KruskalMST(testCase.n, testCase.graph)
// Compare the computed result with the expected result
if computed != testCase.cost {
t.Errorf("Test Case %d, Expected: %d, Computed: %d", i, testCase.cost, computed)
}
})
}
}