/
fibonacci_heap.hpp
421 lines (321 loc) · 8.91 KB
/
fibonacci_heap.hpp
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#ifndef FIBONACCI_HEAP_HPP
#define FIBONACCI_HEAP_HPP
#define GOLDEN_RATIO 1.61803398875
#include <math.h>
#include <cstddef>
#include <utility>
#include <iostream>
#include <string>
namespace pq {
typedef std::pair<int,int> ii;
class fibonacci_heap {
public:
fibonacci_heap();
~fibonacci_heap();
bool empty();
std::size_t size();
class Node {
public:
Node(ii _key)
:key(_key),
degree(0),
marked(false),
parent(nullptr),
left(nullptr),
right(nullptr),
child(nullptr)
{}
~Node() {}
ii key;
int degree;
bool marked;
Node *parent;
Node *left;
Node *right;
Node *child;
};
void DecreaseKey(Node* x, ii key);
Node* push(ii k);
void Insert(Node *x);
Node* FindMin();
Node* DeleteMin();
void pop();
Node* top();
void print();
private:
Node *min;
std::size_t n = 0;
void Consolidate();
void Fib_Heap_Link(Node* y, Node* x);
void Cut(Node* x, Node* y);
void CascadingCut(Node* y);
void DeleteNodes(Node *x);
void printNode(Node *x);
};
void fibonacci_heap::DeleteNodes(Node *x) {
if (x == nullptr)
return;
Node *next = x;
do {
Node* cur = next;
next = next->right;
if (cur->degree > 0)
DeleteNodes(cur->child);
delete cur;
} while (next != x);
}
void fibonacci_heap::print() {
std::cout << " digraph fibheap {" << std::endl;
printNode(min);
std::cout << "}" << std::endl;
}
void fibonacci_heap::printNode(Node* x) {
if (x == nullptr)
return;
Node *next = x;
do {
Node* cur = next;
next = next->right;
if (cur != next) {
std::cout << cur->key.second << " [label=\"" << cur->key.second << "," << cur->key.first << "\"]" << std::endl;
std::cout << cur->key.second << " -> " << next->key.second << " [dir=\"both\",minlen=2.0]" << std::endl;
std::cout << "{ rank=same " << cur->key.second << " " << next->key.second << " }" << std::endl;
}
if (cur->degree > 0) {
std::cout << cur->key.second << " -> " << cur->child->key.second << " [dir=\"both\"]" << std::endl;
printNode(cur->child);
} else
std::cout << cur->key.second << " [label=\"" << cur->key.second << "," << cur->key.first << "\"]" << std::endl;
} while (next != x);
}
// To make an empty Fibonacci heap, the Make-Fib-Heap procedure allocates and returns the Fibonacci heap object H, where H.n = 0 and H.min = NIL.
fibonacci_heap::fibonacci_heap() {
n = 0;
min = nullptr;
}
fibonacci_heap::~fibonacci_heap()
{
DeleteNodes(min);
}
// The following procedure inserts node x into Fibonacci heap H, assuming that the node has already been allocated and that x.key has alread been filled in.
void fibonacci_heap::Insert(Node *x) {
//1 X.degree = 0
x->degree = 0;
//2 x.p = NIL
x->parent = nullptr;
//3 x.child = NIL
x->child = nullptr;
//4 x.marked = FALSE
x->marked = false;
if (min == nullptr) {
// create a root list for H containing just x
x->left = x;
x->right = x;
min = x;
} else {
// insert x into H's root list
min->left->right = x;
x->left = min->left;
min->left = x;
x->right = min;
// if x.key < H.min.key
if (x->key < min->key)
min = x;
//H.n = H.n + 1
}
n++;
}
typename fibonacci_heap::Node* fibonacci_heap::FindMin() {
return min;
}
// First make a root out of each of the minimum node's children and remove the minimum node from the root list.
// Then CONSOLIDATE the root list by linking roots of equal degree undtil at most one root remains of each degree.
typename fibonacci_heap::Node* fibonacci_heap::DeleteMin() {
// z = H.min
Node *z = min;
// If z != NIL
if (z != nullptr) {
// for each child x of z
Node *x = z->child; // First child visited
if (x != nullptr) {
do {
// Remember current childs right neighbor
Node *next = x->right;
// add children to root list of H
min->left->right = x;
x->left = min->left;
min->left = x;
x->right = min;
// x.p = NIL
x->parent = nullptr;
// Look at next child
x = next;
} while (x != z->child);
}
// remove z from the root list of H
min->left->right = min->right;
min->right->left = min->left;
// if z == z.right
if (z == z->right)
min = nullptr; // H.min = NIL
else {
min = z->right;
Consolidate();
}
n--;
}
return z;
}
// The procedure Consolidate uses an auxiliary array A[0 .. D(H.n)] to keep track of roots according to their degrees.
// If A[i] = y, then y is currently root with y.degree = i. It is shown in CLRS 19.4 that D(H.n) is upper bounded by log by golden ratio of n (nice!).
void fibonacci_heap::Consolidate() {
//int golden_ratio = static_cast<int>(floor(log(static_cast<double>(n))/log(static_cast<double>(1 + sqrt(static_cast<double>(5)))/2)));
int max_degree = (int)floor(log((double)n)/log(GOLDEN_RATIO));
int d_hn = max_degree+2; //for good measure.
// let A[O .. D(H.n)] be a new array
Node** A = new Node*[d_hn];
// for i = 0 to D(H.n)
for (int i = 0; i < d_hn; i++)
A[i] = nullptr; // A[i] = NIL
// create a root list to iterate
int rootSize = 0;
Node* next;
Node* w = min;
do {
next = w->right;
rootSize++;
w = next;
} while (w != min);
Node** rootList = new Node*[rootSize];
for (int i=0; i < rootSize; i++) {
rootList[i] = next;
next = next->right;
}
// for each node w in the root list of H
//Node* w = min;
for (int i=0; i < rootSize; i++) {
// x = w
Node* x = rootList[i];
// d = x.degree
int d = x->degree;
// while A[d] != NIL
while ( A[d] != nullptr ) {
Node* y = A[d]; // another node with same degree as x
if ( x->key > y->key ) {
// exchange x with y
Node* temp = x;
x = y;
y = temp;
}
Fib_Heap_Link(y,x);
A[d] = nullptr;
d++;
}
A[d] = x;
}
delete [] rootList;
min = nullptr;
// for i = 0 to D(H.n)
for (int i=0; i < d_hn; i++) {
if (A[i] != nullptr) {
if (min == nullptr) {
min = A[i]; // create a root list for H containing just A[i]
min->right = min;
min->left = min;
} else {
// insert A[i] into H's root list
min->left->right = A[i];
A[i]->left = min->left;
min->left = A[i];
A[i]->right = min;
// update min element
if (A[i]->key < min->key)
min = A[i];
}
}
}
delete [] A;
}
void fibonacci_heap::Fib_Heap_Link(Node* y, Node* x) {
// remove y from the root list of H
y->left->right = y->right;
y->right->left = y->left;
// make y a child of x, incrementing x.degree
if (x->child == nullptr) {
// x has no children, insert y.
x->child = y;
y->right = y;
y->left = y;
x->degree = 1;
} else {
// add y to child list
x->child->left->right = y;
y->left = x->child->left;
x->child->left = y;
y->right = x->child;
x->degree++;
}
y->parent = x;
x->marked = false;
}
void fibonacci_heap::DecreaseKey(Node* x, ii key) {
if (key > x->key)
return; // error: new key is greater than current key
x->key = key;
Node* y = x->parent;
if (y != nullptr && x->key < y->key) {
Cut(x,y);
CascadingCut(y);
}
if (x->key < min->key)
min = x;
}
void fibonacci_heap::Cut(Node* x, Node* y) {
// remove x from the child list of y, decrementing y.degree
if (y->degree == 1) // test if x is only child
y->child = nullptr;
else {
x->left->right = x->right;
x->right->left = x->left;
if (y->child == x)
y->child = x->right;
}
y->degree--;
// add x to the root list of H
min->left->right = x;
x->left = min->left;
min->left = x;
x->right = min;
x->parent = nullptr;
x->marked = false;
}
void fibonacci_heap::CascadingCut(Node* y) {
Node* z = y->parent;
if (z != nullptr) {
if (y->marked == false) {
y->marked = true;
} else {
Cut(y,z);
CascadingCut(z);
}
}
}
bool fibonacci_heap::empty() {
return n == 0;
}
std::size_t fibonacci_heap::size() {
return n;
}
typename fibonacci_heap::Node* fibonacci_heap::push(ii k) {
Node *x = new Node(k);
Insert(x);
return x;
}
void fibonacci_heap::pop() {
DeleteMin();
}
typename fibonacci_heap::Node* fibonacci_heap::top() {
return FindMin();
}
}
#endif