-
Notifications
You must be signed in to change notification settings - Fork 1
/
024_19.1_FibHeap.cpp
409 lines (352 loc) · 8.9 KB
/
024_19.1_FibHeap.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
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
//斐波那契结点ADT
typedef struct FibNode {
int key; //结点
int degree; //度
FibNode *left; //左兄弟
FibNode *right; //右兄弟
FibNode *parent; //父结点
FibNode *child; //第一个孩子结点
bool marked; //是否被删除第1个孩子
} FibNode;
//斐波那契堆ADT
typedef struct FibHeap {
int keyNum; //堆中结点个数
FibNode *min; //最小堆,根结点
int maxNumOfDegree; //最大度
FibNode **cons; //指向最大度的内存区域
} FibHeap;
/*****************函数申明*************************/
//将x从双链表移除
inline void FibNodeRemove(FibNode *x);
//将x堆结点加入y结点之前(循环链表中)
inline void FibNodeAdd(FibNode *x, FibNode *y);
//初始化一个空的Fibonacci Heap
FibHeap *FibHeapMake();
//初始化结点x
FibNode *FibNodeMake();
//堆结点x插入fibonacci heap中
void FibHeapInsert(FibHeap *heap, FibNode *x);
//将数组内的值插入Fibonacci Heap
void FibHeapInsertKeys(FibHeap *heap, int keys[], int keyNum);
//将值插入Fibonacci Heap
static void FibHeapInsertKey(FibHeap *heap, int key);
//抽取最小结点
FibNode *FibHeapExtractMin(FibHeap *heap);
//合并左右相同度数的二项树
void FibHeapConsolidate(FibHeap *heap);
//将x根结点链接到y根结点
void FibHeapLink(FibHeap *heap, FibNode *x, FibNode *y);
//开辟FibHeapConsolidate函数哈希所用空间
static void FibHeapConsMake(FibHeap *heap);
//将堆的最小结点移出,并指向其有兄弟
static FibNode *FibHeapMinRemove(FibHeap *heap);
//减小一个关键字
void FibHeapDecrease(FibHeap *heap, FibNode *x, int key);
//切断x与父节点y之间的链接,使x成为一个根
static void FibHeapCut(FibHeap *heap, FibNode *x, FibNode *y);
//级联剪切
static void FibHeapCascadingCut(FibHeap *heap, FibNode *y);
//修改度数
void reNewDegree(FibNode *parent, int degree);
//删除结点
void FibHeapDelete(FibHeap *heap, FibNode *x);
//堆内搜索关键字
FibNode * FibHeapSearch(FibHeap *heap, int key);
//被FibHeapSearch调用
static FibNode *FibNodeSearch(FibNode *x, int key);
//销毁堆
void FibHeapDestory(FibHeap *heap);
//被FibHeapDestory调用
static void FibNodeDestory(FibNode *x);
//输出打印堆
static void FibHeapPrint(FibHeap * heap);
//被FibHeapPrint调用
static void FibNodePrint(FibNode * x);
/************************************************/
//将x从双链表移除
inline void FibNodeRemove(FibNode *x) {
x->left->right = x->right;
x->right->left = x->left;
}
/*
将x堆结点加入y结点之前(循环链表中)
a …… y
a …… x …… y
*/
inline void FibNodeAdd(FibNode *x, FibNode *y) {
x->left = y->left;
y->left->right = x;
x->right = y;
y->left = x;
}
//初始化一个空的Fibonacci Heap
FibHeap *FibHeapMake() {
FibHeap *heap = NULL;
heap = (FibHeap *)malloc(sizeof(FibHeap));
if (NULL == heap) {
puts("Out of Space!!");
exit(1);
}
memset(heap, 0, sizeof(FibHeap));
return heap;
}
//初始化结点x
FibNode *FibNodeMake() {
FibNode *x = NULL;
x = (FibNode *)malloc(sizeof(FibNode));
if (NULL == x) {
puts("Out of Space!!");
exit(1);
}
memset(x, 0, sizeof(FibNode));
x->left = x->right = x;
return x;
}
//堆结点x插入fibonacci heap中
void FibHeapInsert(FibHeap *heap, FibNode *x) {
if(0 == heap->keyNum) {
heap->min = x;
} else {
FibNodeAdd(x, heap->min);
x->parent = NULL;
if(x->key < heap->min->key) {
heap->min = x;
}
}
heap->maxNumOfDegree++;
}
//将数组内的值插入Fibonacci Heap
void FibHeapInsertKeys(FibHeap *heap, int keys[], int keyNum) {
for(int i = 0; i < keyNum; i++) {
FibHeapInsertKey(heap, keys[i]);
}
}
//将值插入Fibonacci Heap
static void FibHeapInsertKey(FibHeap *heap, int key) {
FibNode *x = NULL;
x = FibNodeMake();
x->key = key;
FibHeapInsert(heap, x);
}
FibNode *FibHeapExtractMin(FibHeap *heap) {
FibNode *x = NULL;
FibNode *z = heap->min;
if(z != NULL) {
//删除z的每一个孩子
while(NULL != z->child) {
x = z->child;
FibNodeRemove(x);
if(x->right == x) {
x->child = NULL;
} else {
z->child = x->right;
}
FibNodeAdd(x, z);//add x to the root list heap
x->parent = NULL;
}
FibNodeRemove(z);
if(z->right == z) {
heap->min = NULL;
} else {
heap->min = z->right;
FibHeapConsolidate(heap);
}
heap->keyNum--;
}
return z;
}
//合并左右相同度数的二项树
void FibHeapConsolidate(FibHeap *heap) {
int D, d;
FibNode *w = heap->min;
FibNode *x = NULL, *y = NULL;
FibHeapConsMake(heap); //开辟哈希所用空间
D = heap->maxNumOfDegree + 1;
for(int i = 0; i < D; i++) {
*(heap->cons + i) = NULL;
}
//合并相同度的根节点,使每个度数的二项树唯一
while(NULL != heap->min) {
x = FibHeapMinRemove(heap);
d = x->degree;
while(NULL != *(heap->cons + d)) {
y = *(heap->cons + d);
if(x->key > y->key) {
swap(x, y);
}
FibHeapLink(heap, y, x); //y从兄弟变成x的孩子
*(heap->cons + d) = NULL;
d++;
}
*(heap->cons + d) = x;
}
heap->min = NULL;//原有根表清除
//将heap->cons中结点都重新加到根表中,且找出最小根
for(int i = 0; i < D; i++) {
if(*(heap->cons + i) != NULL) {
if(heap->min == NULL) {
heap->min = *(heap->cons + i);
} else {
FibNodeAdd(*(heap->cons + i), heap->min);
if((*(heap->cons + i))->key < heap->min->key) {
heap->min = *(heap->cons + i);
}
}
}
}
}
//将x根结点链接到y根结点
void FibHeapLink(FibHeap *heap, FibNode *x, FibNode *y) {
FibNodeRemove(x);
if(NULL == y->child) {
y->child = x;
} else {
FibNodeAdd(x, y->child);
}
x->parent = y;
y->degree++;
x->marked = false;
}
//开辟FibHeapConsolidate函数哈希所用空间
static void FibHeapConsMake(FibHeap *heap) {
int old = heap->maxNumOfDegree;
heap->maxNumOfDegree = int(log(heap->keyNum * 1.0) / log(2.0)) + 1;
if(old < heap->maxNumOfDegree) {
//因为度为heap->maxNumOfDegree可能被合并,所以要maxNumOfDegree + 1
heap->cons = (FibNode **)realloc(heap->cons, sizeof(FibNode *) * (heap->maxNumOfDegree + 1));
if(NULL == heap->cons) {
puts("out of space");
exit(1);
}
}
}
//将堆的最小结点移出,并指向其右兄弟
static FibNode *FibHeapMinRemove(FibHeap *heap) {
FibNode *min = heap->min;
if(heap->min == min->right) {
heap->min = NULL;
} else {
FibNodeRemove(min);
heap->min = min->right;
}
min->left = min->right = min;
return min;
}
//减小一个关键字
void FibHeapDecrease(FibHeap *heap, FibNode *x, int key) {
FibNode *y = x->parent;
if(x->key < key) {
puts("new key is greater than current key!");
exit(1);
}
x->key = key;
if(NULL != y && x->key < y->key) {
//破坏了最小堆性质,需要进行级联剪切操作
//切断x与父节点y之间的链接,使x成为一个根
FibHeapCut(heap, x, y);
//级联剪切
FibHeapCascadingCut(heap, y);
}
}
//切断x与父节点y之间的链接,使x成为一个根
static void FibHeapCut(FibHeap *heap, FibNode *x, FibNode *y) {
FibNodeRemove(x);
reNewDegree(y, x->degree);
if(x == x->right) {
y->child = NULL;
} else {
y->child = x->right;
}
x->parent = NULL;
x->left = x->right = x;
x->marked = false;
FibNodeAdd(x, heap->min);
}
//级联剪切
static void FibHeapCascadingCut(FibHeap *heap, FibNode *y) {
FibNode *z = y->parent;
if(NULL != z) {
if(y->marked = false) {
y->marked = true;
} else {
FibHeapCut(heap, y, z);
FibHeapCascadingCut(heap, z);
}
}
}
//修改度数
void reNewDegree(FibNode *parent, int degree) {
parent->degree -= degree;
if(parent -> parent != NULL) {
reNewDegree(parent->parent, degree);
}
}
//删除结点
void FibHeapDelete(FibHeap * heap, FibNode * x) {
FibHeapDecrease(heap, x, INT_MIN);
FibHeapExtractMin(heap);
}
//堆内搜索关键字
FibNode *FibHeapSearch(FibHeap *heap, int key) {
return FibNodeSearch(heap->min, key);
}
//被FibHeapSearch调用
static FibNode *FibNodeSearch(FibNode *x, int key) {
FibNode *w = x, *y = NULL;
if(x != NULL) {
do
{
if(w->key == key) {
y = w;
break;
} else if(NULL != (y = FibNodeSearch(w->child, key))) {
break;
}
w = w->right;
} while (w != x);
}
return y;
}
//销毁堆
void FibHeapDestory(FibHeap * heap) {
FibNodeDestory(heap->min);
free(heap);
heap = NULL;
}
//被FibHeapDestory调用
static void FibNodeDestory(FibNode * x) {
FibNode * p = x, *q = NULL;
while (p != NULL) {
FibNodeDestory(p->child);
q = p;
if (p == x) {
p = NULL;
} else {
p = p->left;
}
free(q->right);
}
}
//输出打印堆
static void FibHeapPrint(FibHeap * heap) {
printf("The keyNum = %d\n", heap->keyNum);
FibNodePrint(heap->min);
puts("\n");
};
static void FibNodePrint(FibNode *x) {
FibNode *p = NULL;
if(NULL == x) {
return;
}
p = x;
do
{
printf(" (");
printf("%d", p->key);
if(p->child != NULL) {
FibNodePrint(p->child);
}
printf(" )");
p = p->left;
} while (x != p);
}