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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
|
/**
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*
* (c) Vincenzo Nicosia 2009-2017 -- <v.nicosia@qmul.ac.uk>
*
* This file is part of NetBunch, a package for complex network
* analysis and modelling. For more information please visit:
*
* http://www.complex-networks.net/
*
* If you use this software, please add a reference to
*
* V. Latora, V. Nicosia, G. Russo
* "Complex Networks: Principles, Methods and Applications"
* Cambridge University Press (2017)
* ISBN: 9781107103184
*
***********************************************************************
*
* This is an implementation of a Binary Search Tree augmented by a
* Priority Queue. This data structure is central to the
* implementation of the Clauset-Newman-Moore greedy modularity
* optimisation algorithm
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include "utils.h"
#include "bst_pq.h"
/* BST-related functions */
void __recursive_preorder(node_t *cur, ilfunc_t *funs){
if(cur->left){
__recursive_preorder(cur->left, funs);
}
if(cur -> active)
funs->print(cur->info, funs->fileout);
if(cur->right){
__recursive_preorder(cur->right, funs);
}
}
/*
*
* Recursive push of nodes in the nodecache :-)
*
*/
void __recursive_destroy(node_t *cur, ilfunc_t *funs){
if(cur->left){
__recursive_destroy(cur->left, funs);
cur->left = NULL;
}
if(cur->right){
__recursive_destroy(cur->right, funs);
cur->right = NULL;
}
}
int __recursive_insert(node_t *cur, node_t *elem, ilfunc_t *f){
int res ;
res = f->compare(cur->info, elem->info);
/* printf("res: %d\n", res); */
assert(res != 0);
if ( res > 0){
if (cur->left){
return __recursive_insert(cur->left, elem, f);
}
else{
cur->left = elem;
elem->parent = cur;
return 0;
}
}
else if (res < 0){
if (cur->right){
return __recursive_insert(cur->right, elem, f);
}
else{
cur->right = elem;
elem->parent = cur;
return 0;
}
}
if (cur -> active){
printf("warning!!!!! duplicate entry!!!!!!\n\n");
exit(1);
}
else
cur->active = 1;
return -1;
}
void* __recursive_lookup(node_t *cur, int v, ilfunc_t *f){
int res;
res = f->compare(cur->info, v);
if (res > 0){
if(cur->left)
return __recursive_lookup(cur->left, v, f);
else
return NULL;
}
else if (res < 0){
if(cur->right)
return __recursive_lookup(cur->right, v, f);
else
return NULL;
}
else{ /* res == 0, we found the element we were looking for */
return cur;
}
}
node_t* __recursive_getmin(node_t *cur){
if(cur->left){
return __recursive_getmin(cur->left);
}
else{
return cur;
}
}
node_t* __tree_getmin(node_t *n){
if (!n){
return NULL;
}
else{
return __recursive_getmin(n);
}
}
/* This is used by __tree__delete to put another tree v in place of
the current node u */
void __tree_transplant(bst_pq_t t, node_t *u, node_t * v){
if (u->parent == NULL){
t->root = v;
}
else if(u == u->parent->left){
u->parent->left = v;
}
else{
u->parent->right = v;
}
if (v != NULL){
v->parent = u->parent;
}
}
void __tree_delete(bst_pq_t t, node_t *z){
node_t *y;
if (z == NULL)
return;
if (z->left == NULL){
__tree_transplant(t, z, z->right);
}
else if(z->right == NULL){
__tree_transplant(t, z, z->left);
}
else{
y = __tree_getmin(z->right);
if (y->parent != z){
__tree_transplant(t, y, y->right);
y->right = z->right;
y->right->parent = y;
}
__tree_transplant(t, z, y);
y->left = z->left;
y->left->parent = y;
}
}
void bst_pq_tree_destroy(bst_pq_t t){
if(t->root)
__recursive_destroy(t->root, & (t->bst_funs));
free(t);
}
void __bst_pq_tree_view_pre(bst_pq_t t){
if (t->root){
printf("----\n");
__recursive_preorder(t->root, & (t->bst_funs));
printf("\n----\n");
}
else
printf("----- Empty tree!!!! -----\n");
}
void* __bst_pq_tree_lookup(bst_pq_t t , int elem){
if(t->root)
return __recursive_lookup(t->root, elem, & (t->bst_funs) );
else
return NULL;
}
/* void bst_pq_tree_map(bst_pq_t t, void (*func)(void*)){ */
/* __recursive_map(t->root, func); */
/* } */
/* void bst_pq_tree_map_args(bst_pq_t t, void (*func)(void*, void*), void *args){ */
/* __recursive_map_args(t->root, func, args); */
/* } */
void* bst_pq_tree_get_fileout(bst_pq_t t){
return t->bst_funs.fileout;
}
void bst_pq_tree_set_fileout(bst_pq_t t, void *f){
t->bst_funs.fileout = f;
}
node_t* __recursive_getmax(node_t *cur){
if(cur->right){
return __recursive_getmax(cur->right);
}
else{
return cur;
}
}
void* bst_pq_tree_getmax(bst_pq_t t){
if (!t){
return NULL;
}
else{
return __recursive_getmax(t->root);
}
}
/************************
* PQ-related functions *
************************/
void __update_handle(bst_pq_t q, int idx){
node_t *n;
n = (node_t*) q->v[idx];
n->index = idx;
//q->handles[q->pq_funs.get_id(q->v[idx])] = idx;
}
void __bst_pq_queue_sift_up(bst_pq_t q, int i){
int idx, parent;
void *tmp;
if ( q->last < 0)
return; /* no sifting if the PQ is empty!!! */
idx = i;
parent = PARENT(idx);
switch(q->qtype){
case MAX_QUEUE:
while ( idx >0 && q->pq_funs.compare(q->v[idx]->key, q->v[parent]->key) > 0){
tmp = q->v[idx];
q->v[idx] = q->v[parent];
q->v[parent] = tmp;
__update_handle(q, idx);
__update_handle(q, parent);
idx = parent;
parent = PARENT(idx);
}
break;
case MIN_QUEUE:
while ( idx >0 && q-> pq_funs.compare(q->v[idx]->key, q->v[parent]->key) < 0){
tmp = q->v[idx];
q->v[idx] = q->v[parent];
q->v[parent] = tmp;
__update_handle(q, idx);
__update_handle(q, parent);
idx = parent;
parent = PARENT(idx);
}
break;
}
}
void __bst_pq_queue_sift_down(bst_pq_t q, int i){
int left, right, largest, smallest;
void *tmp;
if ( q->last < 0)
return; /* no sifting if the PQ is empty!!! */
if( i > q->last)
return; /* no sifting if the index i is beyond the end of the PQ */
switch(q->qtype){
case MAX_QUEUE:
largest = i;
left = 2 * i + 1;
right = 2 * i + 2;
if (left <= q->last && q->pq_funs.compare(q->v[left]->key, q->v[largest]->key) > 0){
largest = left;
}
if (right <= q->last && q->pq_funs.compare(q->v[right]->key, q->v[largest]->key) > 0){
largest = right;
}
if (largest != i){
tmp = q->v[i];
q->v[i] = q->v[largest];
q->v[largest] = tmp;
__update_handle(q, i);
__update_handle(q, largest);
__bst_pq_queue_sift_down(q, largest);
}
else{
__update_handle(q, i);
}
break;
case MIN_QUEUE:
smallest = i;
left = 2 * i + 1;
right = 2 * i + 2;
if (left <= q->last && q->pq_funs.compare(q->v[left]->key, q->v[smallest]->key) < 0){
smallest = left;
}
if (right <= q->last && q->pq_funs.compare(q->v[right]->key, q->v[smallest]->key) < 0){
smallest = right;
}
if (smallest != i){
tmp = q->v[i];
q->v[i] = q->v[smallest];
q->v[smallest] = tmp;
__update_handle(q, i);
__update_handle(q, smallest);
__bst_pq_queue_sift_down(q, smallest);
}
else{
__update_handle(q, i);
}
break;
}
}
void __bst_pq_queue_insert(bst_pq_t q, void *elem){
//void *tmp;
if (q->last == q->N-1){
/* reallocate the array to arrange a few new elements */
q->N += 10;
q->v = realloc(q->v, (q->N) * sizeof(void*));
VALID_PTR_OR_EXIT(q->v, 17);
//q->v = tmp;
}
q->last += 1;
q->v[q->last] = elem;
__update_handle(q, q->last);
__bst_pq_queue_sift_up(q, q->last);
}
int __bst_pq_queue_delete_index(bst_pq_t q, int index){
if (q->last >=0 && index <= q->last){
//*val = q->v[0];
q->v[index] = q->v[q->last];
q->last -= 1;
__bst_pq_queue_sift_down(q, index);
return 0;
}
else{
return 1;
}
}
void* __bst_pq_queue_peek(bst_pq_t q){
if (q->last >= 0)
return q->v[0];
else
return NULL;
}
int __bst_pq_queue_force_key(bst_pq_t q, unsigned int idx, double key){
switch(q->qtype){
case MAX_QUEUE:
if (q->pq_funs.compare_to_key(q->v[idx], key) > 0){
//q->pq_funs.set_key(q->v[idx], key);
q->v[idx]->key = key;
__bst_pq_queue_sift_down(q, idx);
}
else{
//q->pq_funs.set_key(q->v[idx], key);
q->v[idx]->key = key;
__bst_pq_queue_sift_up(q, idx);
}
break;
case MIN_QUEUE:
if (q->pq_funs.compare_to_key(q->v[idx], key) < 0){
//q->pq_funs.set_key(q->v[idx], key);
q->v[idx]->key = key;
__bst_pq_queue_sift_down(q, idx);
}
else{
//q->pq_funs.set_key(q->v[idx], key);
q->v[idx]->key = key;
__bst_pq_queue_sift_up(q, idx);
}
break;
}
return 0;
}
void __bst_pq_queue_dump(bst_pq_t q){
int i;
unsigned int N;
N = q->last+1;
printf("N: %d last:%d root:", q->N, q->last);
if (q->last >=0)
q->pq_funs.print_elem(q->v[0]);
else
printf("NULL");
printf("\n");
for(i=0; i<N; i++){
if (i < (N+1)/2){
if (2*i+1 < N)
if (2*i + 2 < N){
printf("%d: ", i);
q->pq_funs.print_elem(q->v[i]);
printf(" (");
q->pq_funs.print_elem(q->v[2*i+1]);
printf(", ");
q->pq_funs.print_elem(q->v[2*i+2]);
printf(")\n");
}
else{
printf("%d: ", i);
q->pq_funs.print_elem(q->v[i]);
printf(" (");
q->pq_funs.print_elem(q->v[2*i+1]);
printf(", NULL)\n");
}
else{
printf("%d: ", i);
q->pq_funs.print_elem(q->v[i]);
printf(" (NULL, NULL)\n");
}
}
else{
printf("%d: ", i);
q->pq_funs.print_elem(q->v[i]);
printf(" (NULL, NULL)\n");
}
}
printf("\n");
}
/* bst_pq interface */
/* init the BST_PQ -- TESTED --*/
bst_pq_t bst_pq_create(ilfunc_t *bst_funs, gen_pqueue_func_t *pq_funs, char qtype,
unsigned int N, gen_stack_t *node_cache){
bst_pq_t b;
b = (bst_pq_t)malloc(sizeof(bst_pq_struct_t));
b->root = NULL;
b->bst_funs = *bst_funs;
b->bst_funs.fileout = stdout;
b->qtype = qtype;
b->N = N;
b->last = -1;
b->pq_funs = *pq_funs;
b->v = b->pq_funs.alloc_vector(N);
if (node_cache == NULL){
b->node_cache = malloc(sizeof(gen_stack_t));
gen_stack_create(b->node_cache);
}
else{
b->node_cache = node_cache;
}
return b;
}
/* insert a new element in the BST_PQ -- TESTED */
void bst_pq_insert(bst_pq_t b, unsigned int elem_info, double elem_key){
/* insert the element in the tree first */
node_t *n;
int err;
n = __bst_pq_tree_lookup(b, elem_info);
/* The following assert should fail ONLY if there is an auto-loop */
assert(n == NULL);
if (gen_stack_empty(b->node_cache)){
n = (node_t*)malloc(sizeof(node_t));
//n->info = b->bst_funs.alloc();
//n->key = b->pq_funs.alloc_key();
}
else{
err = gen_stack_pop(b->node_cache, (void**) &n);
if (err){
n = malloc(sizeof(node_t));
//n->info = b->bst_funs.alloc();
//n->key = b->pq_funs.alloc_key();
}
}
//b->bst_funs.copy(elem_info, n->info);
n->info = elem_info;
n->left = n->right = NULL;
if (b->root == NULL){
b->root = n;
n->parent = NULL;
}
else{
err = __recursive_insert(b->root, n, & (b->bst_funs));
if(err){
fprintf(stderr, "Error during insert into BST!!! (%s: %d)\n",
__FILE__, __LINE__);
exit(23);
}
}
n->active = 1;
n->index = -1;
/* set the key as needed */
//b->pq_funs.copy_key(elem_key, n->key);
n->key = elem_key;
/* then, insert the pointer to the element in the PQ */
__bst_pq_queue_insert(b, n);
}
/* delete (or deactivate) the element associated to a given info -- TESTED --*/
int bst_pq_delete(bst_pq_t b, unsigned int info){
node_t *n;
n = __bst_pq_tree_lookup(b, info);
if (n != NULL){
__tree_delete(b, n);
n->active = 0; /* deactivate the node */
/* After the node has been deleted from the tree, we add it to
the node cache */
gen_stack_push(b->node_cache, n);
__bst_pq_queue_delete_index(b, n->index); /* remove the reference form the PQ */
return 0; /* No problem */
}
return 1; /* the element does not exist */
}
/* change the key of an element in the BST_PQ -- TESTED --*/
int bst_pq_change_key(bst_pq_t b, unsigned int info, double key){
node_t *n;
int idx;
n = __bst_pq_tree_lookup(b, info);
if (n != NULL){
/* the element exists. We should just force its key and sift as required */
idx = n->index;
__bst_pq_queue_force_key(b, idx, key);
return 0; /* No problem */
}
else{
return 1; /* The element does not exist */
}
}
/* return the head of the PQ -- TESTED --*/
void* bst_pq_peek(bst_pq_t q){
return __bst_pq_queue_peek(q);
}
/* Read the key of a given node of the BST */
double bst_pq_get_key(bst_pq_t b, unsigned int info){
node_t *n;
n = __bst_pq_tree_lookup(b, info);
if (n != NULL){
return n->key;
}
else{
return -100000;
}
}
/* Read the key of the element of index "index" in the PQ */
double bst_pq_get_key_from_index(bst_pq_t b, int index){
node_t *n;
n = (node_t *)b->v[index];
return n->key;
}
/* Dump the BST -- TESTED -- */
void bst_pq_dump_bst(bst_pq_t b){
__bst_pq_tree_view_pre(b);
}
/* Show the PQ -- TESTED -- */
void bst_pq_dump_pq(bst_pq_t b){
__bst_pq_queue_dump(b);
}
/* Perform a lookup of an element in the BST_PQ -- TESTED-- */
node_t* bst_pq_lookup(bst_pq_t b, unsigned int info){
return __bst_pq_tree_lookup(b, info);
}
node_t* bst_pq_lookup_active(bst_pq_t b, unsigned int info){
node_t *ptr;
ptr = __bst_pq_tree_lookup(b, info);
if(ptr)
assert(ptr->active != 0);
if (ptr != NULL && ptr->active){
return ptr;
}
return NULL;
}
/* void bst_pq_insert_existing(bst_pq_t b, node_t *n){ */
/* /\* insert the element in the tree first *\/ */
/* if (n == NULL){ */
/* fprintf(stderr, "Error!!!! Attempt to insert a NULL existing element (%s: %d)\n", */
/* __FILE__, __LINE__); */
/* exit(37); */
/* } */
/* /\* n->info = b->bst_funs.alloc(); *\/ */
/* /\* n->key = b->pq_funs.alloc_key(); *\/ */
/* /\* b->bst_funs.copy(elem_info, n->info); *\/ */
/* n->left = n->right = NULL; */
/* if (b->root == NULL){ */
/* b->root = n; */
/* n->parent = NULL; */
/* } */
/* else{ */
/* __recursive_insert(b->root, n, & (b->bst_funs)); */
/* } */
/* n->active = 1; */
/* /\* set the key as needed *\/ */
/* //b->pq_funs.copy_key(elem_key, n->key); */
/* /\* then, insert the pointer to the element in the PQ *\/ */
/* __bst_pq_queue_insert(b, n); */
/* } */
void bst_pq_destroy(bst_pq_t b, char destroy_cache){
int i;
node_t *n;
if (b == NULL)
return;
if(destroy_cache && b->node_cache != NULL ){
while(!gen_stack_pop(b->node_cache, (void**) &n)){
//b->bst_funs.dealloc(n->info);
//free(n->key);
free(n);
}
free(b->node_cache->v);
free(b->node_cache);
}
for(i=b->last; i>=0; i--){
__tree_delete(b, b->v[i]);
__bst_pq_queue_delete_index(b, i); /* remove the reference form the PQ */
//bst_pq_delete(b, b->v[i]->info);
//b->bst_funs.dealloc(b->v[i]->info);
//free(b->v[i]->key);
free(b->v[i]);
}
free(b->v);
free(b);
}
|