-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacciHeap.h
More file actions
321 lines (264 loc) · 7.06 KB
/
FibonacciHeap.h
File metadata and controls
321 lines (264 loc) · 7.06 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#ifndef HEAP_FIBONACCIHEAP_H
#define HEAP_FIBONACCIHEAP_H
#include "Vector.h"
#include <cstdlib>
template <class Key>
class FibonacciHeap {
private:
class Node;
public:
class Pointer {
friend FibonacciHeap<Key>;
private:
Node *ptr;
explicit Pointer(Node *ptr_);
public:
Pointer();
Key getKey();
};
FibonacciHeap();
bool is_empty() const;
Pointer insert(Key);
Key get_min() const;
Key extract_min();
void merge(FibonacciHeap&);
void decrease(Pointer, Key);
private:
class Node {
friend FibonacciHeap<Key>;
private:
Key key;
Node *parent, *child, *prev, *next;
size_t degree;
bool mark;
explicit Node(Key);
};
Node *min_node;
void attach(Node*, Node*);
void add_node_to_roots(Node*);
void consolidate(Node*);
void cut(Node*);
void cascading_cut(Node*);
};
template <class Key>
FibonacciHeap<Key>::Pointer::Pointer(Node *ptr_) {
ptr = ptr_;
}
template <class Key>
FibonacciHeap<Key>::Pointer::Pointer() {
ptr = nullptr;
}
template <class Key>
Key FibonacciHeap<Key>::Pointer::getKey() {
return ptr->key;
}
template <class Key>
FibonacciHeap<Key>::FibonacciHeap() {
min_node = nullptr;
}
template <class Key>
bool FibonacciHeap<Key>::is_empty() const {
return min_node == nullptr;
}
template<class Key>
typename FibonacciHeap<Key>::Pointer FibonacciHeap<Key>::insert(Key key) {
Node *new_node = new Node(key);
add_node_to_roots(new_node);
return Pointer(new_node);
}
template<class Key>
Key FibonacciHeap<Key>::get_min() const {
if (is_empty()) {
throw std::logic_error("FibonacciHeap instance is empty");
}
return min_node->key;
}
template <class Key>
Key FibonacciHeap<Key>::extract_min() {
if (is_empty()) {
throw std::logic_error("FibonacciHeap instance is empty");
}
Key ret = min_node->key;
Node *cur = min_node->child;
if (cur != nullptr) {
Vector<Node*> children;
children.push_back(cur);
Node *start = cur;
cur = cur->next;
while (cur != start) {
children.push_back(cur);
cur = cur->next;
}
for (int i = 0; i < children.size(); ++i) {
Node *cur = children[i];
cur->parent = nullptr;
add_node_to_roots(cur);
}
}
Node *prev_node = min_node->prev;
Node *next_node = min_node->next;
prev_node->next = next_node;
next_node->prev = prev_node;
bool flag = (next_node == min_node);
delete min_node;
min_node = nullptr;
if (!flag) {
consolidate(next_node);
}
return ret;
}
template<class Key>
void FibonacciHeap<Key>::merge(FibonacciHeap &otherHeap) {
if (min_node == nullptr) {
min_node = otherHeap.min_node;
}
else if (otherHeap.min_node == nullptr) {
return;
}
else if (min_node->next == min_node) {
otherHeap.add_node_to_roots(min_node);
min_node = otherHeap.min_node;
}
else if (otherHeap.min_node->next == otherHeap.min_node) {
add_node_to_roots(otherHeap.min_node);
}
else {
Node *a = min_node, *b = min_node->next;
Node *c = otherHeap.min_node, *d = otherHeap.min_node->prev;
a->next = c;
c->prev = a;
d->next = b;
b->prev = a;
if (otherHeap.min_node->key < min_node->key) {
min_node = otherHeap.min_node;
}
}
otherHeap.min_node = nullptr;
}
template<class Key>
void FibonacciHeap<Key>::decrease(Pointer ptr, Key key) {
Node *cur = ptr.ptr;
if (cur->key < key) {
throw std::invalid_argument("Decrease new value is bigger than current value");
}
cur->key = key;
Node *par = cur->parent;
if (par != nullptr && cur->key < par->key) {
cut(cur);
cascading_cut(par);
}
else if (cur->key < min_node->key) {
min_node = cur;
}
}
template<class Key>
FibonacciHeap<Key>::Node::Node(Key key_) {
key = key_;
parent = child = prev = next = nullptr;
degree = 0;
mark = false;
}
template <class Key>
void FibonacciHeap<Key>::attach(Node *root, Node *child) {
// attaches child node to root node
Node *root_child = root->child;
if (root_child == nullptr) {
root->child = child;
child->next = child->prev = child;
}
else {
Node *next_root_child = root_child->next;
root_child->next = child, next_root_child->prev = child;
child->prev = root_child, child->next = next_root_child;
}
++root->degree;
child->parent = root;
}
template <class Key>
void FibonacciHeap<Key>::add_node_to_roots(Node *node) {
// assume node is a root in a tree (parent == null)
// and we add it to roots list maintaining min_node
if (min_node == nullptr) {
min_node = node;
node->next = node->prev = node;
}
else {
Node *next_node = min_node->next;
node->next = next_node, node->prev = min_node;
min_node->next = node, next_node->prev = node;
if (node->key < min_node->key) {
min_node = node;
}
}
}
template <class Key>
void FibonacciHeap<Key>::consolidate(Node *root_node) {
// param root_node - arbitrary node in roots list
Vector<Node*> arr;
Node *cur = root_node;
Node *start = cur;
arr.push_back(cur);
cur = cur->next;
while (cur != start ) {
arr.push_back(cur);
cur = cur->next;
}
size_t max_degree = 0;
for (int i = 0; i < arr.size(); ++i) {
arr[i]->prev = arr[i]->next = nullptr;
max_degree = max(max_degree, arr[i]->degree);
}
Vector<Node*> con;
for (int i = 0; i < arr.size(); ++i) {
cur = arr[i];
while (con.size() <= cur->degree) {
con.push_back(nullptr);
}
while (con[cur->degree] != nullptr) {
Node *cur2 = con[cur->degree];
if (cur->key > cur2->key) {
swap(cur, cur2);
}
con[cur->degree] = nullptr;
attach(cur, cur2);
if (con.size() <= cur->degree) {
con.push_back(nullptr);
}
}
con[cur->degree] = cur;
}
for (int i = 0; i < con.size(); ++i) {
if (con[i] != nullptr) {
add_node_to_roots(con[i]);
}
}
}
template<class Key>
void FibonacciHeap<Key>::cut(Node *node) {
Node *par = node->parent;
--par->degree;
par->child = nullptr;
Node *prev_node = node->prev, *next_node = node->next;
prev_node->next = next_node, next_node->prev = prev_node;
if (next_node != node) {
par->child = next_node;
}
node->prev = node->next = node;
node->parent = nullptr;
node->mark = false;
add_node_to_roots(node);
}
template<class Key>
void FibonacciHeap<Key>::cascading_cut(Node *node) {
Node *par = node->parent;
if (par != nullptr) {
if (!node->mark) {
node->mark = true;
}
else {
cut(node);
cascading_cut(par);
}
}
}
#endif //HEAP_FIBONACCIHEAP_H