-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathprim_test.go
More file actions
40 lines (34 loc) · 806 Bytes
/
Copy pathprim_test.go
File metadata and controls
40 lines (34 loc) · 806 Bytes
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
package graphs
import (
"testing"
)
func TestPrim(t *testing.T) {
graph := NewGraph[string]()
graph.AddEdge("a", "b", 8)
graph.AddEdge("a", "c", 5)
graph.AddEdge("b", "c", 10)
graph.AddEdge("b", "d", 2)
graph.AddEdge("b", "e", 18)
graph.AddEdge("c", "d", 3)
graph.AddEdge("c", "f", 16)
graph.AddEdge("d", "e", 12)
graph.AddEdge("d", "f", 30)
graph.AddEdge("d", "g", 14)
graph.AddEdge("e", "g", 4)
graph.AddEdge("f", "g", 26)
tree := Prim(graph, "g")
if tree == nil {
t.Error("no result")
t.FailNow()
}
result := NewGraph[string]()
result.AddEdge("g", "e", 4)
result.AddEdge("e", "d", 12)
result.AddEdge("d", "b", 2)
result.AddEdge("d", "c", 3)
result.AddEdge("c", "a", 5)
result.AddEdge("c", "f", 16)
if !tree.Equals(result) {
t.Error("bad minimal spanning tree")
}
}