-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinittree.c
1721 lines (1629 loc) · 70.4 KB
/
inittree.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
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
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <math.h>
#include "structs.h"
#include "convert.h"
#include "bbtsp.h"
#include "cheaptsp.h"
#include "binencode.h"
#include "greedy_median.h"
#include "condense3.h"
#include "lk_main.h"
#include "inittree.h"
#include "inversion_median_alberto.h"
#include "sorting_reversal_median.h"
extern int VIncrease;
void
SetTreeEdgePtrs ( struct tNode *tree, int *edgepool, int *index )
{
struct tNode *node;
node = tree;
if ( node == NULL )
return;
if ( node->lChild )
{
node->lChild->sc_parent = node->sc_lChild = &edgepool[*index];
*index += 1;
SetTreeEdgePtrs ( node->lChild, edgepool, index );
}
if ( node->rChild )
{
node->rChild->sc_parent = node->sc_rChild = &edgepool[*index];
*index += 1;
SetTreeEdgePtrs ( node->rChild, edgepool, index );
}
return;
}
void
initialize_tree_random ( struct tNode *tree, struct genome_struct *labels,
int num_genes, int num_genomes )
{
/* random permutations of 1..n assigned as labels, with sign chosen
randomly as well; faster than NN, but produces poorer solutions;
slower than trivial below, because of the many calls to random */
struct genome_struct *gen;
int id;
if ( tree == NULL )
return;
if ( tree->leaf == FALSE )
{
/* index into labels array using tree->tag;
it's negative, so compute proper index */
id = tree->tag;
gen = tree->genome = &labels[num_genomes - id];
gen->genome_num = id;
fillGenomeRand ( gen->genes, num_genes );
}
initialize_tree_random ( tree->lChild, labels, num_genes, num_genomes );
initialize_tree_random ( tree->rChild, labels, num_genes, num_genomes );
return;
}
void
initialize_tree_trivial ( struct tNode *tree, struct genome_struct *labels,
int num_genes, int num_genomes )
{
/* every internal node gets the outgroup label; superfast, but
tends to produces poorer solutions */
struct genome_struct *gen;
int id;
int i;
if ( tree == NULL )
return;
if ( tree->leaf == FALSE )
{
/* index into labels array using tree->tag;
it's negative, so compute proper index */
id = tree->tag;
gen = tree->genome = &labels[num_genomes - id];
gen->genome_num = id;
for ( i = 0; i < num_genes; i++ )
gen->genes[i] = i + 1;
}
initialize_tree_trivial ( tree->lChild, labels, num_genes, num_genomes );
initialize_tree_trivial ( tree->rChild, labels, num_genes, num_genomes );
return;
}
/* next two routines handle a queue; the queue array is initialized
in main to be linked around the array; head points to the head
of the queue, tail points to the end of the queue;
note that this is *not* a correct queue implementation: it only
works under the assumptions that one will never dequeue from an
empty queue and that the queue cannot overflow
*/
INLINE struct tNode *
dequeue ( struct qNode *queue, struct qNode **head, struct qNode **tail )
{
struct tNode *node;
#ifdef DEBUG
if ( ( *head ) == ( *tail ) )
{
fprintf ( outfile, "Error: queue is empty!\n" );
fflush ( outfile );
}
#endif
node = ( *head )->item;
*head = ( *head )->next;
return node;
}
INLINE void
add_queue ( struct qNode *queue,
struct tNode *node, struct qNode **head, struct qNode **tail )
{
( *tail )->item = node;
*tail = ( *tail )->next;
return;
}
struct tNode *
find_tree_middle ( struct tNode *tree, struct qNode *queue,
struct tNode *tpool, int num_genomes )
{
/* called from a leaf, will return a node on the middle of the
path from that leaf to the farthest leaf from it */
int i;
struct tNode *farthest, *node, *next;
struct qNode *head, *tail;
/* use bfs to find the farthest leaf */
#ifdef DEBUG
if ( tree->tag < 0 )
{
fprintf ( outfile,
"Error in find_tree_middle: start vertex is internal\n" );
fflush ( outfile );
}
#endif
head = tail = queue; /* initialize queue pointers */
for ( i = 0; i <= 2 * num_genomes; i++ )
{ /* set all flags to false */
tpool[i].leaf = FALSE;
}
node = tree;
node->leaf = TRUE;
node->pred = NULL;
add_queue ( queue, node, &head, &tail );
if ( node->lChild )
{
node->lChild->leaf = TRUE;
node->lChild->pred = node;
add_queue ( queue, node->lChild, &head, &tail );
}
if ( node->rChild )
{
node->rChild->leaf = TRUE;
node->rChild->pred = node;
add_queue ( queue, node->rChild, &head, &tail );
}
while ( head != tail )
{
/* loop until queue is empty -- last found is farthest */
node = dequeue ( queue, &head, &tail ); /* get next node */
/* here we know that we started at the root and so only have
to go down child pointers */
if ( node->tag < 0 )
{ /* only internal nodes have children */
next = node->lChild;
if ( next->leaf == FALSE )
{
next->leaf = TRUE;
next->pred = node;
add_queue ( queue, next, &head, &tail );
}
next = node->rChild;
if ( next->leaf == FALSE )
{
next->leaf = TRUE;
next->pred = node;
add_queue ( queue, next, &head, &tail );
}
}
} /* end while */
/* last node seen is a leaf and is farthest from the starting pt */
farthest = node;
/* retrace path and count number of nodes */
i = 0;
next = farthest;
while ( next != NULL )
{
i++;
next = next->pred;
}
i = i / 2;
next = farthest;
while ( i > 0 )
{
i--;
next = next->pred;
}
/* restore leaf flags before returning */
#if 0
for ( i = 0; i <= 2 * num_genomes; i++ )
#else
for ( i = 0; i < 2 * num_genomes - 2; i++ )
#endif
{
if ( tpool[i].tag < 0 )
{
tpool[i].leaf = FALSE;
}
else
{
tpool[i].leaf = TRUE;
}
}
return ( next );
}
void
combine ( int *gene, struct genome_struct *g1, struct genome_struct *g2,
int num_genes )
{
int i;
#ifdef DEBUG
fprintf ( outfile, "combine(): num_genes: %3d\n", num_genes );
fprintf ( outfile, "combine(): trying to combine %p and %p\n", g1, g2 );
fflush ( outfile );
#endif
/* fast hack: simply copies g1 */
/* randomization does not seem to help much here and causes 30% slowdown */
for ( i = 0; i < num_genes; i++ )
{
gene[i] = g1->genes[i];
}
return;
}
void
propagate ( struct tNode *current, struct genome_struct *labels,
int num_genes, int num_genomes, int *succ1, int *succ2f,
int *succ2b, int *degree, int *pred1, int *pred2, int *tour,
int *otherEnd, int comb_method )
{
/* uses postorder traversal from an internal node, so needs
flags (leaf tag) to avoid going back */
struct tNode *parent, *lChild, *rChild;
struct genome_struct *gen;
int id, index;
#ifdef DEBUG
fprintf ( outfile, "Entering propagate, combine=%2d current: %p\n",
comb_method, current );
fprintf ( outfile, " num_genes: %3d num_genomes: %3d\n", num_genes,
num_genomes );
fprintf ( outfile, " pred1: %p pred2: %p tour: %p otherEnd: %p\n", pred1,
pred2, tour, otherEnd );
fprintf ( outfile, " succ1: %p succ2f: %p succ2b: %p degree: %p\n", succ1,
succ2f, succ2b, degree );
fflush ( outfile );
if ( current->tag >= 0 )
{
fprintf ( outfile, "Error in propagate: node is a leaf\n" );
fflush ( outfile );
}
#endif
current->leaf = TRUE;
/* need to look at up to two subtrees, but which? */
parent = current->parent;
if ( parent )
{
if ( !( parent->leaf ) )
{
propagate ( parent, labels, num_genes, num_genomes,
succ1, succ2f, succ2b, degree, pred1, pred2, tour,
otherEnd, comb_method );
}
}
lChild = current->lChild;
if ( lChild )
{
if ( !( lChild->leaf ) )
{
propagate ( lChild, labels, num_genes, num_genomes,
succ1, succ2f, succ2b, degree, pred1, pred2, tour,
otherEnd, comb_method );
}
}
rChild = current->rChild;
if ( rChild )
{
if ( !( rChild->leaf ) )
{
propagate ( rChild, labels, num_genes, num_genomes,
succ1, succ2f, succ2b, degree, pred1, pred2, tour,
otherEnd, comb_method );
}
}
/* now we have taken care of subtrees, so we have two labeled
neighbors and an unlabeled one */
/* set up a node for this genome */
id = current->tag;
index = num_genomes - id;
gen = current->genome = &labels[index];
gen->genome_num = id;
/* combine genome1 and genome2 */
if ( parent->genome == NULL )
{
if ( comb_method == 0 )
{ /* FASTPROP */
combine ( gen->genes, lChild->genome, rChild->genome, num_genes );
}
else
{ /* MEDIANPROP */
greedy_median ( gen->genes, lChild->genome, rChild->genome,
num_genes, degree, succ1, succ2f, succ2b, pred1,
pred2, tour, otherEnd );
}
}
else
{
if ( lChild->genome == NULL )
{
if ( comb_method == 0 )
{ /* FASTPROP */
combine ( gen->genes, parent->genome, rChild->genome,
num_genes );
}
else
{ /* MEDIANPROP */
greedy_median ( gen->genes, parent->genome, rChild->genome,
num_genes, degree, succ1, succ2f, succ2b,
pred1, pred2, tour, otherEnd );
}
}
else
{ /* by default, rChild->genome must be null */
if ( comb_method == 0 )
{ /* FASTPROP */
combine ( gen->genes, parent->genome, lChild->genome,
num_genes );
}
else
{ /* MEDIANPROP */
greedy_median ( gen->genes, parent->genome, lChild->genome,
num_genes, degree, succ1, succ2f, succ2b,
pred1, pred2, tour, otherEnd );
}
}
}
return;
}
void
initialize_tree_propagate ( int COND, struct tNode *tree, struct tNode *tpool,
struct genome_struct *labels, struct qNode *queue,
struct adj_struct *adj_list,
struct adj_struct *adj_pool,
int **weights,
int *stack, int *degree, int *otherEnd,
intpair_t * neighbors, edge_t * edges,
int *incycle, int *outcycle,
int num_genes, int num_genomes,
int *pred1, int *pred2, int *picked, int *decode,
int comb_method, int inittspsolver,
int thresh, int CIRCULAR )
{
/* long list of args needed for calls to propagate (really, to
greedy_median) and to bbtsp */
int i, id;
int realsolver, num_cond, ncount;
struct tNode *root, *parent, *lChild, *rChild;
struct tNode *condnode1, *condnode2, *condnode3, *condmedian;
struct genome_struct *gen[3];
/* Uses a postorder traversal from a central internal node (middle
of longest path in the tree) to propagate leaf labels to internal
nodes in a simple way */
if ( tree == NULL )
return;
/* get starting node for postorder */
root = find_tree_middle ( tree, queue, tpool, num_genomes );
#ifdef DEBUG
fprintf ( outfile, "ITP: root %p with tag %3d\n", root, root->tag );
#endif
/* find_tree_middle routine used and restored leaf flags */
/* we'll reuse some of the space passed through for convert
and bbtsp; thus propagate will use stack for succ1,
picked for succ2f, incycle for succ2b, and outcycle for tour */
root->leaf = TRUE; /* avoid passing through this node! */
/* start up to three separate postorder traversals, one in each
subtree of this node; but only for non-leaves */
parent = root->parent;
if ( parent->tag < 0 )
{
propagate ( parent, labels, num_genes, num_genomes,
stack, picked, incycle, degree,
pred1, pred2, outcycle, otherEnd, comb_method );
}
lChild = root->lChild;
if ( lChild->tag < 0 )
{
propagate ( lChild, labels, num_genes, num_genomes,
stack, picked, incycle, degree, pred1, pred2, outcycle,
otherEnd, comb_method );
}
rChild = root->rChild;
if ( rChild->tag < 0 )
{
propagate ( rChild, labels, num_genes, num_genomes,
stack, picked, incycle, degree, pred1, pred2, outcycle,
otherEnd, comb_method );
}
/* restore leaf flags */
#if 0
for ( i = 0; i <= 2 * num_genomes; i++ )
#else
for ( i = 0; i < 2 * num_genomes - 2; i++ )
#endif
{
if ( tpool[i].tag < 0 )
{
tpool[i].leaf = FALSE;
}
else
{
tpool[i].leaf = TRUE;
}
}
/* now just set up label of root from those of parent, lChild, rChild */
/* first set up a node for this genome */
id = root->tag;
root->genome = &labels[num_genomes - id];
root->genome->genome_num = id;
/* now take the median of these three leaves */
/* condense? */
if ( COND )
{ /* condensation */
ncount = 2 * num_genomes;
condnode1 = &tpool[ncount + 2];
condnode1->genome = &labels[ncount + 2];
condnode2 = &tpool[ncount + 3];
condnode2->genome = &labels[ncount + 3];
condnode3 = &tpool[ncount + 4];
condnode3->genome = &labels[ncount + 4];
condmedian = &tpool[ncount + 5];
condmedian->genome = &labels[ncount + 5];
condense3 ( parent->genome->genes,
lChild->genome->genes,
rChild->genome->genes,
condnode1->genome->genes,
condnode2->genome->genes,
condnode3->genome->genes, num_genes, &num_cond,
#if 0
pred1, pred2, picked,
#else
pred1 + num_genes, pred2 + num_genes, picked + num_genes,
#endif
decode );
/* use pred1=succ,pred2=pred,picked=code */
if ( ( CIRCULAR && ( num_cond == 0 ) )
|| ( ( !CIRCULAR ) && ( num_cond <= 1 ) ) )
{
/* three identical neighbors... */
for ( i = 0; i < num_genes; i++ )
{ /* so just copy on in place */
root->genome->genes[i] = parent->genome->genes[i];
}
goto done; /* and exit */
}
/* call solvers with condensed genomes */
/* the LK solvers need a weight matrix, but the bbtsp solver does not */
/* force use of bbtsp if problem is too small */
realsolver = inittspsolver;
if ( ( num_cond <= thresh ) && ( inittspsolver != TSP_COALESCED ) )
realsolver = TSP_BBTSP;
switch ( realsolver )
{
case INVERSION_MEDIAN:
gen[0] = condnode1->genome;
gen[1] = condnode2->genome;
gen[2] = condnode3->genome;
find_reversal_median ( condmedian->genome, gen, num_cond,
NULL );
break;
case INVERSION_MEDIAN_FAST:
gen[0] = condnode1->genome;
gen[1] = condnode1->genome;
gen[2] = condnode1->genome;
if ( CIRCULAR )
albert_inversion_median_circular ( gen,
num_cond,
condmedian->genome->
genes );
else
albert_inversion_median_noncircular ( gen,
num_cond,
condmedian->genome->
genes );
break;
case TSP_BBTSP:
/* does not create a weight matrix, but sets weight in the adj lists */
convert2_to_tsp ( condnode1->genome, condnode2->genome,
condnode3->genome, adj_list, adj_pool,
num_cond, CIRCULAR );
bbtsp ( 2 * num_cond, condmedian->genome->genes, FALSE, /* cannot use median that does not exist */
condnode1->genome->genes, condnode2->genome->genes,
condnode3->genome->genes,
adj_list, neighbors, stack, outcycle, degree,
otherEnd, edges, CIRCULAR );
break;
#ifdef CONCORDE
case TSP_CHLINKERN:
/* creates a weight matrix -- more expensive! */
convert_to_tsp ( condnode1->genome, condnode2->genome,
condnode3->genome, num_cond, CIRCULAR,
weights );
chlinkern ( 2 * num_cond, weights, condmedian->genome->genes,
incycle, outcycle );
break;
case TSP_GREEDYLK:
/* creates a weight matrix -- more expensive! */
convert_to_tsp ( condnode1->genome, condnode2->genome,
condnode3->genome, num_cond, CIRCULAR,
weights );
greedylk ( 2 * num_cond, weights, condmedian->genome->genes,
incycle, outcycle );
break;
#endif
case TSP_COALESCED:
/* does not create a weight matrix, but sets weight in the adj lists */
convert2_to_tsp ( condnode1->genome, condnode2->genome,
condnode3->genome, adj_list, adj_pool,
num_cond, CIRCULAR );
coalestsp ( 2 * num_cond, condmedian->genome->genes, FALSE, /* cannot use median that does not exist */
condnode1->genome->genes,
condnode2->genome->genes,
condnode3->genome->genes, adj_list, neighbors,
stack, outcycle, degree, otherEnd, edges,
CIRCULAR );
break;
}
/* decode the median and assign it to the node */
decode3 ( root->genome->genes, condmedian->genome->genes,
#if 0
pred1 /* really, succ */ ,
#else
pred1 + num_genes /* really, succ */ ,
#endif
decode, num_cond );
} /* end condensation */
else
{ /* no condensation */
/* which solver to call? */
/* force use of bbtsp if problem is too small */
realsolver = inittspsolver;
if ( ( num_genes <= thresh ) && ( inittspsolver != TSP_COALESCED ) )
realsolver = TSP_BBTSP;
switch ( realsolver )
{
case INVERSION_MEDIAN:
gen[0] = parent->genome;
gen[1] = lChild->genome;
gen[2] = rChild->genome;
find_reversal_median ( root->genome, gen, num_genes, NULL );
break;
case INVERSION_MEDIAN_FAST:
gen[0] = parent->genome;
gen[1] = lChild->genome;
gen[2] = rChild->genome;
if ( CIRCULAR )
albert_inversion_median_circular ( gen, num_genes,
root->genome->genes );
else
albert_inversion_median_noncircular ( gen, num_genes,
root->genome->
genes );
break;
case TSP_BBTSP:
/* does not create a weight matrix, but sets weight in the adj lists */
convert2_to_tsp ( parent->genome, lChild->genome,
rChild->genome, adj_list, adj_pool,
num_genes, CIRCULAR );
bbtsp ( 2 * num_genes, root->genome->genes, FALSE,
parent->genome->genes, lChild->genome->genes,
rChild->genome->genes, adj_list, neighbors, stack,
outcycle, degree, otherEnd, edges, CIRCULAR );
break;
#ifdef CONCORDE
case TSP_CHLINKERN:
/* creates a weight matrix -- more expensive! */
convert_to_tsp ( parent->genome, lChild->genome,
rChild->genome, num_genes, CIRCULAR,
weights );
chlinkern ( 2 * num_genes, weights, root->genome->genes,
incycle, outcycle );
break;
case TSP_GREEDYLK:
/* creates a weight matrix -- more expensive! */
convert_to_tsp ( parent->genome, lChild->genome,
rChild->genome, num_genes, CIRCULAR,
weights );
greedylk ( 2 * num_genes, weights, root->genome->genes,
incycle, outcycle );
break;
#endif
case TSP_COALESCED:
/* does not create a weight matrix, but sets weight in the adj lists */
convert2_to_tsp ( parent->genome, lChild->genome,
rChild->genome, adj_list, adj_pool,
num_genes, CIRCULAR );
coalestsp ( 2 * num_genes, root->genome->genes, FALSE,
parent->genome->genes, lChild->genome->genes,
rChild->genome->genes, adj_list, neighbors, stack,
outcycle, degree, otherEnd, edges, CIRCULAR );
break;
}
} /* end -- no condensation */
done:;
/* tspsolvers automatically assigned median to current node */
return;
}
/* used by initialize_tree_SNN */
struct tNode *
find_closest_leaf ( struct tNode *node, struct qNode *queue )
{
/* by "closest" we mean "the fewest edges away", since we
have no values for edge lengths; thus we need a simple bfs */
/* use node->leaf as a visited flag -- needed, since we have
an unrooted tree */
struct qNode *head, *tail; /* queue pointers */
struct tNode *next;
head = tail = queue; /* initialize queue pointers */
node->leaf = TRUE;
while ( 1 )
{ /* loop until a leaf is found -- one must exist */
if ( node->tag >= 0 )
{
return node;
}
else
{ /* can move in 2 of 3 directions, but which? */
next = node->parent;
if ( next->leaf == FALSE )
{
next->leaf = TRUE;
add_queue ( queue, next, &head, &tail );
}
next = node->lChild;
if ( next->leaf == FALSE )
{
next->leaf = TRUE;
add_queue ( queue, next, &head, &tail );
}
next = node->rChild;
if ( next->leaf == FALSE )
{
next->leaf = TRUE;
add_queue ( queue, next, &head, &tail );
}
node = dequeue ( queue, &head, &tail );
/* cannot fail -- there is always a leaf */
}
}
}
/* used by initialize_tree_BNN */
void
real_init_tree_NN ( int COND, struct tNode *tree, struct tNode *tpool,
struct genome_struct *labels, triple_t * triple,
struct adj_struct *adj_list, struct adj_struct *adj_pool,
int **weights, int *stack, int *degree, int *otherEnd,
intpair_t * neighbors, edge_t * edges,
int *incycle, int *outcycle,
int *pred, int *succ, int *code, int *decode,
struct tNode *condnode1, struct tNode *condnode2,
struct tNode *condnode3, struct tNode *condmedian,
int num_genes, int num_genomes, int inittspsolver,
int thresh, int CIRCULAR )
{
struct genome_struct *node1, *node2, *node3, *nodem;
struct genome_struct *gen[3];
int i, id;
int realsolver, num_cond;
if ( tree->tag >= 0 )
return; /* nothing to do at a leaf */
/* no storage assigned yet; get a node from the label array */
id = tree->tag;
i = num_genomes - id;
nodem = tree->genome = &labels[i];
nodem->genome_num = id;
node1 = &labels[triple[i].A];
node2 = &labels[triple[i].B];
node3 = &labels[triple[i].C];
/* now take the median of these three leaves */
#ifdef DEBUG
fprintf ( outfile,
"in real_init_NN, about to convert, id=%3d, A=%3d, B=%3d, C=%3d\n",
id, triple[i].A, triple[i].B, triple[i].C );
#endif
if ( COND )
{ /* condensation */
condense3 ( node1->genes, node2->genes, node3->genes,
condnode1->genome->genes,
condnode2->genome->genes,
condnode3->genome->genes,
num_genes, &num_cond, succ, pred, code, decode );
if ( num_cond == 0 )
{ /* three identical neighbors... */
/* note: only works for circular genome; for linear, value is 1 */
for ( i = 0; i < num_genes; i++ )
{ /* so just copy one in place */
nodem->genes[i] = node1->genes[i];
}
}
else
{ /* get the median the hard way ;-) */
/* needs a small fix here: bbtsp will try to use existing
genome at tree->genome as a tour, which will crash, since
there is not one yet... so pass FALSE to use_median */
realsolver = inittspsolver;
if ( ( num_cond <= thresh )
&& ( inittspsolver != TSP_COALESCED ) )
realsolver = TSP_BBTSP;
switch ( realsolver )
{
case INVERSION_MEDIAN:
gen[0] = condnode1->genome;
gen[1] = condnode2->genome;
gen[2] = condnode3->genome;
find_reversal_median ( condmedian->genome, gen, num_cond,
NULL );
break;
case INVERSION_MEDIAN_FAST:
gen[0] = condnode1->genome;
gen[1] = condnode1->genome;
gen[2] = condnode1->genome;
if ( CIRCULAR )
albert_inversion_median_circular ( gen, num_cond,
condmedian->
genome->genes );
else
albert_inversion_median_noncircular ( gen, num_cond,
condmedian->
genome->genes );
break;
case TSP_BBTSP:
convert2_to_tsp ( condnode1->genome, condnode2->genome,
condnode3->genome, adj_list, adj_pool,
num_cond, CIRCULAR );
bbtsp ( 2 * num_cond, condmedian->genome->genes, FALSE,
condnode1->genome->genes,
condnode2->genome->genes,
condnode3->genome->genes, adj_list, neighbors,
stack, outcycle, degree, otherEnd, edges,
CIRCULAR );
break;
case TSP_COALESCED:
convert2_to_tsp ( condnode1->genome, condnode2->genome,
condnode3->genome, adj_list, adj_pool,
num_cond, CIRCULAR );
coalestsp ( 2 * num_cond, condmedian->genome->genes,
FALSE, condnode1->genome->genes,
condnode2->genome->genes,
condnode3->genome->genes, adj_list, neighbors,
stack, outcycle, degree, otherEnd, edges,
CIRCULAR );
break;
#ifdef CONCORDE
case TSP_GREEDYLK:
/* creates a weight matrix -- more expensive! */
convert_to_tsp ( condnode1->genome, condnode2->genome,
condnode3->genome, num_cond, CIRCULAR,
weights );
greedylk ( 2 * num_cond, weights,
condmedian->genome->genes, incycle, outcycle );
break;
case TSP_CHLINKERN:
/* creates a weight matrix -- more expensive! */
convert_to_tsp ( condnode1->genome, condnode2->genome,
condnode3->genome, num_cond, CIRCULAR,
weights );
chlinkern ( 2 * num_cond, weights,
condmedian->genome->genes, incycle,
outcycle );
break;
#endif
}
/* bbtsp automatically assigned median to current node,
* but it has to be decoded */
decode3 ( nodem->genes, condmedian->genome->genes, succ, decode,
num_cond );
}
} /* end condensation */
else
{ /* no condensation */
realsolver = inittspsolver;
if ( ( num_genes <= thresh ) && ( inittspsolver != TSP_COALESCED ) )
realsolver = TSP_BBTSP;
switch ( realsolver )
{
case TSP_BBTSP:
convert2_to_tsp ( node1, node2, node3,
adj_list, adj_pool, num_genes, CIRCULAR );
bbtsp ( 2 * num_genes, nodem->genes, FALSE,
node1->genes, node2->genes, node3->genes,
adj_list, neighbors, stack, outcycle, degree,
otherEnd, edges, CIRCULAR );
break;
case INVERSION_MEDIAN:
gen[0] = node1;
gen[1] = node2;
gen[2] = node3;
find_reversal_median ( nodem, gen, num_genes, NULL );
break;
case INVERSION_MEDIAN_FAST:
gen[0] = node1;
gen[1] = node2;
gen[2] = node3;
if ( CIRCULAR )
albert_inversion_median_circular ( gen, num_genes,
nodem->genes );
else
albert_inversion_median_noncircular ( gen, num_genes,
nodem->genes );
break;
case TSP_COALESCED:
convert2_to_tsp ( node1, node2, node3, adj_list,
adj_pool, num_genes, CIRCULAR );
coalestsp ( 2 * num_genes, nodem->genes, FALSE,
node1->genes, node2->genes, node3->genes,
adj_list, neighbors, stack, outcycle, degree,
otherEnd, edges, CIRCULAR );
break;
#ifdef CONCORDE
case TSP_CHLINKERN:
/* creates a weight matrix -- more expensive! */
convert_to_tsp ( node1, node2, node3,
num_genes, CIRCULAR, weights );
chlinkern ( 2 * num_genes, weights, nodem->genes,
incycle, outcycle );
break;
case TSP_GREEDYLK:
/* creates a weight matrix -- more expensive! */
convert_to_tsp ( node1, node2, node3,
num_genes, CIRCULAR, weights );
greedylk ( 2 * num_genes, weights, nodem->genes,
incycle, outcycle );
break;
#endif
}
/* bbtsp automatically assigned median to current node */
} /* end no condensation */
/* go process remaining nodes, if any */
if ( tree->lChild )
real_init_tree_NN ( COND, tree->lChild, tpool, labels, triple,
adj_list, adj_pool, weights, stack, degree,
otherEnd, neighbors, edges, incycle, outcycle,
pred, succ, code, decode, condnode1, condnode2,
condnode3, condmedian, num_genes, num_genomes,
inittspsolver, thresh, CIRCULAR );
if ( tree->rChild )
real_init_tree_NN ( COND, tree->rChild, tpool, labels, triple,
adj_list, adj_pool, weights, stack, degree,
otherEnd, neighbors, edges, incycle, outcycle,
pred, succ, code, decode, condnode1, condnode2,
condnode3, condmedian, num_genes, num_genomes,
inittspsolver, thresh, CIRCULAR );
return;
}
int
move_up ( struct tNode *current, triple_t * triple, int num_genomes )
{
/* recursive routine to propagate up from the leaves the
identity of and distance to the closest leaf */
/* function returns the ID of the leaf, stores the two
closest leaves (lChild and rChild) in triple.A and .B,
and stores distance to closer one, plus 1, in sc field of parent */
int id, ind;
id = current->tag;
if ( id >= 0 )
{ /* a leaf */
*( current->sc_parent ) = 0;
return ( id );
}
/* continue postorder ( id is negative ) */
ind = num_genomes - id;
triple[ind].A = move_up ( current->lChild, triple, num_genomes );
triple[ind].B = move_up ( current->rChild, triple, num_genomes );
/* which side has the closer leaf? */
if ( *( current->sc_lChild ) <= *( current->sc_rChild ) )
{
*( current->sc_parent ) = *( current->sc_lChild ) + 1;
id = triple[ind].A;
}
else
{
*( current->sc_parent ) = *( current->sc_rChild ) + 1;
id = triple[ind].B;
}
return ( id );
}
void
move_down ( struct tNode *parent, struct tNode *current, int which,
triple_t * triple, int num_genomes )
{
/* recursive routine to explore the tree and set up the triple[]
array for internal nodes, listing the three closest leaves */
/* routine need only set triple.C, the third closest leaf, that
in the direction of the parent */
int id, currind;
id = current->tag;
if ( id >= 0 )
{ /* leaf, done */
return;
}
/* continue preorder ( id is negative ) */
currind = num_genomes - id;
id = parent->tag;
if ( id >= 0 )
{ /* parent is a leaf */
triple[currind].C = id;
}
else
{ /* parent is an internal node */
switch ( which )
{
case 0: /* current is lChild of parent */
/* so compare rChild of parent and parent of parent, if any */
if ( *( parent->sc_rChild ) <= *( parent->sc_parent ) )
{
triple[currind].C = triple[num_genomes - id].B;
}
else
{
triple[currind].C = triple[num_genomes - id].C;
}
break;
case 1: /* current is rChild of parent */
/* so compare lChild of parent and parent of parent, if any */
if ( *( parent->sc_lChild ) <= *( parent->sc_parent ) )
{
triple[currind].C = triple[num_genomes - id].A;
}
else
{
triple[currind].C = triple[num_genomes - id].C;
}
break;
}
}
move_down ( current, current->lChild, 0, triple, num_genomes );
move_down ( current, current->rChild, 1, triple, num_genomes );
}
/* NN version for large numbers of genomes: all linear work */
void
initialize_tree_BNN ( int COND, struct tNode *tree, struct tNode *tpool,
struct genome_struct *labels, triple_t * triple,
struct adj_struct *adj_list,
struct adj_struct *adj_pool, int **weights, int *stack,
int *degree, int *otherEnd, intpair_t * neighbors,
edge_t * edges, int *incycle, int *outcycle, int *pred,
int *succ, int *code, int *decode, int num_genes,
int num_genomes, int inittspsolver, int thresh,