-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfib_heap.cpp
261 lines (213 loc) · 5.21 KB
/
fib_heap.cpp
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
/*
* fib_heap.cpp
*
* Created on: Oct 7, 2021
* Author: d-w-h
*/
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <cstdlib>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include "memory.hpp"
#include "user_types.hpp"
void fib_heap_insert(FibHeap* H, node* x) {
x->degree = 0;
x->p = NULL;
x->child = NULL;
x->mark = false;
if(H->min == NULL) {
x->left = x;
x->right = x;
H->min = x;
H->n = 0;
}
else {
x->left = H->min;
x->right = H->min->right;
H->min->right->left = x;
H->min->right = x;
if(x->key < H->min->key) {
H->min = x;
}
}
H->n = H->n + 1;
}
void make_child_of(FibHeap* H, node* y, node* x) {
//Remove node from root list
y->left->right = y->right;
y->right->left = y->left;
if(x->child == NULL) {
x->child = y;
y->p = x;
y->left = y;
y->right = y;
}
else {
y->left = x->child;
y->right = x->child->right;
y->p = x;
x->child->right->left = y;
x->child->right = y;
}
//Set mark
y->mark = false;
x->degree = x->degree + 1;
}
void link_dup_degree(FibHeap* H, node** A, node*& x) {
int d = x->degree;
if(A[d] != x) { //Don't link nodes to themselves
while(A[d] != NULL) {
node* y = A[d];
//Link x and y
if(y->key > x->key) {
//Make y child of x
make_child_of(H, y, x);
if(y == H->min) {
H->min = x;
}
}
else {
//Make x child of y
make_child_of(H, x, y);
//Reset root node and root list tracker
H->min = y;
x = H->min;
}
A[d] = NULL;
d = d + 1;
}
A[d] = x;
}
}
void consolidate(FibHeap* H) {
//Compute upper bound root list
double golden = (1.0 + sqrt(5.0)) / 2.0;
double f = log(H->n) / log(golden);
int D = floor(f + 0.01) + 1;
//Allocate memory for root list construction
node** A = new node*[D + 1];
for(int i = 0; i < D + 1; ++i) {
A[i] = NULL;
}
//Ensure all root nodes have unique degrees
node* x = H->min;
if(x != NULL) {
do {
link_dup_degree(H, A, x);
x = x->right;
} while(x != H->min);
}
//Reconstruct root list
H->min = NULL;
for(int i = 0; i < D + 1; ++i) {
if(A[i] != NULL) {
if(H->min == NULL) {
A[i]->left = A[i];
A[i]->right = A[i];
A[i]->p = NULL;
H->min = A[i];
}
else {
A[i]->left = H->min;
A[i]->right = H->min->right;
H->min->right->left = A[i];
H->min->right = A[i];
A[i]->p = NULL;
if(A[i]->key < H->min->key) {
H->min = A[i];
}
}
}
}
//Free root list references
delete [] A;
}
void nullify_children_parent_node(node* z) {
node* xt = z->child;
if(xt != NULL) {
do {
xt->p = NULL;
xt = xt->right;
} while(xt != z->child);
}
}
node* fib_heap_extract_min(FibHeap* H) {
node* z = H->min;
if(z != NULL) {
//Add each child of z to root list
node* y = z->child;
if(y != NULL) {
//Set children's parent node to NULL
nullify_children_parent_node(z);
y->left->right = z->right;
z->right->left = y->left;
y->left = z;
z->right = y;
z->degree = 0;
z->child = NULL;
}
//Remove z from root list
z->left->right = z->right;
z->right->left = z->left;
if(z == z->right) {
H->min = NULL;
}
else {
H->min = z->right;
consolidate(H);
}
H->n = H->n - 1;
}
return z;
}
void cut(FibHeap* H, node* x, node* y) {
//If x is only child set child of parent to null
if(x == x->right) {
y->child = NULL;
y->degree = 0;
}
else {
y->child = x->right;
y->degree = y->degree - 1;
}
//Remove x from child list of y and add x to root list of H
x->left->right = x->right;
x->right->left = x->left;
x->right = H->min->right;
x->left = H->min;
H->min->right->left = x;
H->min->right = x;
x->p = NULL;
x->mark = false;
}
void cascading_cut(FibHeap* H, node* y) {
node* z = y->p;
if(z != NULL) {
if(y->mark == false) {
y->mark = true;
}
else {
cut(H, y, z);
cascading_cut(H, z);
}
}
}
void fib_heap_decrease_key(FibHeap* H, node* x, int k) {
if(k > x->key) {
const char* s = "new key is greater than current key";
std::cout << s << std::endl;
throw s;
}
x->key = k;
node* y = x->p;
if(y != NULL && x->key < y->key) {
cut(H, x, y);
cascading_cut(H, y);
}
if(x->key < H->min->key) {
H->min = x;
}
}