-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFibheap.h
569 lines (470 loc) · 12.2 KB
/
Fibheap.h
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*
Fibonacci Heap:
This is the Fibonacci Heap class and its function we implemented
for our centralized queue
*/
#ifndef Fibheap_h
#define Fibheap_h
#include<cmath>
#include<algorithm>
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <vector>
//#include<io.h>
#include "patient.h"
#include <math.h>
#include "alist_func.h"
using namespace std;
class FibNode : public patient {
public:
int key; // priority
int degree;
FibNode *left;
FibNode *right;
FibNode *child; // first child node
FibNode *parent;
bool marked;
int priority(); //for calculating priority key
long int timestamp; //timestamp for each person
//Constructer
FibNode() {
degree = 0;
marked = false;
left = this;
right = this;
parent = NULL;
child = NULL;
}
};
//For calculating priority
int FibNode::priority(){
//Start from 0 each time
int ret=0;
//Those of high risks will never be treated unless there are no people waiting
if(risk==3) ret+=100000;
//Those with suppose greater than 0 will never be popped before those can be popped immediately
//and it will also be judeged, if larger than 0, no pops
if(suppose>0){
ret+=1000;
}
//priority calculations within order
ret+=waiting+age*10+profession*100;
return ret;
}
//Centralized queue
class FibHeap {
public:
int keyNum; //Node numbers
int maxDegree;
FibNode *min;
FibNode **cons; //for consolidates
/*
When waiting time exceeds this, pop out unconditionally
Can be adjusted by requirements
*/
int Deadline;
FibHeap();
~FibHeap();
//get the minimum pointer
FibNode* getmin();
//For Updates everyday
void daily(FibNode* root);
//For Withdrawals
void withdraw(int ID);
// For adding to centralized
void insert(FibNode *node);
void insert2(FibNode* node);
void addNode(FibNode *node, FibNode *root);
//for reports
void traverse(AList<FibNode*>& List, FibNode* root);
// For popping the minimum into hospitals
void removeMin();
bool checkMin();
void removeNode(FibNode *node);
FibNode* extractMin();
void consolidate();
void link(FibNode* node, FibNode* root);
void makeCons();
//For updates
void update(int identity, int newval,int item);
void decrease(FibNode *node, int newkey,int item);
void increase(FibNode *node, int newkey,int item);
void update(FibNode *node, int newkey,int item);
FibNode* search(FibNode *root, int identity);
FibNode* search(int identity);
//Helper functions
bool contains(int identity);
void cut(FibNode *node, FibNode *parent);
void cascadingCut(FibNode *node);
void renewDegree(FibNode *parent, int degree);
};
void FibHeap:: traverse(AList<FibNode*>& List, FibNode* root){
if(root==NULL) return;
List.append(root);
FibNode* t=root;
do
{
traverse(List,t->child);
t = t->right;
} while (t != root);
}
//Constructor
FibHeap::FibHeap()
{
keyNum = 0;
maxDegree = 0;
min = NULL;
cons = NULL;
//can be adjusted
Deadline=5;
}
FibHeap::~FibHeap()
{
}
//From Locals to Centralized
void FibHeap::addNode(FibNode *node, FibNode *root)
{
node->left = root->left;
root->left->right = node;
node->right = root;
root->left = node;
}
void FibHeap::insert(FibNode *node)
{
//If the person to be inserted if already in FibHeap
//We ask if the user want to update this person's information
if(contains(node->ID)){
char flg;
cout<<"This person is already there!\n";
cout<<"Do you want to update his information?y(yes)n(no)\n";
cin>>flg;
if(flg=='n') return;
cout<<"Which item you want to update his information?profession(0)risk(1)\n";
int choice;
cin>>choice;
cout<<"To what?\n";
int newkey;
cin>>newkey;
update(node,newkey,choice);
return;
}
if (keyNum == 0) min = node;
else{
addNode(node, min);
if (node->key < min->key)
min = node;
}
keyNum++;
}
//From Centralized to Hospitals
void FibHeap::removeNode(FibNode *node)
{
node->left->right = node->right;
node->right->left = node->left;
}
bool FibHeap::checkMin()
{
FibNode* m = min;
if (min==NULL){
return 0;
}
if(m->suppose>0) return 0;
return 1;
}
void FibHeap::removeMin()
{
if (min==NULL){
return;
}
FibNode* child=NULL;
FibNode* m = min;
while (m->child != NULL)
{
child = m->child;
removeNode(child);
if (child->right == child)
m->child = NULL;
else
m->child = child->right;
addNode(child, min);
child->parent = NULL;
}
removeNode(m);
if(m->right == m) {
min = NULL;
}
else{
min = m->right;
consolidate();
}
keyNum--;
}
void FibHeap::link(FibNode* node, FibNode* root)
{
removeNode(node);
if (root->child == NULL)
root->child = node;
else
addNode(node, root->child);
node->parent = root;
root->degree++;
node->marked = false;
}
//make rooms for consolidates
void FibHeap::makeCons()
{
int old = maxDegree;
maxDegree = (log(keyNum)/log(2.0)) + 1;
if (old >= maxDegree)
return ;
cons = (FibNode **)realloc(cons, sizeof(FibHeap*) * (maxDegree + 1));
}
void FibHeap::consolidate()
{
int i;
int d;
int D;
FibNode* x;
FibNode* y;
FibNode* tmp;
makeCons();
D = maxDegree + 1;
for (i = 0; i < D; i++)
cons[i] = NULL;
while (min != NULL){
x = extractMin();
d = x->degree;
while (cons[d] != NULL){
y = cons[d];
if (x->key > y->key){
swap(x, y);
}
link(y, x);
cons[d] = NULL;
d++;
}
cons[d] = x;
}
min = NULL;
for(i=0; i<D; i++)
{
if (cons[i] != NULL)
{
if (min == NULL)
min = cons[i];
else
{
addNode(cons[i], min);
if ((cons[i])->key < min->key)
min = cons[i];
}
}
}
}
//Updates
FibNode* FibHeap::search(FibNode *root, int identity)
{
FibNode *t = root;
FibNode *p = NULL;
if (root==NULL)
return root;
do
{
if (t->ID == identity)
{
p = t;
break;
}
else
{
if ((p = search(t->child, identity)) != NULL)
break;
}
t = t->right;
} while (t != root);
return p;
}
FibNode* FibHeap::search(int identity)
{
if (min==NULL)
return NULL;
return search(min, identity);
}
bool FibHeap::contains(int identity)
{
return search(identity)!=NULL ? true: false;
}
void FibHeap::decrease(FibNode *node, int newkey,int item){
//item 0 means we do PROFESSION adjustments
FibNode* parent;
if (min==NULL ||node==NULL) return ;
if(item==0) node->profession = newkey;
else{
node->risk=newkey;
if(node->risk==0 || node->risk==1) node->suppose=0;
else if(node->risk==2) node->suppose=30;
}
node->key=node->priority();
parent = node->parent;
if (parent!=NULL && node->key < parent->key)
{
cut(node, parent);
cascadingCut(parent);
}
if (node->key < min->key) min = node;
return;
}
void FibHeap::cut(FibNode *node, FibNode *parent)
{
removeNode(node);
renewDegree(parent, node->degree);
if (node == node->right)
parent->child = NULL;
else
parent->child = node->right;
node->parent = NULL;
node->left = node->right = node;
node->marked = false;
addNode(node, min);
}
void FibHeap::cascadingCut(FibNode *node)
{
FibNode *parent = node->parent;
if (parent != NULL)
{
if (node->marked == false)
node->marked = true;
else
{
cut(node, parent);
cascadingCut(parent);
}
}
}
void FibHeap::renewDegree(FibNode *parent, int degree)
{
parent->degree -= degree;
if (parent-> parent != NULL)
renewDegree(parent->parent, degree);
}
void FibHeap::increase(FibNode *node, int newkey, int item)
{
FibNode *child, *parent, *right;
if (min==NULL ||node==NULL)
return ;
while (node->child != NULL)
{
child = node->child;
removeNode(child);
if (child->right == child)
node->child = NULL;
else
node->child = child->right;
addNode(child, min);
child->parent = NULL;
}
node->degree = 0;
if(item==0) node->profession = newkey;
else{
node->risk=newkey;
if(node->risk==0 || node->risk==1) node->suppose=0;
else if(node->risk==2) node->suppose=30;
}
node->key=node->priority();
parent = node->parent;
if(parent != NULL)
{
cut(node, parent);
cascadingCut(parent);
}
else if(min == node)
{
right = node->right;
while(right != node)
{
if(node->key > right->key)
min = right;
right = right->right;
}
}
}
void FibHeap::update(FibNode *node, int newkey,int item){
if(item==0 && newkey < node->profession)
decrease(node, newkey,item);
else if(item==0 && newkey > node->profession)
increase(node, newkey,item);
else if(item==1&&newkey > node->risk)
increase(node, newkey,item);
else
decrease(node,newkey,item);
}
void FibHeap::update(int identity, int newval,int item)
{
FibNode *node;
node = search(identity);
if (node!=NULL)
update(node, newval,item);
}
void FibHeap::withdraw(int identity){
FibNode* removal=search(identity);
if(removal==NULL){
return;
}
WithDrawals.push_back(identity);
removal->left->right = removal->right;
removal->right->left = removal->left;
keyNum--;
}
//Daily Updates
void FibHeap::daily(FibNode* root){
if(root==NULL) return;
root->suppose-=0.5;
root->waiting+=0.5;
if(root->waiting>=Deadline){
root->key=0;
}else{
root->key=root->priority();
}
FibNode* t=root;
do
{
daily(t->child);
t = t->right;
} while (t != root);
}
FibNode* FibHeap::extractMin()
{
FibNode *p = min;
if (p == p->right)
min = NULL;
else
{
removeNode(p);
min = p->right;
}
p->left = p->right = p;
return p;
}
FibNode* FibHeap:: getmin(){
return min;
};
void FibHeap::insert2(FibNode *node)
{
if(contains(node->ID)){
char flg;
cout<<"This person is already there! we update it\n";
update(node,node->profession,0);
update(node,node->risk,1);
return;
}
if (keyNum == 0) min = node;
else{
addNode(node, min);
if (node->key < min->key)
min = node;
}
keyNum++;
}
#endif