-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfibheap.c
224 lines (183 loc) · 4.21 KB
/
fibheap.c
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
/**
* author: Liu Zhiyong<[email protected]>
*/
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "fibheap.h"
static fibnode_t *fibnode_remove(fibnode_t *node)
{
fibnode_t *ret = (node != node->left) ? node->left : NULL;
node->right->left = node->left;
node->left->right = node->right;
node->left = node;
node->right = node;
return ret;
}
static void fibnode_insert(fibnode_t *a, fibnode_t *b)
{
b->left = a;
b->right = a->right;
a->right->left = b;
a->right = b;
}
static void fibnode_union(fibnode_t *a, fibnode_t *b)
{
fibnode_t *atail = a->right;
fibnode_t *btail = b->right;
atail->left = b;
b->right = atail;
a->right = btail;
btail->left = a;
}
static void fibheap_link(fibnode_t *node, fibnode_t *parent)
{
if (parent->child == NULL)
parent->child = node;
else
fibnode_insert(parent->child, node);
node->parent = parent;
node->mark = 0;
parent->degree++;
}
static void fibheap_cut(fibheap_t *fib, fibnode_t *node, fibnode_t *parent)
{
parent->degree--;
parent->child = fibnode_remove(node);
node->parent = NULL;
node->mark = 0;
fibnode_insert(fib->min, node);
}
static void __fibheap_insert(fibheap_t *fib, fibnode_t *node)
{
if (fib->min == NULL) {
fib->min = node;
} else {
fibnode_insert(fib->min, node);
if (node->key < fib->min->key)
fib->min = node;
}
}
static void fibheap_consolidate(fibheap_t *fib)
{
int i, max = 0;
fibnode_t *tmp, *node, *min, *stack[1 + 8 * sizeof(long)] = {NULL};
while ((min = fib->min) != NULL) {
fib->min = fibnode_remove(min);
for (i = min->degree; stack[i] != NULL; i++) {
node = stack[i], stack[i] = NULL;
if (min->key > node->key)
tmp = min, min = node, node = tmp;
fibheap_link(node, min);
}
stack[i] = min;
if (i > max)
max = i;
}
assert(max < 1 + 8 * sizeof(long));
for (i = 0; i <= max; i++) {
if (stack[i])
__fibheap_insert(fib, stack[i]);
}
}
static void fibheap_cascading_cut(fibheap_t *fib, fibnode_t *node)
{
fibnode_t *parent;
while ((parent = node->parent) != NULL) {
if (node->mark == 0) {
node->mark = 1;
return;
}
fibheap_cut(fib, node, parent);
node = parent;
}
}
static fibnode_t *__fibheap_extract_min(fibheap_t *fib)
{
fibnode_t *n, *min = fib->min;
// 将min的子节点合并到堆的根节点上
if (min->child) {
min->child->parent = NULL;
for (n = min->child->left; n != min->child; n = n->left)
n->parent = NULL;
fibnode_union(min, min->child);
}
fib->nodes--;
fib->min = fibnode_remove(min);
fibheap_consolidate(fib);
FIBNODE_INIT(min);
return min;
}
void fibheap_init(fibheap_t *fib)
{
*fib = FIBHEAP_INIT();
}
void fibheap_fini(fibheap_t *fib)
{
while (fib->min) {
fibnode_t *min = __fibheap_extract_min(fib);
FIBNODE_INIT(min);
}
*fib = FIBHEAP_INIT();
}
int fibheap_insert(fibheap_t *fib, fibnode_t *node)
{
assert(node->key > FIBHEAP_MINKEY);
FIBNODE_INIT(node);
__fibheap_insert(fib, node);
fib->nodes++;
return 0;
}
fibnode_t *fibheap_min(fibheap_t *fib)
{
return fib->min ? fib->min : NULL;
}
fibnode_t *fibheap_extract_min(fibheap_t *fib)
{
return fib->min ? __fibheap_extract_min(fib) : NULL;
}
int fibheap_union(fibheap_t *fib, fibheap_t *other)
{
if (!fib->min || !other->min) {
if (!fib->min)
*fib = *other;
goto DONE;
}
// 链接两个堆的根的双向链表
fibnode_union(fib->min, other->min);
// 比较最小的节点
if (fib->min->key > other->min->key)
fib->min = other->min;
fib->nodes += other->nodes;
DONE:
*other = FIBHEAP_INIT();
return 0;
}
static int __fibheap_decrease_key(fibheap_t *fib, fibnode_t *node, long key)
{
fibnode_t *parent = node->parent;
node->key = key;
if (parent && node->key <= parent->key) {
fibheap_cut(fib, node, parent);
fibheap_cascading_cut(fib, parent);
}
if (node->key <= fib->min->key)
fib->min = node;
return 0;
}
int fibheap_decrease_key(fibheap_t *fib, fibnode_t *node, long key)
{
assert(key > FIBHEAP_MINKEY && node->key > key);
return node->key != key ? __fibheap_decrease_key(fib, node, key) : 0;
}
int fibheap_delete(fibheap_t *fib, fibnode_t *node)
{
long okey = node->key;
__fibheap_decrease_key(fib, node, FIBHEAP_MINKEY);
assert(node == fib->min);
__fibheap_extract_min(fib);
node->key = okey;
return 0;
}