斐波那契堆之Go实现

ACM

一个比二叉堆更高效的数据结构,但是实现起来非常复杂。本科的时候看《算法导论》的时候曾经研究过,不是很明白。今天终于对它有了一个比较清晰的了解。
enter description here

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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package fibonacciHeap
const MinInt = -int(^uint(0)>>1) - 1
type Vertex struct {
value int
parent *Vertex
child *Vertex
left *Vertex
right *Vertex
mark bool
degree int
}
type FibonacciHeap struct {
min *Vertex
size int
}
// Insert inserts a node into heap with value v
func (F *FibonacciHeap) Insert(value int) *Vertex {
v := &Vertex{value: value}
if F.min == nil {
F.min = v
} else {
F.insertToList(F.min, v)
if F.min.value > v.value {
F.min = v
}
}
F.size++
return v
}
func (F *FibonacciHeap) Empty() bool {
return F.min == nil
}
func (F *FibonacciHeap) Minmum() *Vertex {
return F.min
}
func (F *FibonacciHeap) ExtractMin() *Vertex {
z := F.min
if z == nil {
return nil
}
for z.child != nil {
v := F.extractVertex(z.child)
F.insertToList(F.min, v)
}
if z == z.right { //the heap only contains one node
F.min = nil
} else {
F.min = z.right
z = F.extractVertex(z)
F.consolidate()
}
F.size--
return z
}
func (F *FibonacciHeap) Merge(Other *FibonacciHeap) {
if F.min == nil { // if current heap is empty
F.min = Other.min
} else if Other.min != nil {
// cut two circle and rearrange them
F.min.right.left = Other.min.left
Other.min.left.right = F.min.right //reverse
F.min.right = Other.min
Other.min.left = F.min //reverse
if F.min.value > Other.min.value {
F.min = Other.min
}
}
Other.min = nil
}
func (F *FibonacciHeap) DecreaseKey(v *Vertex, value int) bool {
if v.value < value {
return false // do not increase
}
if v.value == value {
return true
}
v.value = value
parent := v.parent
if parent != nil && v.value < parent.value {
v = F.extractVertex(v)
F.insertToList(F.min, v)
F.cascadingCut(parent)
}
if v.value < F.min.value {
F.min = v
}
return true
}
func (F *FibonacciHeap) IncreaseKey(v *Vertex, value int) bool {
if v.value > value {
return false
}
if v.value == value {
return true
}
for v.child != nil {
F.insertToList(F.min, F.extractVertex(v.child))
}
v.value = value
parent := v.parent
if parent != nil {
F.insertToList(F.min, F.extractVertex(v))
F.cascadingCut(parent)
} else if F.min == v {
for cur := v.right; cur != v; cur = cur.right {
if v.value > cur.value {
F.min = cur
}
}
}
return true
}
func (F *FibonacciHeap) Modify(v *Vertex, value int) {
switch {
case v.value < value:
F.IncreaseKey(v, value)
case v.value > value:
F.DecreaseKey(v, value)
}
}
func (F *FibonacciHeap) DeleteVertex(v *Vertex) {
F.DecreaseKey(v, MinInt)
F.extractVertex(v)
}
// insertToList inserts v after pos
func (F *FibonacciHeap) insertToList(pos *Vertex, v *Vertex) {
if pos == nil {
return
}
pos.right.left = v
v.right = pos.right
pos.right = v
v.left = pos
v.parent = pos.parent
if pos.parent != nil {
pos.parent.degree++
}
}
func (F *FibonacciHeap) extractVertex(v *Vertex) *Vertex {
if v == nil {
return nil
}
if v.parent != nil {
if v.right != v {
v.parent.child = v.right
} else {
v.parent.child = nil
}
v.parent.degree--
v.parent = nil
}
if v.left != v {
v.left.right = v.right
v.right.left = v.left
v.left = v
v.right = v
}
v.mark = false
return v
}
func (F *FibonacciHeap) cascadingCut(v *Vertex) {
parent := v.parent
if parent == nil {
return
}
if v.mark == false {
v.mark = true
} else {
// when the parent have lost a child
F.extractVertex(v)
F.insertToList(F.min, v)
F.cascadingCut(parent)
}
}
func (F *FibonacciHeap) consolidate() {
if F.min == nil {
return
}
v := F.min
degree := v.degree
// a record table to help merging vertices with the same degree
table := make([]*Vertex, degree+1)
for {
if len(table) <= degree {
// extend table size
table = append(table, make([]*Vertex, degree-len(table)+1)...)
}
if table[degree] == v {
break
}
if table[degree] == nil {
//currently ,there is no vertices having the same degree of v
table[degree] = v
v = v.right
} else {
// make sure v is the minimal vertex
if table[degree].value < v.value {
table[degree], v = v, table[degree]
}
// merge table[degree] to v as his child
// make v become root
table[degree] = F.extractVertex(table[degree])
if v.child == nil {
v.child = table[degree]
table[degree].parent = v
v.degree++
} else {
//v.degree has been increased in this function
F.insertToList(v.child, table[degree])
}
table[degree] = nil
}
degree = v.degree
}
// find the min
F.min = nil
for _, v := range table {
if v == nil {
continue
}
if F.min == nil {
F.min = v
} else if v.value < F.min.value {
F.min = v
}
}
}

参考