-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
62 lines (48 loc) · 921 Bytes
/
main.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
package main
import (
"math"
)
const INF uint = math.MaxUint
func min(ln int, key *[]uint, mst *[]bool) uint {
var m, mIndex uint = INF, 0
for i := 0; i < ln; i++ {
if !(*mst)[i] && (*key)[i] < m {
m, mIndex = (*key)[i], uint(i)
}
}
return mIndex
}
func prim(g *[][]uint) *[]int {
var (
par []int = make([]int, len(*g))
key []uint = make([]uint, len(*g))
mst []bool = make([]bool, len(*g))
)
for i := range key {
key[i] = INF
}
key[0] = 0
par[0] = -1
for i := 0; i < len(*g); i++ {
mst[min(len(*g), &key, &mst)] = true
for j := 0; j < len(*g); j++ {
if (*g)[i][j] != 0 &&
!mst[j] && (*g)[i][j] < key[j] {
par[j], key[j] = i, (*g)[i][j]
}
}
}
return &par
}
func main() {
example := [][]uint{
{0, 2, 0, 6, 0},
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},
{6, 8, 0, 0, 9},
{0, 5, 7, 9, 0},
}
show(&example, nil)
ans := prim(&example)
show(&example, ans)
}