-
Notifications
You must be signed in to change notification settings - Fork 0
/
internal.go
205 lines (169 loc) · 4.55 KB
/
internal.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
package fibheap
import (
"bytes"
"container/list"
"errors"
"fmt"
"math"
)
func probeTree[t any](buffer *bytes.Buffer, tree *list.List) {
buffer.WriteString(fmt.Sprintf("< "))
for e := tree.Front(); e != nil; e = e.Next() {
buffer.WriteString(fmt.Sprintf("%f ", e.Value.(*node[t]).priority))
if e.Value.(*node[t]).children.Len() != 0 {
probeTree[t](buffer, e.Value.(*node[t]).children)
}
}
buffer.WriteString(fmt.Sprintf("> "))
}
func (heap *FibHeap[t]) deleteNode(n *node[t]) {
heap.decreaseKey(n, math.Inf(-1))
heap.extractMin()
}
func (heap *FibHeap[t]) link(parent, child *node[t]) {
child.marked = false
child.parent = parent
child.self = parent.children.PushBack(child)
parent.degree++
}
func (heap *FibHeap[t]) resetMin() {
heap.min = heap.roots.Front().Value.(*node[t])
for tree := heap.min.self.Next(); tree != nil; tree = tree.Next() {
if tree.Value.(*node[t]).priority < heap.min.priority {
heap.min = tree.Value.(*node[t])
}
}
}
func (heap *FibHeap[t]) cut(n *node[t]) {
n.parent.children.Remove(n.self)
n.parent.degree--
n.parent = nil
n.marked = false
n.self = heap.roots.PushBack(n)
}
func (heap *FibHeap[t]) cascadingCut(n *node[t]) {
if n.parent != nil {
if !n.marked {
n.marked = true
} else {
parent := n.parent
heap.cut(n)
heap.cascadingCut(parent)
}
}
}
func (heap *FibHeap[t]) consolidate() {
for tree := heap.roots.Front(); tree != nil; tree = tree.Next() {
heap.treeDegrees[tree.Value.(*node[t]).position] = nil
}
for tree := heap.roots.Front(); tree != nil; {
if heap.treeDegrees[tree.Value.(*node[t]).degree] == nil {
heap.treeDegrees[tree.Value.(*node[t]).degree] = tree
tree.Value.(*node[t]).position = tree.Value.(*node[t]).degree
tree = tree.Next()
continue
}
if heap.treeDegrees[tree.Value.(*node[t]).degree] == tree {
tree = tree.Next()
continue
}
for heap.treeDegrees[tree.Value.(*node[t]).degree] != nil {
anotherTree := heap.treeDegrees[tree.Value.(*node[t]).degree]
heap.treeDegrees[tree.Value.(*node[t]).degree] = nil
if tree.Value.(*node[t]).priority <= anotherTree.Value.(*node[t]).priority {
heap.roots.Remove(anotherTree)
heap.link(tree.Value.(*node[t]), anotherTree.Value.(*node[t]))
} else {
heap.roots.Remove(tree)
heap.link(anotherTree.Value.(*node[t]), tree.Value.(*node[t]))
tree = anotherTree
}
}
heap.treeDegrees[tree.Value.(*node[t]).degree] = tree
tree.Value.(*node[t]).position = tree.Value.(*node[t]).degree
}
heap.resetMin()
}
func (heap *FibHeap[t]) insert(data t, priority float64) error {
if math.IsInf(priority, -1) {
return errors.New("Negative infinity priority is reserved for internal usage ")
}
heap.mutex.Lock()
defer heap.mutex.Unlock()
if _, exists := heap.index[data]; exists {
return errors.New("Duplicate data is not allowed ")
}
node := new(node[t])
node.children = list.New()
node.data = data
node.priority = priority
node.self = heap.roots.PushBack(node)
heap.index[node.data] = node
heap.num++
if heap.min == nil || heap.min.priority > node.priority {
heap.min = node
}
return nil
}
func (heap *FibHeap[t]) extractMin() *node[t] {
heap.mutex.Lock()
defer heap.mutex.Unlock()
min := heap.min
children := heap.min.children
if children != nil {
for e := children.Front(); e != nil; e = e.Next() {
e.Value.(*node[t]).parent = nil
e.Value.(*node[t]).self = heap.roots.PushBack(e.Value.(*node[t]))
}
}
heap.roots.Remove(heap.min.self)
heap.treeDegrees[min.position] = nil
delete(heap.index, heap.min.data)
heap.num--
if heap.num == 0 {
heap.min = nil
} else {
heap.consolidate()
}
return min
}
func (heap *FibHeap[t]) decreaseKey(n *node[t], priority float64) error {
heap.mutex.Lock()
defer heap.mutex.Unlock()
if priority >= n.priority {
return errors.New("New priority is not smaller than current priority ")
}
n.priority = priority
if n.parent != nil {
parent := n.parent
if n.priority < n.parent.priority {
heap.cut(n)
heap.cascadingCut(parent)
}
}
if n.parent == nil && n.priority < heap.min.priority {
heap.min = n
}
return nil
}
func (heap *FibHeap[t]) increaseKey(n *node[t], priority float64) error {
heap.mutex.Lock()
defer heap.mutex.Unlock()
if priority <= n.priority {
return errors.New("New priority is not larger than current priority ")
}
n.priority = priority
child := n.children.Front()
for child != nil {
childNode := child.Value.(*node[t])
child = child.Next()
if childNode.priority < n.priority {
heap.cut(childNode)
heap.cascadingCut(n)
}
}
if heap.min == n {
heap.resetMin()
}
return nil
}