| 1 | package graph |
| 2 | |
| 3 | import ( |
| 4 | "math/rand" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestMST(t *testing.T) { |
| 9 | g := New(0) |
| 10 | if mess, diff := diff(MST(g), []int{}); diff { |
| 11 | t.Errorf("MST: %s", mess) |
| 12 | } |
| 13 | |
| 14 | g = New(10) |
| 15 | g.AddBothCost(0, 1, 4) |
| 16 | g.AddBothCost(0, 7, 8) |
| 17 | g.AddBothCost(1, 2, 8) |
| 18 | g.AddBothCost(1, 7, 11) |
| 19 | g.AddBothCost(2, 3, 7) |
| 20 | g.AddBothCost(2, 8, 2) |
| 21 | g.AddBothCost(2, 5, 4) |
| 22 | g.AddBothCost(3, 4, 9) |
| 23 | g.AddBothCost(3, 5, 14) |
| 24 | g.AddBothCost(4, 5, 10) |
| 25 | g.AddBothCost(5, 6, 2) |
| 26 | g.AddBothCost(6, 7, 1) |
| 27 | g.AddBothCost(6, 8, 6) |
| 28 | g.AddBothCost(7, 8, 7) |
| 29 | exp := []int{-1, 0, 5, 2, 3, 6, 7, 0, 2, -1} |
| 30 | if mess, diff := diff(MST(g), exp); diff { |
| 31 | t.Errorf("MST: %s", mess) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func BenchmarkMST(b *testing.B) { |
| 36 | n := 1000 |
| 37 | b.StopTimer() |
| 38 | g := New(n) |
| 39 | for i := 0; i < 2*n; i++ { |
| 40 | g.AddCost(rand.Intn(n), rand.Intn(n), int64(rand.Int())) |
| 41 | } |
| 42 | b.StartTimer() |
| 43 | for i := 0; i < b.N; i++ { |
| 44 | _ = MST(g) |
| 45 | } |
| 46 | } |