-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibheap.cpp
More file actions
338 lines (317 loc) · 7.19 KB
/
Copy pathfibheap.cpp
File metadata and controls
338 lines (317 loc) · 7.19 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// ===== File: fibheap.cpp =====
#include "fibheap.h"
#include <vector>
#include <cmath>
#include <cstdlib>
#include <climits>
using namespace std;
// allocate a new node
static fh_node *create_node(int vertex, int key)
{
fh_node *x = new fh_node(vertex, key);
return x;
}
// remove x from its circular list and isolate it
static void remove_from_list(fh_node *x)
{
if (!x)
return;
if (x->left == x && x->right == x)
{
// single node, leave isolated
}
else
{
x->left->right = x->right;
x->right->left = x->left;
}
x->left = x->right = x;
}
// link child under parent
static void link_nodes(fibheap_c *H, fh_node *parent, fh_node *child)
{
// detach child from root list
remove_from_list(child);
child->parent = parent;
child->mark = false;
if (!parent->child)
{
parent->child = child;
child->left = child->right = child;
}
else
{
child->right = parent->child->right;
child->left = parent->child;
parent->child->right->left = child;
parent->child->right = child;
}
parent->degree++;
}
// consolidate root list after extract
static void consolidate(fibheap_c *H)
{
if (!H->minptr)
return;
int maxDeg = (int)(log2(max(1, H->nodes))) + 2;
vector<fh_node *> A(maxDeg, nullptr);
// take snapshot of root list
vector<fh_node *> roots;
fh_node *start = H->minptr;
fh_node *cur = start;
do
{
roots.push_back(cur);
cur = cur->right;
} while (cur != start);
for (fh_node *w : roots)
{
fh_node *x = w;
int d = x->degree;
while (d >= (int)A.size())
A.resize(A.size() * 2 + 1, nullptr);
while (A[d] != nullptr)
{
fh_node *y = A[d];
if (y->key < x->key)
{
fh_node *tmp = x;
x = y;
y = tmp;
}
link_nodes(H, x, y);
A[d] = nullptr;
d++;
while (d >= (int)A.size())
A.resize(A.size() * 2 + 1, nullptr);
}
A[d] = x;
}
// rebuild root list and find new min
H->minptr = nullptr;
for (fh_node *n : A)
{
if (!n)
continue;
n->left = n->right = n;
n->parent = nullptr;
if (!H->minptr)
H->minptr = n;
else
{
n->right = H->minptr->right;
n->left = H->minptr;
H->minptr->right->left = n;
H->minptr->right = n;
if (n->key < H->minptr->key)
H->minptr = n;
}
}
}
// move children of z to root list
static void move_children_to_root(fibheap_c *H, fh_node *z)
{
if (!z || !z->child)
return;
vector<fh_node *> children;
fh_node *cstart = z->child;
fh_node *cc = cstart;
do
{
children.push_back(cc);
cc = cc->right;
} while (cc != cstart);
for (fh_node *ch : children)
{
remove_from_list(ch);
ch->parent = nullptr;
ch->mark = false;
if (!H->minptr)
{
ch->left = ch->right = ch;
H->minptr = ch;
}
else
{
ch->right = H->minptr->right;
ch->left = H->minptr;
H->minptr->right->left = ch;
H->minptr->right = ch;
if (ch->key < H->minptr->key)
H->minptr = ch;
}
}
z->child = nullptr;
}
// create heap
fibheap_c *fh_create()
{
fibheap_c *H = new fibheap_c();
H->nodes = 0;
H->minptr = nullptr;
return H;
}
// destroy heap and free nodes
void fh_destroy(fibheap_c *H)
{
if (!H)
return;
if (H->minptr)
{
vector<fh_node *> stack;
vector<fh_node *> all;
fh_node *start = H->minptr;
fh_node *cur = start;
do
{
stack.push_back(cur);
cur = cur->right;
} while (cur != start);
while (!stack.empty())
{
fh_node *n = stack.back();
stack.pop_back();
if (!n)
continue;
if (n->degree == -1)
continue; // visited marker
n->degree = -1;
all.push_back(n);
if (n->child)
{
fh_node *cstart = n->child;
fh_node *cc = cstart;
do
{
stack.push_back(cc);
cc = cc->right;
} while (cc != cstart);
}
if (n->right && n->right != n)
stack.push_back(n->right);
}
for (fh_node *p : all)
delete p;
}
delete H;
}
// insert and return node pointer
fh_node *fh_insert(fibheap_c *H, int vertex, int key)
{
if (!H)
return nullptr;
fh_node *x = create_node(vertex, key);
if (!H->minptr)
{
H->minptr = x;
}
else
{
x->right = H->minptr->right;
x->left = H->minptr;
H->minptr->right->left = x;
H->minptr->right = x;
if (x->key < H->minptr->key)
H->minptr = x;
}
H->nodes++;
return x;
}
// extract min node and return it (caller should delete)
fh_node *fh_extract_min_node(fibheap_c *H)
{
if (!H || !H->minptr)
return nullptr;
fh_node *z = H->minptr;
move_children_to_root(H, z);
// remove z from root list
if (z->left == z && z->right == z)
{
H->minptr = nullptr;
}
else
{
z->left->right = z->right;
z->right->left = z->left;
H->minptr = z->right; // temporary
}
// isolate z
z->left = z->right = z;
H->nodes--;
if (H->minptr)
consolidate(H);
return z;
}
// cut x from parent and move to root list
static void cut_node_to_root(fibheap_c *H, fh_node *x, fh_node *par)
{
if (!x || !par)
return;
if (x->right == x)
{
par->child = nullptr;
}
else
{
if (par->child == x)
par->child = x->right;
x->right->left = x->left;
x->left->right = x->right;
}
par->degree--;
x->parent = nullptr;
x->mark = false;
// add x to root list
if (!H->minptr)
{
x->left = x->right = x;
H->minptr = x;
}
else
{
x->right = H->minptr->right;
x->left = H->minptr;
H->minptr->right->left = x;
H->minptr->right = x;
if (x->key < H->minptr->key)
H->minptr = x;
}
}
// cascading cut
static void cascading_cut(fibheap_c *H, fh_node *y)
{
fh_node *z = y->parent;
if (!z)
return;
if (!y->mark)
{
y->mark = true;
}
else
{
cut_node_to_root(H, y, z);
cascading_cut(H, z);
}
}
// decrease key of node x
void fh_decrease_key(fibheap_c *H, fh_node *x, int newKey)
{
if (!H || !x)
return;
if (newKey >= x->key)
return;
x->key = newKey;
fh_node *y = x->parent;
if (y && x->key < y->key)
{
cut_node_to_root(H, x, y);
cascading_cut(H, y);
}
if (!H->minptr || x->key < H->minptr->key)
H->minptr = x;
}
// check empty
bool fh_empty(const fibheap_c *H)
{
return !H || H->minptr == nullptr;
}