-
Notifications
You must be signed in to change notification settings - Fork 121
/
fibHeap.go
303 lines (269 loc) · 5.71 KB
/
fibHeap.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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package heap
//FibHeapElement :Cross package
type FibHeapElement struct {
p, l, r *FibHeapElement
c, list *fibHeapElementList
mark bool
Key, Value interface{}
}
func (e *FibHeapElement) init(key, value interface{}) *FibHeapElement {
e.p = nil
e.l = nil
e.r = nil
e.c = newFabHeapElementList(e)
e.mark = false
e.Key = key
e.Value = value
return e
}
func (e *FibHeapElement) right() *FibHeapElement {
if p := e.r; e.list != nil && p != &e.list.root {
return p
}
return nil
}
func (e *FibHeapElement) left() *FibHeapElement {
if p := e.l; e.list != nil && p != &e.list.root {
return p
}
return nil
}
func (e *FibHeapElement) degree() int {
return e.c.Len()
}
func (e *FibHeapElement) addChild(child *FibHeapElement) {
child.p = e
e.c.PushRight(child)
}
func newFabHeapElement(key, value interface{}) *FibHeapElement {
return new(FibHeapElement).init(key, value)
}
//list container
type fibHeapElementList struct {
p *FibHeapElement
root FibHeapElement
len int
}
func (l *fibHeapElementList) Init(p *FibHeapElement) *fibHeapElementList {
l.root.l = &l.root
l.root.r = &l.root
l.p = p
l.len = 0
return l
}
func (l *fibHeapElementList) Len() int {
return l.len
}
func (l *fibHeapElementList) insert(e, at *FibHeapElement) *FibHeapElement {
n := at.r
at.r = e
e.l = at
e.r = n
n.l = e
e.p = l.p
e.list = l
l.len++
return e
}
func (l *fibHeapElementList) Remove(e *FibHeapElement) {
if e.list == l {
e.l.r = e.r
e.r.l = e.l
e.r = nil // avoid memory leaks
e.l = nil // avoid memory leaks
e.list = nil
e.p = nil
l.len--
}
}
func (l *fibHeapElementList) PushLeft(e *FibHeapElement) {
l.insert(e, &l.root)
}
func (l *fibHeapElementList) PushRight(e *FibHeapElement) {
l.insert(e, l.root.l)
}
func (l *fibHeapElementList) Leftist() *FibHeapElement {
if l.len == 0 {
return nil
}
return l.root.r
}
func (l *fibHeapElementList) Rightist() *FibHeapElement {
if l.len == 0 {
return nil
}
return l.root.l
}
func (l *fibHeapElementList) MergeRightList(other *fibHeapElementList) *fibHeapElementList {
for i, e := other.Len(), other.Leftist(); i > 0; i = i - 1 {
nextE := e.right()
other.Remove(e)
l.insert(e, l.root.l)
e = nextE
}
return l
}
func (l *fibHeapElementList) MergeLeftList(other *fibHeapElementList) *fibHeapElementList {
for i, e := other.Len(), other.Rightist(); i > 0; i = i - 1 {
nextE := e.left()
other.Remove(e)
l.insert(e, l.root.r)
e = nextE
}
return l
}
func newFabHeapElementList(p *FibHeapElement) *fibHeapElementList {
return new(fibHeapElementList).Init(p)
}
//FibHeapMixin : could be implements
type FibHeapMixin interface {
LessKey(interface{}, interface{}) bool
}
//FibHeap : Cross package
type FibHeap struct {
root *fibHeapElementList
min *FibHeapElement
n int
mixin FibHeapMixin
}
//Init : Cross package
func (h *FibHeap) Init(mixin FibHeapMixin) *FibHeap {
h.root = newFabHeapElementList(nil)
h.min = nil
h.n = 0
h.mixin = mixin
return h
}
//default less function, max heap
func (h *FibHeap) less(n1, n2 *FibHeapElement) bool {
if n1 == nil && n2 == nil {
panic("both nodes are nil!")
} else if n1 == nil {
return false
} else if n2 == nil {
return true
}
return h.mixin.LessKey(n1.Key, n2.Key)
}
//Degree : floor(lg n), Cross package
func (h *FibHeap) Degree() int {
if h.n == 0 {
return 0
}
i := 1
for diff := h.n; diff != 1; {
diff = h.n >> uint32(i)
i++
}
return i
}
//Len : n, Cross package
func (h *FibHeap) Len() int {
return h.n
}
//Insert : Cross package
func (h *FibHeap) Insert(key, value interface{}) *FibHeapElement {
n := newFabHeapElement(key, value)
h.root.PushRight(n)
if h.less(n, h.min) {
h.min = n
}
h.n++
return n
}
//Union : Cross package
func (h *FibHeap) Union(h1 *FibHeap) *FibHeap {
h.root = h.root.MergeRightList(h1.root)
if h.less(h1.min, h.min) {
h.min = h1.min
}
h.n += h1.n
return h
}
func (h *FibHeap) consolidate() {
degreeArray := make([]*FibHeapElement, h.Degree()+1, h.Degree()+1)
for i, e := h.root.Len(), h.root.Leftist(); i > 0; i = i - 1 {
nextE := e.right()
for e1 := degreeArray[e.degree()]; e1 != nil && e.degree() < h.Degree(); e1 = degreeArray[e.degree()] {
degreeArray[e.degree()] = nil
if h.less(e1, e) {
e, e1 = e1, e
}
e1.mark = false
h.root.Remove(e1)
e.c.PushRight(e1)
}
degreeArray[e.degree()] = e
e = nextE
}
h.min = nil
h.root = newFabHeapElementList(nil)
for i := range degreeArray {
if degreeArray[i] != nil {
h.root.PushRight(degreeArray[i])
if h.less(degreeArray[i], h.min) {
h.min = degreeArray[i]
}
}
}
}
//ExtractMin : Cross package
func (h *FibHeap) ExtractMin() *FibHeapElement {
n := h.min
if n != nil {
for i, e := n.degree(), n.c.Leftist(); i > 0; i = i - 1 {
nextE := e.right()
n.c.Remove(e)
h.root.PushLeft(e)
e = nextE
}
h.root.Remove(n)
if n == n.right() {
h.min = nil
} else {
h.min = n.right()
h.consolidate()
}
h.n--
}
return n
}
func (h *FibHeap) cascadingCut(n *FibHeapElement) {
if p := n.p; p != nil {
if n.mark {
p.c.Remove(n)
h.root.PushLeft(n)
n.mark = false
h.cascadingCut(p)
} else {
n.mark = true
}
}
}
//ModifyNode : Cross package
func (h *FibHeap) ModifyNode(n *FibHeapElement, key, value interface{}) {
if h.mixin.LessKey(n.Key, key) {
panic("Key violated")
}
n.Key = key
n.Value = value
if p := n.p; n.p != nil && h.less(n, n.p) {
p.c.Remove(n)
h.root.PushLeft(n)
n.mark = false
h.cascadingCut(p)
}
if h.less(n, h.min) {
h.min = n
}
}
type defalutFibHeapLessMixin struct {
FibHeapMixin
}
func (m *defalutFibHeapLessMixin) LessKey(i, j interface{}) bool {
return i.(int) > j.(int)
}
//NewFibHeap : Cross package
func NewFibHeap() *FibHeap {
return new(FibHeap).Init(new(defalutFibHeapLessMixin))
}