forked from shellfly/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprim_mst.go
94 lines (84 loc) · 1.64 KB
/
prim_mst.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
package algs4
import "fmt"
type FloatPQItem float32
func (f FloatPQItem) CompareTo(other interface{}) int {
ff := other.(FloatPQItem)
if f < ff {
return -1
} else if f > ff {
return 1
} else {
return 0
}
}
func (f FloatPQItem) String() string {
return fmt.Sprintf("%.5f", f)
}
// PrimMST ...
type PrimMST struct {
edgeTo []*Edge
distTo []float32
marked []bool
pq *IndexMinPQ
}
// NewPrimMST ...
func NewPrimMST(g *EdgeWeightedGraph) *PrimMST {
edgeTo := make([]*Edge, g.V())
distTo := make([]float32, g.V())
marked := make([]bool, g.V())
pq := NewIndexMinPQ(g.V())
l := &PrimMST{edgeTo, distTo, marked, pq}
for v := 0; v < g.V(); v++ {
l.distTo[v] = 999.0 // fake infinity
}
for v := 0; v < g.V(); v++ {
if !l.marked[v] {
l.prim(g, v)
}
}
return l
}
func (l *PrimMST) prim(g *EdgeWeightedGraph, s int) {
l.distTo[s] = 0.0
l.pq.Insert(s, FloatPQItem(l.distTo[s]))
for !l.pq.IsEmpty() {
v := l.pq.DelMin()
l.visit(g, v)
}
}
func (l *PrimMST) visit(g *EdgeWeightedGraph, v int) {
l.marked[v] = true
for _, e := range g.Adj(v) {
w := e.Other(v)
if l.marked[w] {
continue
}
if e.Weight() < l.distTo[w] {
l.distTo[w] = e.Weight()
l.edgeTo[w] = e
if l.pq.Contains(w) {
l.pq.DecreaseKey(w, FloatPQItem(l.distTo[w]))
} else {
l.pq.Insert(w, FloatPQItem(l.distTo[w]))
}
}
}
}
// Weight ...
func (l *PrimMST) Weight() float32 {
var weight float32
for _, e := range l.Edges() {
weight += e.Weight()
}
return weight
}
// Edges ...
func (l *PrimMST) Edges() (edges []*Edge) {
for v := 0; v < len(l.edgeTo); v++ {
e := l.edgeTo[v]
if e != nil {
edges = append(edges, e)
}
}
return
}