-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibheap.cpp
More file actions
253 lines (210 loc) · 5.11 KB
/
Copy pathfibheap.cpp
File metadata and controls
253 lines (210 loc) · 5.11 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
/*
* FibHeap.cpp
*
* Created on: Oct 7, 2021
* Author: d-w-h
*/
#include <vector>
#include "memory.hpp"
#include "usertypes.hpp"
void fibHeapInsert(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 makeChildOf(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 linkDupDegree(FibHeap * H, Node ** A, Node *& x) {
int d = x->degree;
if(A[d] == x) //Don't link Nodes to themselves
return;
while(A[d] != NULL) {
Node * y = A[d];
//Link x and y
if(x != y) {
if(y->key > x->key) {
//Make y child of x
makeChildOf(H, y, x);
if(y == H->min) {
H->min = x;
}
}
else {
//Make x child of y
makeChildOf(H, x, y);
//Reset root Node and root list tracker
H->min = y;
x = H->min;
}
A[d] = NULL;
d = d + 1;
}
else {
A[d] = NULL;
}
}
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 = getNodeRef(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 {
linkDupDegree(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 reference
delete [] A;
}
void nullifyChildrenParentNode(Node * z) {
Node * xt = z->child;
if(xt != NULL) {
do {
xt->p = NULL;
xt = xt->right;
} while(xt != z->child);
}
}
Node * fibHeapExtractMin(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
nullifyChildrenParentNode(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 cascadingCut(FibHeap * H, Node * y) {
Node * z = y->p;
if(z != NULL) {
if(y->mark == false) {
y->mark = true;
}
else {
cut(H, y, z);
cascadingCut(H, z);
}
}
}
void fibHeapDecreaseKey(FibHeap * H, Node * x, int k) {
if(k > x->key) {
throw "key invalid";
}
x->key = k;
Node * y = x->p;
if(y != NULL && x->key < y->key) {
cut(H, x, y);
cascadingCut(H, y);
}
if(x->key < H->min->key) {
H->min = x;
}
}