-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_tree.c
716 lines (628 loc) · 17.3 KB
/
gen_tree.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
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
/*gen_tree.c
* author: Mi Yan, June 2000
*/
#include <string.h>
#include "gen_tree.h"
#include "circ_order.h"
#ifdef GMP
#include "gmp.h"
#endif
FILE *outfile;
int *bestOrdering;
int *ordering;
int nextID;
int **switchDist;
extern int DOBRANCH;
/*if bin_tree is available, return 0*/
#ifdef GMP
void
inc_bin_tree_mpz ( mpz_t rop,
int n, bin_env_t * bin_env, mpz_t inc, int *dif_lev )
{
int i;
bin_env_t *tmp_env;
mpz_t tmp, tmp2;
if ( n < 3 )
{
mpz_set_si ( rop, -1 );
return;
}
mpz_init_set ( tmp, inc );
for ( i = n; i >= 3; i-- )
{
tmp_env = bin_env + ( i - 3 );
mpz_add_ui ( tmp, tmp, tmp_env->tree_id );
if ( mpz_cmp_si ( tmp, tmp_env->tree_max ) < 0 )
{
tmp_env->tree_id = mpz_get_si ( tmp );
*dif_lev = i;
mpz_clear ( tmp );
mpz_set_si ( rop, 0 );
return;
}
else
{
mpz_init ( tmp2 );
tmp_env->tree_id = mpz_mod_ui ( tmp2, tmp, tmp_env->tree_max );
mpz_clear ( tmp2 );
mpz_tdiv_q_ui ( tmp, tmp, tmp_env->tree_max );
}
}
mpz_set ( rop, tmp );
mpz_clear ( tmp );
return;
}
int
inc_bin_tree ( int n, bin_env_t * bin_env, mpz_t inc, int *dif_lev )
{
int i;
bin_env_t *tmp_env;
mpz_t tmp, tmp2;
if ( n < 3 )
{
return ( -1 );
}
mpz_init_set ( tmp, inc );
for ( i = n; i >= 3; i-- )
{
tmp_env = bin_env + ( i - 3 );
mpz_add_ui ( tmp, tmp, tmp_env->tree_id );
if ( mpz_cmp_si ( tmp, tmp_env->tree_max ) < 0 )
{
tmp_env->tree_id = mpz_get_si ( tmp );
*dif_lev = i;
mpz_clear ( tmp );
return ( 0 );
}
else
{
mpz_init ( tmp2 );
tmp_env->tree_id = mpz_mod_ui ( tmp2, tmp, tmp_env->tree_max );
mpz_clear ( tmp2 );
mpz_tdiv_q_ui ( tmp, tmp, tmp_env->tree_max );
}
}
i = mpz_sgn ( tmp );
mpz_clear ( tmp );
return ( i );
}
#else
int
inc_bin_tree ( int n, bin_env_t * bin_env, long long inc, int *dif_lev )
{
int i;
bin_env_t *tmp_env;
long long tmp;
if ( n < 3 )
{
return ( -1 );
}
tmp = inc;
for ( i = n; i >= 3; i-- )
{
tmp_env = bin_env + ( i - 3 );
tmp = tmp_env->tree_id + tmp;
if ( tmp < tmp_env->tree_max )
{
tmp_env->tree_id = tmp;
*dif_lev = i;
return ( 0 );
}
else
{
tmp_env->tree_id = tmp % tmp_env->tree_max;
tmp = tmp / tmp_env->tree_max;
}
}
return ( tmp );
}
#endif
bin_env_t *
alloc4_bin_tree_env ( int n )
{
bin_env_t *ptr_env, *bin_env;
int i;
if ( n < 3 )
{
bin_env = NULL;
return 0;
}
bin_env = ( bin_env_t * ) malloc ( ( n - 2 ) * sizeof ( bin_env_t ) );
assert_malloc ( bin_env, __FILE__, __LINE__ - 1 );
for ( i = 3; i <= n; i++ )
{
ptr_env = bin_env + i - 3;
ptr_env->tree_exp =
( int * ) malloc ( ( 2 * i - 1 ) * sizeof ( int ) );
assert_malloc ( ptr_env->tree_exp, __FILE__, __LINE__ - 1 );
}
return ( bin_env );
}
int
free_bin_tree_env ( int n, bin_env_t * bin_env )
{
int i;
bin_env_t *ptr_env;
if ( n < 3 )
return 0;
for ( i = 3; i <= n; i++ )
{
ptr_env = bin_env + i - 3;
free ( ptr_env->tree_exp );
}
free ( bin_env );
return 0;
}
int
init_gen_bin ( int n, bin_env_t * bin_env )
{
bin_env_t *ptr_env;
int i;
if ( n < 3 )
return 0;
for ( i = 3; i <= n; i++ )
{
ptr_env = bin_env + i - 3;
ptr_env->tree_id = 0;
ptr_env->tree_max = 2 * i - 5;
ptr_env->size_of_tree = 2 * i - 1;
}
return 1;
}
struct tNode *
first ( int n, struct tNode *tpool, int MYPROC, bin_env_t * env )
{
struct tNode *tree;
int exp[2 * MAX_GENOMES + 1];
int dif_lev;
#ifdef GMP
mpz_t k;
mpz_init_set_si ( k, MYPROC );
if ( inc_bin_tree ( n, env, k, &dif_lev ) != 0 )
{
mpz_clear ( k );
return ( NULL );
}
mpz_clear ( k );
#else
if ( inc_bin_tree ( n, env, MYPROC, &dif_lev ) != 0 )
return ( NULL );
#endif
gen_tree ( n, exp, 0, env );
tree = exp_to_tree ( exp, n, tpool );
return tree;
}
#ifdef GMP
struct tNode *
next ( int n, struct tNode *tpool, mpz_t PROCS, bin_env_t * env )
#else
struct tNode *
next ( int n, struct tNode *tpool, long long PROCS, bin_env_t * env )
#endif
{
struct tNode *tree;
int exp[2 * MAX_GENOMES + 1];
int dif_lev;
if ( inc_bin_tree ( n, env, PROCS, &dif_lev ) != 0 )
return ( NULL );
gen_tree ( n, exp, dif_lev, env );
tree = exp_to_tree ( exp, n, tpool );
return tree;
}
#if 0
/*from_tag and to_tag are betwwen 0..((2n-3)-1)!!, to_tag>=from_tag */
main ( int argc, char **argv )
{
char *outfilename = NULL, *s;
bin_env_t *env;
struct tNode *tpool;
int n = 0;
int i;
#ifdef DEBUG
int j, exp[2 * MAX_GENOMES + 1];
char str[MAX_STR_LEN];
#endif
struct tNode *tree;
/*get arguments */
while ( --argc > 0 && ( *++argv )[0] == '-' )
{
for ( s = argv[0] + 1; *s != '\0'; s++ )
{
switch ( *s )
{
case 'o':
if ( argc <= 1 )
perror
( "output filename expected after -o (e.g. -o filename)" );
argc--;
outfilename =
( char * ) malloc ( MAX_FILENAME_LEN *
sizeof ( char ) );
strcpy ( outfilename, *++argv );
break;
case 'n':
n = atoi ( *++argv );
if ( n < 2 )
perror ( "n must be larger or equal to 2\n" );
argc--;
break;
case 'h':
fprintf ( stdout, "Main Options:\n" );
fprintf ( stdout, " -o outfilename -n num_of_nodes \n" );
fprintf ( stdout, "\n\n" );
exit ( 1 );
break;
default:
perror ( "illegal option" );
}
}
}
if ( outfilename == NULL )
outfile = stdout;
else
outfile = fopen ( outfilename, "a+" );
tpool = ( struct tNode * ) malloc
( ( 2 * MAX_NUM_GENES + 1 ) * sizeof ( struct tNode ) );
if ( tpool == ( struct tNode * ) NULL )
fprintf ( stderr, "ERROR: tpool NULL\n" );
init_gen_bin ( n, &env );
tree = first ( n, tpool, 0, env );
while ( tree )
{
/*print each tree */
#ifdef DEBUG
i = tree_to_exp ( tree->lChild, str );
str[i] = '\0';
fprintf ( stdout, "bin_tree:%s\n", str );
fflush ( outfile );
#endif
tree = next ( n, tpool, 1, env );
}
return;
}
#endif
/*substitute the i-th edge of old_ptr with the base tree*/
int
gen_new_tree ( int *old_tree, int edge, int *new_tree,
int n, int size_of_tree )
{
int leaf_cut, inter_cut;
int numOfChild;
int *old_ptr, *new_ptr;
inter_cut = edge + 2;
leaf_cut = inter_cut + 1;
if ( old_tree[leaf_cut - 1] < 0 )
numOfChild = 2;
else
numOfChild = 0;
while ( numOfChild > 0 )
{
if ( old_tree[leaf_cut] < 0 )
numOfChild = numOfChild + 1;
else
numOfChild = numOfChild - 1;
leaf_cut++;
}
#ifdef DEBUG1
if ( n == 4 )
{
fprintf ( outfile, "edge:%d,inter_cut:%d,leaf_cut:%d\n",
edge, inter_cut, leaf_cut );
fflush ( outfile );
}
#endif
/*insert new internal */
memcpy ( ( char * ) new_tree, ( char * ) old_tree,
( inter_cut ) * sizeof ( int ) );
new_tree[inter_cut] = -n + 2; /*?? */
old_ptr = old_tree + inter_cut;
new_ptr = new_tree + inter_cut + 1;
/*insert new leaf */
memcpy ( ( char * ) new_ptr, ( char * ) old_ptr,
( leaf_cut - inter_cut ) * sizeof ( int ) );
old_ptr = old_ptr + leaf_cut - inter_cut;
new_ptr = new_ptr + leaf_cut - inter_cut;
*new_ptr = n;
new_ptr++;
memcpy ( ( char * ) new_ptr, ( char * ) old_ptr,
( size_of_tree - leaf_cut ) * sizeof ( int ) );
return 0;
}
/*generated trees are stored in new_trees in ascending order of tree_id,
old_trees acts as a buffer*/
int
gen_tree ( int n, int new_tree[2 * MAX_GENOMES + 1],
int dif_lev, bin_env_t * env )
{
int old_trees[2 * MAX_GENOMES + 1];
int i;
if ( n == dif_lev - 1 )
{
memcpy ( ( char * ) new_tree, ( char * ) ( env[n - 3].tree_exp ),
env[n - 3].size_of_tree * sizeof ( int ) );
return 0;
}
if ( n == 3 )
{
/*base case */
for ( i = 0; i < env[n - 3].size_of_tree; i++ )
new_tree[i] = 0;
new_tree[0] = 0; /*it is tree_id, ranged from 0..((2n-5)!!-1) */
new_tree[1] = 1;
new_tree[2] = -1;
new_tree[3] = 2;
new_tree[4] = 3;
}
else
{
gen_tree ( n - 1, old_trees, dif_lev, env );
#ifdef DEBUG
printf ( "after return,tree_id:%d,size:%d\n", env[n - 3].tree_id,
env[n - 4].size_of_tree );
fflush ( stdout );
#endif
gen_new_tree ( old_trees, env[n - 3].tree_id, new_tree, n,
env[n - 4].size_of_tree );
}
memcpy ( ( char * ) ( env[n - 3].tree_exp ), ( char * ) new_tree,
env[n - 3].size_of_tree * sizeof ( int ) );
return 0;
}
/*transform pre-order tree traversal expression to tree*/
struct tNode *
exp_to_tree ( int *exp, int n, struct tNode *tpool )
{
struct tNode *tree, *child, *ptr;
int i, lchild;
#ifdef DEBUG1
for ( i = 0; i < 2 * n - 1; i++ )
fprintf ( outfile, "%d,", exp[i] );
fprintf ( outfile, "\n" );
#endif
/* no need for malloc: use the next slot in the tpool array */
tree = &tpool[0];
tree->parent = tree->rChild = tree->lChild = NULL;
tree->leaf = FALSE;
tree->tag = exp[1]; /*its value always be 1 */
/* no need for malloc: use the next slot in the tpool array */
child = &tpool[1];
child->parent = tree;
tree->lChild = child;
child->tag = exp[2];
child->lChild = child->rChild = NULL;
ptr = child;
lchild = 1;
for ( i = 3; i < 2 * n - 1; i++ )
{
/* no need for malloc: use the next slot in the tpool array */
child = &tpool[i - 1];
child->parent = ptr;
ptr->leaf = FALSE;
child->tag = exp[i];
child->lChild = child->rChild = NULL;
if ( lchild == 1 )
ptr->lChild = child;
else
ptr->rChild = child;
if ( exp[i] < 0 )
{
ptr = child;
lchild = 1;
}
if ( exp[i] > 0 )
{
child->lChild = child->rChild = NULL;
child->leaf = TRUE;
if ( lchild == 0 )
{
while ( ptr->rChild != NULL )
{
ptr = ptr->parent;
}
}
else
lchild = 0;
}
}
return ( tree );
}
int
tree_to_exp ( struct tNode *tree, char *str )
{
int len, tag, pos;
if ( ( tag = tree->tag ) > 0 )
{
sprintf ( str, "%d", tree->tag );
len = 1;
while ( ( tag = tag / 10 ) > 0 )
len++;
return len;
}
else
{
*str = '(';
pos = 1;
len = tree_to_exp ( tree->lChild, str + pos );
pos = pos + len;
*( str + pos ) = ',';
pos = pos + 1;
len = tree_to_exp ( tree->rChild, str + pos );
pos = pos + len;
*( str + pos ) = ')';
return ( pos + 1 );
}
}
int
assert_malloc ( void *buf, char *file, int line )
{
if ( buf == NULL )
{
printf ( "malloc faliuer at line %d in file %s\n", line, file );
fflush ( stdout );
exit ( -1 );
}
return 0;
}
#ifdef GMP
struct tNode *
testonelevel ( int n, struct tNode *tpool, mpz_t PROCS, bin_env_t * env,
int *flag, int **distmatrix, int best_so_far, int minidist )
#else
struct tNode *
testonelevel ( int n, struct tNode *tpool, int PROCS, bin_env_t * env,
int *flag, int **distmatrix, int best_so_far, int minidist )
#endif
{
struct tNode *tree;
int exp[2 * MAX_GENOMES + 1];
int dif_lev;
int old_trees[2 * MAX_GENOMES + 1];
int tmp_trees[2 * MAX_GENOMES + 1];
int new_tree[2 * MAX_GENOMES + 1];
int i, prev, colb, firstg;
int treeid;
if ( inc_bin_tree ( n, env, 1, &dif_lev ) != 0 )
return ( NULL );
DOBRANCH = 1;
gen_tree ( n - 1, old_trees, dif_lev, env );
tree = exp_to_tree ( old_trees, n - 1, tpool );
tree->leaf = TRUE;
prev = tree->tag;
firstg = prev;
colb = 0;
bestOrdering = tmp_trees;
ordering = new_tree;
bestOrdering[0] = prev - 1;
nextID = 1;
for ( i = 1; i <= 2 * ( n - 2 ); i++ )
{
treeid = old_trees[i];
if ( treeid < 0 )
continue;
colb += distmatrix[treeid - 1][prev - 1];
prev = treeid;
bestOrdering[nextID] = treeid;
nextID++;
}
nextID = 0;
colb += distmatrix[firstg - 1][prev - 1];
colb = test_all_co_score ( best_so_far - minidist + 1, colb,
distmatrix, tree,
tree->lChild, firstg, &prev, 0 );
DOBRANCH = 0;
if ( ( colb + minidist ) > best_so_far )
{
*flag = 1;
return NULL;
}
else
{
minidist = 10000;
for ( i = 0; i < n - 2; i++ )
{
nextID = switchDist[bestOrdering[i]][bestOrdering[i + 1]];
if ( nextID < minidist )
minidist = nextID;
}
if ( ( colb + minidist ) > best_so_far )
{
*flag = 1;
return NULL;
}
}
gen_new_tree ( old_trees, env[n - 3].tree_id, exp, n,
env[n - 4].size_of_tree );
memcpy ( ( char * ) ( env[n - 3].tree_exp ), ( char * ) exp,
env[n - 3].size_of_tree * sizeof ( int ) );
tree = exp_to_tree ( exp, n, tpool );
return tree;
}
/* not include in the main distribution, recommend not use it*/
struct tNode *
testtwolevel ( int n, struct tNode *tpool, int NUM_GENOMES, bin_env_t * env,
int *flag, int **distmatrix, int best_so_far, int minidist )
{
struct tNode *tree;
int exp[2 * MAX_GENOMES + 1];
int dif_lev;
int old_trees[2 * MAX_GENOMES + 1];
if ( inc_bin_tree ( n, env, 1, &dif_lev ) != 0 )
return ( NULL );
if ( gen_tree_levels
( n - 1, old_trees, tpool, dif_lev, env, NUM_GENOMES, distmatrix,
best_so_far, minidist ) == -1 )
{
*flag = 1;
return NULL;
}
gen_new_tree ( old_trees, env[n - 3].tree_id, exp, n,
env[n - 4].size_of_tree );
memcpy ( ( char * ) ( env[n - 3].tree_exp ), ( char * ) exp,
env[n - 3].size_of_tree * sizeof ( int ) );
tree = exp_to_tree ( exp, n, tpool );
return tree;
}
/*generated trees are stored in new_trees in ascending order of tree_id,
old_trees acts as a buffer*/
int
gen_tree_levels ( int n, int new_tree[2 * MAX_GENOMES + 1],
struct tNode *tpool, int dif_lev, bin_env_t * env,
int NUM_GENOMES, int **distmatrix, int best_so_far,
int minidist )
{
int old_trees[2 * MAX_GENOMES + 1];
int i, prev, colb, firstg;
int treeid;
struct tNode *tree;
if ( n == dif_lev - 1 )
{
memcpy ( ( char * ) new_tree, ( char * ) ( env[n - 3].tree_exp ),
env[n - 3].size_of_tree * sizeof ( int ) );
return 0;
}
if ( n == 3 )
{
/*base case */
for ( i = 0; i < env[n - 3].size_of_tree; i++ )
new_tree[i] = 0;
new_tree[0] = 0; /*it is tree_id, ranged from 0..((2n-5)!!-1) */
new_tree[1] = 1;
new_tree[2] = -1;
new_tree[3] = 2;
new_tree[4] = 3;
}
else
{
gen_tree_levels ( n - 1, old_trees, tpool, dif_lev, env, NUM_GENOMES,
distmatrix, best_so_far, minidist );
if ( n == NUM_GENOMES - 1 )
{
tree = exp_to_tree ( old_trees, n - 1, tpool );
tree->leaf = TRUE;
prev = tree->tag;
firstg = prev;
colb = 0;
for ( i = 1; i <= 2 * ( n - 2 ); i++ )
{
treeid = old_trees[i];
if ( treeid < 0 )
continue;
colb += distmatrix[treeid - 1][prev - 1];
prev = treeid;
}
colb += distmatrix[firstg - 1][prev - 1];
colb = test_all_co_score ( best_so_far - minidist + 1, colb,
distmatrix, tree,
tree->lChild, firstg, &prev, 0 );
if ( ( colb + minidist ) > best_so_far )
{
return -1;
}
}
gen_new_tree ( old_trees, env[n - 3].tree_id, new_tree, n,
env[n - 4].size_of_tree );
}
memcpy ( ( char * ) ( env[n - 3].tree_exp ), ( char * ) new_tree,
env[n - 3].size_of_tree * sizeof ( int ) );
return 0;
}