-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathprim_test.go
143 lines (136 loc) · 3.42 KB
/
prim_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package graph
import (
"fmt"
"reflect"
"testing"
)
func TestPrimMST(t *testing.T) {
var testCases = []struct {
edges []Edge
vertices int
start int
cost int
mst []Edge
}{
{
edges: []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},
},
vertices: 5,
start: 0,
cost: 20,
mst: []Edge{
{Start: 0, End: 1, Weight: 4},
{Start: 1, End: 3, Weight: 3},
{Start: 3, End: 4, Weight: 4},
{Start: 1, End: 2, Weight: 9},
},
},
{
edges: []Edge{
{Start: 0, End: 1, Weight: 4},
{Start: 0, End: 7, Weight: 8},
{Start: 1, End: 2, Weight: 8},
{Start: 1, End: 7, Weight: 11},
{Start: 2, End: 3, Weight: 7},
{Start: 2, End: 5, Weight: 4},
{Start: 2, End: 8, Weight: 2},
{Start: 3, End: 4, Weight: 9},
{Start: 3, End: 5, Weight: 14},
{Start: 4, End: 5, Weight: 10},
{Start: 5, End: 6, Weight: 2},
{Start: 6, End: 7, Weight: 1},
{Start: 6, End: 8, Weight: 6},
{Start: 7, End: 8, Weight: 7},
},
vertices: 9,
start: 3,
cost: 37,
mst: []Edge{
{Start: 3, End: 2, Weight: 7},
{Start: 2, End: 8, Weight: 2},
{Start: 2, End: 5, Weight: 4},
{Start: 5, End: 6, Weight: 2},
{Start: 6, End: 7, Weight: 1},
{Start: 2, End: 1, Weight: 8},
{Start: 1, End: 0, Weight: 4},
{Start: 3, End: 4, Weight: 9},
},
},
{
edges: []Edge{
{Start: 0, End: 1, Weight: 2},
{Start: 0, End: 3, Weight: 6},
{Start: 1, End: 2, Weight: 3},
{Start: 1, End: 3, Weight: 8},
{Start: 1, End: 4, Weight: 5},
{Start: 2, End: 4, Weight: 7},
{Start: 3, End: 4, Weight: 9},
},
vertices: 5,
start: 2,
cost: 16,
mst: []Edge{
{Start: 2, End: 1, Weight: 3},
{Start: 1, End: 0, Weight: 2},
{Start: 1, End: 4, Weight: 5},
{Start: 0, End: 3, Weight: 6},
},
},
{
edges: []Edge{
{Start: 0, End: 0, Weight: 0},
},
vertices: 1,
start: 0,
cost: 0,
mst: nil,
},
{
edges: []Edge{
{Start: 0, End: 1, Weight: 1},
{Start: 0, End: 2, Weight: 6},
{Start: 0, End: 3, Weight: 5},
{Start: 1, End: 2, Weight: 2},
{Start: 1, End: 4, Weight: 4},
{Start: 2, End: 4, Weight: 9},
},
vertices: 5,
start: 4,
cost: 12,
mst: []Edge{
{Start: 4, End: 1, Weight: 4},
{Start: 1, End: 0, Weight: 1},
{Start: 1, End: 2, Weight: 2},
{Start: 0, End: 3, Weight: 5},
},
},
}
for i, testCase := range testCases {
t.Run(fmt.Sprintf("Test Case %d", i), func(t *testing.T) {
// Initializing graph, adding edges
graph := New(testCase.vertices)
graph.Directed = false
for _, edge := range testCase.edges {
graph.AddWeightedEdge(int(edge.Start), int(edge.End), edge.Weight)
}
computedMST, computedCost := graph.PrimMST(Vertex(testCase.start))
// Compare the computed result with the expected result
if computedCost != testCase.cost {
t.Errorf("Test Case %d, Expected Cost: %d, Computed: %d", i, testCase.cost, computedCost)
}
if !reflect.DeepEqual(testCase.mst, computedMST) {
t.Errorf("Test Case %d, Expected MST: %v, Computed: %v", i, testCase.mst, computedMST)
}
})
}
}