-
Notifications
You must be signed in to change notification settings - Fork 7
/
rspr.h
executable file
·7057 lines (6524 loc) · 209 KB
/
rspr.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
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
/*******************************************************************************
rspr.h
Calculate approximate and exact Subtree Prune and Regraft (rSPR)
distances and the associated maximum agreement forests (MAFs) between pairs
of rooted binary trees.
Supports arbitrary labels. See the
README for more information.
Copyright 2009-2014 Chris Whidden
http://kiwi.cs.dal.ca/Software/RSPR
April 29, 2014
Version 1.2.2
This file is part of rspr.
rspr 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.
rspr 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 rspr. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
#define RSPR
//#define DEBUG 1
//#define DEBUG_CONTRACTED 1
//#define DEBUG_APPROX 1
//#define DEBUG_CLUSTERS 1
//#define DEBUG_SYNC 1
//#define DEBUG_UNDO 1
//#define DEBUG_DEPTHS 1
//#define DEBUG_CASE_COUNTER 1
//#define MULT_PICK_LARGEST_GROUP 1
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <iostream>
#include <sstream>
#include <climits>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <algorithm>
#include "Forest.h"
#include "ClusterForest.h"
#include "LCA.h"
#include "ClusterInstance.h"
#include "SiblingPair.h"
#include "UndoMachine.h"
using namespace std;
enum RELAXATION {STRICT, NEGATIVE_RELAXED, ALL_RELAXED};
const string whitespaces = " \t\f\v\n\r";
// note: not using undo
int rSPR_worse_3_mult_approx_hlpr(Forest *T1, Forest *T2, list<Node *> *singletons, list<Node *> *sibling_pairs, Forest **F1, Forest **F2, bool save_forests);
int rSPR_worse_3_mult_approx(Forest *T1, Forest *T2);
int rSPR_worse_3_mult_approx(Forest *T1, Forest *T2, bool sync);
int rSPR_branch_and_bound_mult_range(Forest *T1, Forest *T2, int start_k);
int rSPR_branch_and_bound_mult_range(Forest *T1, Forest *T2, int start_k, int end_k);
int rSPR_branch_and_bound_mult(Forest *T1, Forest *T2, int k);
int rSPR_branch_and_bound_mult_hlpr(Forest *T1, Forest *T2, int k, list<Node*> *sibling_groups, list<Node*> *singletons, Node *protected_stack, list<pair<Forest,Forest>> *AFs, int* num_ties);
int rSPR_3_approx_hlpr(Forest *T1, Forest *T2, list<Node *> *singletons,
list<Node *> *sibling_pairs);
int rSPR_3_approx(Forest *T1, Forest *T2);
int rSPR_worse_3_approx_hlpr(Forest *T1, Forest *T2, list<Node *> *singletons, list<Node *> *sibling_pairs, Forest **F1, Forest **F2, bool save_forests);
int rSPR_worse_3_approx(Forest *T1, Forest *T2);
int rSPR_worse_3_approx(Forest *T1, Forest *T2, bool sync);
int rSPR_worse_3_approx(Node *subtree, Forest *T1, Forest *T2);
int rSPR_worse_3_approx(Node *subtree, Forest *T1, Forest *T2, bool sync);
int rSPR_worse_3_approx_binary_hlpr(Forest *T1, Forest *T2, list<Node *> *singletons, list<Node *> *sibling_pairs, Forest **F1, Forest **F2, bool save_forests);
int rSPR_worse_3_approx_binary(Forest *T1, Forest *T2, bool sync);
int rSPR_worse_3_approx_binary(Forest *T1, Forest *T2);
int rSPR_branch_and_bound(Forest *T1, Forest *T2);
int rSPR_branch_and_bound(Forest *T1, Forest *T2, int k);
int rSPR_branch_and_bound(Forest *T1, Forest *T2, int k,
map<string, int> *label_map,
map<int, string> *reverse_label_map);
int rSPR_branch_and_bound_range(Forest *T1, Forest *T2, int end_k);
int rSPR_branch_and_bound_range(Forest *T1, Forest *T2, int start_k,
int end_k);
int rSPR_branch_and_bound_hlpr(Forest *T1, Forest *T2, int k,
set<SiblingPair> *sibling_pairs, list<Node *> *singletons, bool cut_b_only,
list<pair<Forest,Forest> > *AFs, list<Node *> *protected_stack,
int *num_ties);
int rSPR_branch_and_bound_hlpr(Forest *T1, Forest *T2, int k,
set<SiblingPair> *sibling_pairs, list<Node *> *singletons, bool cut_b_only,
list<pair<Forest,Forest> > *AFs, list<Node *> *protected_stack,
int *num_ties, Node *prev_T1_a, Node *prev_T1_c);
int rSPR_total_approx_distance(Node *T1, vector<Node *> &gene_trees);
int rSPR_total_approx_distance(Node *T1, vector<Node *> &gene_trees,
int threshold);
int rSPR_total_distance(Node *T1, vector<Node *> &gene_trees);
int rSPR_total_distance(Node *T1, vector<Node *> &gene_trees,
vector<int> *original_scores);
void rSPR_pairwise_distance(Node *T1, vector<Node *> &gene_trees);
void rSPR_pairwise_distance(Node *T1, vector<Node *> &gene_trees, bool approx);
void rSPR_pairwise_distance(Node *T1, vector<Node *> &gene_trees, int start, int end);
void rSPR_pairwise_distance(Node *T1, vector<Node *> &gene_trees, int start, int end, bool approx);
void rSPR_pairwise_distance(Node *T1, vector<Node *> &gene_trees, int max_spr);
void rSPR_pairwise_distance(Node *T1, vector<Node *> &gene_trees, int max_spr, int start, int end);
void rSPR_pairwise_distance_unrooted(Node *T1, vector<Node *> &gene_trees);
void rSPR_pairwise_distance_unrooted(Node *T1, vector<Node *> &gene_trees, int start, int end);
void rSPR_pairwise_distance_unrooted(Node *T1, vector<Node *> &gene_trees, bool approx);
void rSPR_pairwise_distance_unrooted(Node *T1, vector<Node *> &gene_trees, int start, int end, bool approx);
int rf_total_distance(Node *T1, vector<Node *> &gene_trees);
int rf_total_distance_unrooted(Node *T1, vector<Node *> &gene_trees);
void rf_pairwise_distance(Node *T1, vector<Node *> &gene_trees);
void rf_pairwise_distance(Node *T1, vector<Node *> &gene_trees, int start, int end);
void rf_pairwise_distance_unrooted(Node *T1, vector<Node *> &gene_trees);
void rf_pairwise_distance_unrooted(Node *T1, vector<Node *> &gene_trees, int start, int end);
int rSPR_total_distance_unrooted(Node *T1, vector<Node *> &gene_trees, int threshold);
int rSPR_total_distance_unrooted(Node *T1, vector<Node *> &gene_trees, int threshold, vector<int> *original_scores);
int rSPR_branch_and_bound_simple_clustering(Node *T1, Node *T2, Forest **out_F1, Forest **out_F2);
int rSPR_branch_and_bound_simple_clustering(Node *T1, Node *T2, bool verbose, map<string, int> *label_map, map<int, string> *reverse_label_map);
int rSPR_branch_and_bound_simple_clustering(Node *T1, Node *T2, bool verbose, map<string, int> *label_map, map<int, string> *reverse_label_map, int min_k, int max_k);
int rSPR_branch_and_bound_simple_clustering(Node *T1, Node *T2);
int rSPR_branch_and_bound_simple_clustering(Node *T1, Node *T2, bool verbose);
int rSPR_branch_and_bound_simple_clustering(Node *T1, Node *T2, bool verbose, int min_k, int max_k);
int rSPR_branch_and_bound_simple_clustering(Node *T1, Node *T2, bool verbose, map<string, int> *label_map, map<int, string> *reverse_label_map, int min_k, int max_k, Forest **out_F1, Forest **out_F2);
void reduction_leaf_mult(Forest *T1, Forest *T2);
void reduction_leaf(Forest *T1, Forest *T2);
void reduction_leaf(Forest *T1, Forest *T2, UndoMachine *um);
bool chain_match(Node *T1_node, Node *T2_node, Node *T2_node_end);
Node *find_subtree_of_approx_distance(Node *n, Forest *F1, Forest *F2, int target_size);
Node *find_best_root(Node *T1, Node *T2);
double find_best_root_acc(Node *T1, Node *T2);
void find_best_root_hlpr(Node *T2, int pre_separator, int group_1_total,
int group_2_total, Node **best_root, double *best_root_b_acc);
void find_best_root_hlpr(Node *n, int pre_separator, int group_1_total,
int group_2_total, Node **best_root, double *best_root_b_acc,
int *p_group_1_descendants, int *p_group_2_descendants, int *num_ties);
int rf_distance(Node *T1, Node *T2);
int count_differing_bipartitions(Node *n);
bool contains_bipartition(Node *n, int pre_start, int pre_end,
int group_1_total, int group_2_total, int *p_group_1_descendants,
int *p_group_2_descendants);
void modify_bipartition_support(Node *T1, Node *T2, enum RELAXATION relaxed);
void modify_bipartition_support(Node *n, Forest *F1, Forest *F2,
Node *T1, Node *T2, vector<int> *F1_descendant_counts, enum RELAXATION);
void modify_bipartition_support(Forest *F1, Forest *F2, Node *n1);
bool is_nonbranching(Forest *T1, Forest *T2, Node *T1_a, Node *T1_c, Node *T2_a, Node *T2_c);
bool outgroup_root(Node *T, set<string, StringCompare> outgroup);
bool outgroup_root(Node *n, vector<int> &num_in, vector<int> &num_out);
bool outgroup_reroot(Node *n, vector<int> &num_in, vector<int> &num_out);
void count_in_out(Node *n, vector<int> &num_in, vector<int> &num_out,
set<string, StringCompare> &outgroup);
void randomize_tree_with_spr(Node* T1, Node* T2, int count);
/*Joel's part*/
int rSPR_branch_and_bound_simple_clustering(Forest *T1, Forest *T2, bool verbose, map<string, int> *label_map, map<int, string> *reverse_label_map);
int rSPR_branch_and_bound_simple_clustering(Forest *T1, Forest *T2);
int rSPR_branch_and_bound_simple_clustering(Forest *T1, Forest *T2, bool verbose);
int rSPR_total_distance(Forest *T1, vector<Node *> &gene_trees);
bool BB = false;
bool APPROX_CHECK_COMPONENT = false;
bool APPROX_REVERSE_CUT_ONE_B = false;
bool APPROX_REVERSE_CUT_ONE_B_2 = false;
bool APPROX_CUT_ONE_B = false;
bool APPROX_CUT_TWO_B = false;
bool APPROX_CUT_TWO_B_ROOT = false;
bool APPROX_EDGE_PROTECTION = false;
bool CUT_ONE_B = false;
bool REVERSE_CUT_ONE_B = false;
bool REVERSE_CUT_ONE_B_2 = false;
bool REVERSE_CUT_ONE_B_3 = false;
bool CUT_TWO_B = false;
bool CUT_TWO_B_ROOT = false;
bool CUT_ALL_B = false;
bool CUT_AC_SEPARATE_COMPONENTS = false;
bool CUT_ONE_AB = false;
bool CLUSTER_REDUCTION = false;
bool PREFER_RHO = false;
bool MAIN_CALL = true;
bool MEMOIZE = false;
bool MULTIFURCATING = false;
bool MULT_4_BRANCH = false;
bool USE_CASE_7 = true;
bool ALL_MAFS = false;
int NUM_CLUSTERS = 0;
int MAX_CLUSTERS = -1;
bool UNROOTED_MIN_APPROX = false;
bool VERBOSE = false;
bool CLAMP = false;
int MAX_SPR = 1000;
int CLUSTER_MAX_SPR = MAX_SPR;
int MIN_SPR = 0;
bool FIND_RATE = false;
bool EDGE_PROTECTION = false;
bool EDGE_PROTECTION_TWO_B = false;
bool ABORT_AT_FIRST_SOLUTION = false;
bool PREORDER_SIBLING_PAIRS = false;
bool DEEPEST_ORDER = false;
bool DEEPEST_PROTECTED_ORDER = false;
bool NEAR_PREORDER_SIBLING_PAIRS = false;
bool LEAF_REDUCTION = false;
bool LEAF_REDUCTION2 = false;
bool SPLIT_APPROX = false;
bool IN_SPLIT_APPROX = false;
int SPLIT_APPROX_THRESHOLD = 25;
float INITIAL_TREE_FRACTION = 0.4;
bool COUNT_LOSSES = false;
bool CUT_LOST = false;
bool CHECK_MERGE_DEPTH = false;
bool check_all_pairs = true;
bool PREFER_NONBRANCHING = false;
int CLUSTER_TUNE = -1;
int SIMPLE_UNROOTED_LEAF = 0;
bool SHOW_CLUSTERS = false;
class ProblemSolution {
public:
string T1;
string T2;
int k;
ProblemSolution(Forest *t1, Forest *t2, int new_k) {
T1 = t1->str();
T2 = t2->str();
k = new_k;
}
};
map<string, ProblemSolution> memoized_clusters = map<string, ProblemSolution>();
/*******************************************************************************
RSPR WORSE_3_MULT_APPROX
*******************************************************************************/
/* rSPR_worse_3_mult_approx
* Calculate an approximate maximum agreement forest and SPR distance for two multifurcating trees
* RETURN At most 3 times the rSPR distance
* NOTE: destructive. The computed forests replace T1 and T2.
* T1 and T2 can be multifurcating forests.
*/
int rSPR_worse_3_mult_approx(Forest *T1, Forest *T2) {
return rSPR_worse_3_mult_approx(T1, T2, true);
}
int rSPR_worse_3_mult_approx(Forest *T1, Forest *T2, bool sync) {
// match up nodes of T1 and T2
if (sync) {
if (!sync_twins(T1, T2))
return 0;
}
if (LEAF_REDUCTION) {
reduction_leaf_mult(T1, T2);
}
// cout << "T1: "; T1->print_components();
// cout << "T2: "; T2->print_components();
// find sibling pairs of T1
list<Node *> *sibling_groups = T1->find_sibling_groups();
/*
list<Node *>::iterator c;
list<Node *>::iterator ch;
for (c = sibling_groups->begin(); c != sibling_groups->end(); c++) {
cout << "Sibling group parent: " << (*c)->str() << endl;
list<Node *> children = (*c)->get_children();
for (ch = children.begin(); ch != children.end(); ch++) {
cout << "\tChild: " << (*ch)->str() <<endl;
}
list<list<Node *>> *identical_sibling_groups = (*c)->find_identical_sibling_groups();
if (identical_sibling_groups->size() > 0) {
cout << "Printing identical sibling group:" << endl;
for (auto g = identical_sibling_groups->begin(); g != identical_sibling_groups->end(); g++) {
cout << "Sibling identical group:" << endl;
for (auto ge = g->begin(); ge != g->end(); ge++) {
cout << "\tchild:" << (*ge)->get_name() << endl;
}
}
}
else {
cout << "No identical sibling groups" <<endl;
}
}
//return 0;
*/
// find singletons of T2
list<Node *> singletons = T2->find_singletons();
//list<pair<Forest,Forest> > AFs = list<pair<Forest,Forest> >();
Forest *F1;
Forest *F2;
T2->max_preorder = T2->components[0]->get_max_preorder_number(0);//preorder_number(0);
int ans = rSPR_worse_3_mult_approx_hlpr(T1, T2, &singletons, sibling_groups, &F1, &F2, true);
F1->swap(T1);
F2->swap(T2);
sync_twins(T1,T2);
delete sibling_groups;
delete F1;
delete F2;
return ans;
}
// rSPR_worse_3_mult_approx recursive helper function
int rSPR_worse_3_mult_approx_hlpr(Forest *T1, Forest *T2, list<Node *> *singletons, list<Node *> *sibling_groups, Forest **F1, Forest **F2, bool save_forests) {
int num_cut = 0;
Node* previous_group = NULL;
while(!singletons->empty() || !sibling_groups->empty()) {
// Case 1 - Remove singletons
while(!singletons->empty()) {
Node *T2_a = singletons->back();
singletons->pop_back();
#ifdef DEBUG_APPROX
cout << "Handling singleton: " << T2_a->str() << endl;
#endif
// find twin in T1
Node *T1_a = T2_a->get_twin();
// if this is in the first component of T_2 then
// it is not really a singleton.
// TODO: problem when we cluster and have a singleton as the
// first comp of T2
// NEED TO MODIFY CUTTING?
// HERE AND IN BB?
Node *T1_a_p = T1_a->parent();
if (T1_a_p == NULL)
continue;
if (T2_a == T2->get_component(0)){
if (!T1->contains_rho()) {
T1->add_rho();
T2->add_rho();
num_cut++;
}
}
//continue;
bool is_sibling_group = T1_a_p->is_sibling_group();
// cut the edge above T1_a
T1_a->cut_parent();
if (!T1_a->is_leaf()) {
T1_a_p->decrement_non_leaf_children();//although would this ever be a non leaf?
}
T1->add_component(T1_a);
//only contract if one node
if (T1_a_p->get_children().size() == 1) {
if (is_sibling_group) {
sibling_groups->remove(T1_a_p);
#ifdef DEBUG_APPROX
cout << "Removed ";
for (list<Node*>::iterator i = T1_a_p->get_children().begin(); i != T1_a_p->get_children().end(); i++) {
cout << (*i)->str();
}
cout << " from sibling groups" << endl;
#endif
}
Node *possible_previous_sibling = T1_a_p->get_children().front();
bool was_sibling_group = possible_previous_sibling->is_sibling_group();
Node *node = T1_a_p->contract(true);
if (node != NULL) {
node->recalculate_non_leaf_children(); //can we tell what this would be instead of recalculating?
if (node->is_sibling_group()) {
if (was_sibling_group) {
list<Node*>::iterator i = find(sibling_groups->begin(), sibling_groups->end(), possible_previous_sibling);
*i = node;
}
else{
sibling_groups->push_front(node);
}
}
}
}
}//!singletons->empty()
if(!sibling_groups->empty()) {
//Get the first group that has identical sibling groups, otherwise default to the group on the back
list<Node*>::iterator i = sibling_groups->end();
i--;
Node *T1_sibling_group = sibling_groups->back();
list<list<Node*>> identical_sibling_groups = list<list<Node*>>();
T1_sibling_group->find_identical_sibling_groups(&identical_sibling_groups);
for (; i != sibling_groups->begin(); i-- ){
(*i)->find_identical_sibling_groups(&identical_sibling_groups);
if (identical_sibling_groups.size() > 0) {
T1_sibling_group = (*i);
break;
}
}
#ifdef DEBUG_APPROX
cout << "F2: ";
T2->print_components();
cout << endl;
cout << "F1: ";
T1->print_components();
cout << endl;
#endif
/*
Case where a subset of the group have the same parent both in T1 and T2
Step 5 in paper
*/
// Case 2 - Contract identical sibling pair
if (identical_sibling_groups.size() > 0) {
list<list<Node *>>::iterator i;
for (i = identical_sibling_groups.begin(); i != identical_sibling_groups.end(); i++) {
list<Node *> T2_group = (*i);
Node *T2_p = T2_group.front()->parent();
#ifdef DEBUG_APPROX
cout << "Contracting T1... " << endl;
#endif
Node *T1_group_new = T1_sibling_group->contract_twin_group(&T2_group);
#ifdef DEBUG_APPROX
cout << "Contracting T2... " << endl;
#endif
Node *T2_group_new = T2_p->contract_sibling_group(&T2_group);
T1_group_new->set_twin(T2_group_new);
T2_group_new->set_twin(T1_group_new);
// check if T2_p is a singleton after the contraction
if (T2_p->is_singleton() && T2_p != T2->get_component(0)) {
//(T2_p->is_singleton() && T1_sibling_group != T1->get_component(0) && T2_p != T2->get_component(0)) {
singletons->push_front(T2_p);
}
if (T1_sibling_group->parent() != NULL) {
//Check if the contraction made a new sibling group
T1_sibling_group->parent()->recalculate_non_leaf_children();
if (T1_sibling_group->parent()->is_sibling_group()) {
sibling_groups->push_front(T1_sibling_group->parent());
}
}
if (!T1_sibling_group->is_sibling_group()) {
sibling_groups->remove(T1_sibling_group);
}
}
}
/*
4 branching case
Step 6-8 in paper
Cut above a1, b1, a2, b2
Part 1: Get the LCA, this is the numbering to the top part
Part 2: Get subset of sibling group that is descendant of this LCA
Part 3: Sort them based on depth
Part 4: Since this is the approximation, we cut deepest 2
*/
// Case 3
else {
#ifdef DEBUG_APPROX
cout << "Sibling group to be cutting: " << T1_sibling_group->str_subtree() << endl;
#endif
//vector of ints describing how many of the siblings are in its descendants, indexed by preorder number
vector<int> descendant_count = T1_sibling_group->find_pseudo_lca_descendant_count(T2->max_preorder + 1);
Node* arbitrary_lca = T1_sibling_group->find_arbitrary_lca(T2->components, descendant_count);
vector<Node *> deepest_siblings;
//All siblings are in different components, ie no path between them
//Get depth of siblings from root of each component
if (arbitrary_lca == NULL) {
vector<vector<Node *>> siblings_by_depth = vector<vector<Node *>>(10);
for (int i = 0; i != T2->components.size(); i++) {
T2->components[i]->get_deepest_siblings(descendant_count, siblings_by_depth);
}
deepest_siblings = contract_deepest_siblings(siblings_by_depth);
}
//Otherwise they share an LCA
else {
vector<vector<Node *>> siblings_by_depth = arbitrary_lca->get_deepest_siblings(descendant_count);
deepest_siblings = contract_deepest_siblings(siblings_by_depth);
}
// Should assert here
if (deepest_siblings.size() < 2) { cout << "improper length" << endl; }
//Get the deepest two of the siblings
Node *T2_a1 = deepest_siblings[0];
Node *T2_a2 = deepest_siblings[1];
#ifdef DEBUG_APPROX
cout << "a1: " << T2_a1->str() << " a2: " << T2_a2->str() << endl;
#endif
bool cut_a1 = false;
bool cut_a1_p = false;
bool cut_a2 = false;
bool cut_a2_p = false;
if (T1_sibling_group->get_children().size() == 2) {
/*
7.1 case
Cut a1, pa1, a2 in F2, add 3 to num_cut
Consider adding 3 regardless if we actually cut 3,
*/
cut_a1 = true;
cut_a1_p = true;
cut_a2 = true;
//num_cut += 3;
/*
7.2 case
Cut a1, a2, pa1, pa2, add 4 to num_cuts
*/
if (previous_group == T1_sibling_group) {
cut_a2_p = true;
#ifdef DEBUG_APPROX
cout <<"Case 7.2" << endl;
#endif
//num_cut += 1;
}
else {
#if DEBUG_APPROX
cout << "Case 7.1" << endl;
#endif
}
} // size == 2
else if (T1_sibling_group->get_children().size() > 2) {
/*
7.3 case
If a2's parent's only sibling is part of the sibling group,
and a1's parent is a root or has a sibling that is not part of the sibling group
then cut a2 and a2_p otherwise a1 and a1_p
*/
if (previous_group != T1_sibling_group) {
Node* T2_a2_p = T2_a2->parent();
bool x_2 = false;
bool a2_p_one_sibling = (T2_a2_p != NULL) &&
(T2_a2_p->parent() != NULL) &&
(T2_a2_p->parent()->get_children().size() == 2);
if (a2_p_one_sibling) {
list<Node *> group = T1_sibling_group->get_children();
//get the other one
Node *a2_p_sibling = T2_a2_p->parent()->get_children().front() == T2_a2_p ?
T2_a2_p->parent()->get_children().back() :
T2_a2_p->parent()->get_children().front();
//check if it is part of sibling group
bool a2_p_sibling_in_group = descendant_count[a2_p_sibling->get_preorder_number()] == -1;
if (a2_p_sibling_in_group) {
Node* T2_a1_p = T2_a1->parent();
bool a1_p_is_root = T2_a1_p->parent() == NULL;
bool a1_p_sibling_not_in_group = false;
if (!a1_p_is_root) {
list<Node*> a1_p_siblings = T2_a1_p->parent()->get_children();
for (list<Node*>::iterator i = a1_p_siblings.begin(); i != a1_p_siblings.end(); i++) {
if (descendant_count[(*i)->get_preorder_number()] != -1) {
a1_p_sibling_not_in_group = true;
break;
}
}
}
if (a2_p_one_sibling && a2_p_sibling_in_group && (a1_p_is_root || a1_p_sibling_not_in_group)) {
x_2 = true;
}
}
}
#ifdef DEBUG_APPROX
cout << "Case 7.3" << endl;
#endif
//num_cut += 2;
if (x_2){
cut_a2 = true;
cut_a2_p = true;
}
else {
cut_a1 = true;
cut_a1_p = true;
}
}
/*7.4 case
cut a1 and a1_p
*/
else if (previous_group == T1_sibling_group) {
#ifdef DEBUG_APPROX
cout << "Case 7.4" << endl;
#endif
cut_a1 = true;
cut_a1_p = true;
//num_cut += 2;
}
}
/*
Cutting section
*/
Node* T2_a2_p = NULL;
if (cut_a1) {
Node *T2_a1_p = T2_a1->parent();
//singletons? components?
if (T2_a1_p != NULL) {
//Cut connections
T2_a1->cut_parent();
num_cut++;
//add as components
T2->add_component(T2_a1);
//Just cut 1 of two children of a1 parent
if (T2_a1_p->get_children().size() == 1) {
if (T2_a1_p->parent() == NULL) {
T2_a1_p->contract(true);
if (T2_a1_p->is_singleton() && T2_a1_p != T2->get_component(0)) {
singletons->push_front(T2_a1_p);
}
}
else {
Node* T2_b1 = T2_a1_p->get_children().front();
T2_a1_p->contract(true);
T2_a1_p = T2_b1; // <------------------------------------
}
}
//Check for singletons
if (T2_a1->is_singleton()) // wont ever be C0?
singletons->push_front(T2_a1);
if (cut_a1_p) {
if (T2_a1_p->parent() != NULL) {
bool aborted_a2 = false;
if (T2_a1_p->parent() == T2_a2->parent() &&
T2_a2->parent()->get_children().size() == 2)
{
cut_a2 = false; //cutting a1_p will cause a2 to get contracted up, so there is no more a2 to cut
cut_a2_p = true; //Instead we cut a2_p
aborted_a2 = true;
}
Node *T2_a1_gp = T2_a1_p->parent();
//Cut connections
T2_a1_p->cut_parent();
num_cut++;
//add as components
T2->add_component(T2_a1_p);
//Just cut 1 of two children of a1 parent
if (T2_a1_gp->get_children().size() == 1) {
if (T2_a1_gp->parent() == NULL) {
T2_a1_gp->contract(true);
}
else {
Node* T2_b1_p = T2_a1_gp->get_children().front();
T2_a1_gp->contract(true);
T2_a1_gp = T2_b1_p;
}
if (aborted_a2) { T2_a2_p = T2_a1_gp; }
if (T2_a1_gp != NULL && T2_a1_gp->is_singleton() && (T2_a1_gp != T2->get_component(0) || aborted_a2)) {
singletons->push_front(T2_a1_gp);
}
}
//Check for singletons
if (T2_a1_p->is_singleton() && T2_a1_p != T2->get_component(0)) {
singletons->push_front(T2_a1_p);
}
}
}
}//T2_a1_p() != NULL
}
if (cut_a2){
T2_a2_p = T2_a2->parent();
//could have cut a2's parent in previous steps, so could be singleton now
if (T2_a2->is_leaf() && T2_a2_p == NULL) {
singletons->push_front(T2_a2);
}
else if (T2_a2_p != NULL) {
//Cut connections
T2_a2->cut_parent();
num_cut++;
//add as components
T2->add_component(T2_a2);
//Just cut 1 of two children of a2 parent
if (T2_a2_p->get_children().size() == 1) {
if (T2_a2_p->parent() == NULL) {
T2_a2_p->contract(true);
if (T2_a2_p->is_singleton() && T2_a2_p != T2->get_component(0)) {
singletons->push_front(T2_a2_p);
}
}
else {
Node* T2_b2 = T2_a2_p->get_children().front();
T2_a2_p->contract(true);
T2_a2_p = T2_b2;
}
}
//Check for singletons
if (T2_a2->is_singleton())
singletons->push_front(T2_a2);
}
}
if (cut_a2_p) {
if (T2_a2_p != NULL && T2_a2_p->parent() != NULL) {
Node *T2_a2_gp = T2_a2_p->parent();
//Cut connections
T2_a2_p->cut_parent();
num_cut++;
//add as components
T2->add_component(T2_a2_p);
//Just cut 1 of two children of a2 parent
if (T2_a2_gp->get_children().size() == 1) {
if (T2_a2_gp->parent() == NULL) {
T2_a2_gp->contract(true);
}
else {
Node* T2_b2_p = T2_a2_gp->get_children().front();
T2_a2_gp->contract(true);
T2_a2_gp = T2_b2_p;
}
if (T2_a2_gp->is_singleton() && T2_a2_gp != T2->get_component(0)) {
singletons->push_front(T2_a2_gp);
}
}
//Check for singletons
if (T2_a2_p->is_singleton()) {
singletons->push_front(T2_a2_p);
}
}
}
}//else
//delete identical_sibling_groups;
previous_group = T1_sibling_group;
}//!sibling_groups->empty()
} //while(!sibling_groups->empty() && !singletons->empty()
// if the first component of the forests differ then we have cut p
/*
if (T1->get_component(0)->get_twin() != T2->get_component(0)) {
if (!T1->contains_rho()) {
T1->add_rho();
T2->add_rho();
}
else
// hack to ignore rho when it shouldn't be in a cluster
num_cut -=3;
}
*/
if (save_forests) {
*F1 = new Forest(T1);
*F2 = new Forest(T2);
}
//if (num_cut) num_cut--;
return num_cut;
}
#ifdef DEBUG_CASE_COUNTER
struct mult_case_counter {
int case_71, case_72, case_73, case_74;
int case_81, case_82, case_83, case_84;
int case_85, case_86, case_87;
};
static mult_case_counter case_counter = {};
void print_mult_case_count() {
cout << "Stats:" << endl;
cout << "\tCase 7.1:" << case_counter.case_71 << endl;
cout << "\tCase 7.2:" << case_counter.case_72 << endl;
cout << "\tCase 7.3:" << case_counter.case_73 << endl;
cout << "\tCase 7.4:" << case_counter.case_74 << endl;
cout << "\tCase 8.1:" << case_counter.case_81 << endl;
cout << "\tCase 8.2:" << case_counter.case_82 << endl;
cout << "\tCase 8.3:" << case_counter.case_83 << endl;
cout << "\tCase 8.4:" << case_counter.case_84 << endl;
cout << "\tCase 8.5:" << case_counter.case_85 << endl;
cout << "\tCase 8.6:" << case_counter.case_86 << endl;
cout << "\tCase 8.7:" << case_counter.case_87 << endl;
}
#endif
int rSPR_branch_and_bound_mult_range(Forest *T1, Forest *T2, int start_k){
return rSPR_branch_and_bound_mult_range(T1, T2, start_k, MAX_SPR);
}
int rSPR_branch_and_bound_mult_range(Forest *T1, Forest *T2, int start_k, int end_k){
int exact_spr = -1;
int k;
for (k = start_k; k <= end_k; k++) {
Forest *F1 = new Forest(T1);
Forest *F2 = new Forest(T2);
if (!sync_twins(F1,F2)) {
exact_spr = 0;
continue;
}
if (LEAF_REDUCTION) {
reduction_leaf_mult(F1, F2);
}
#ifdef DEBUG
cout << "Trying K = " << k << endl << "------------------" << endl;
#else
cout << k << " " << endl;
#endif
exact_spr = rSPR_branch_and_bound_mult(F1, F2, k);
#ifdef DEBUG
cout << "------------------" << endl;
cout << "Finished K = " << k << " return value : " << exact_spr << endl;
#endif
if (exact_spr >= 0) {
F1->swap(T1);
F2->swap(T2);
}
delete F1;
delete F2;
if (exact_spr >= 0) {
break;
}
}
#ifdef DEBUG_CASE_COUNTER
print_mult_case_count();
#endif
if (k > end_k) {
k = -1;
}
return k;
}
int rSPR_branch_and_bound_mult(Forest *T1, Forest *T2, int k){
if (!sync_twins(T1,T2)) {
return 0;
}
T2->max_preorder = T2->components[0]->get_max_preorder_number(0);//preorder_number(0);
list<Node *> *sibling_groups = T1->find_sibling_groups();
//sibling_groups->push_front(sibling_groups->back());
//sibling_groups->pop_back();
list<Node *> singletons = T1->find_singletons();
list<pair<Forest,Forest>> AFs = list<pair<Forest,Forest>>();
//list<Node *> protected_stack = list<Node*>();
int num_ties = 2;
int final_k = rSPR_branch_and_bound_mult_hlpr(T1, T2, k, sibling_groups, &singletons, NULL, &AFs, &num_ties);
//print AFs
if (!AFs.empty() && final_k > -1) {
if (ALL_MAFS) {
cout << endl << endl << "FOUND ANSWER" << endl;
// TODO: this is a cheap hack
for (list<pair<Forest,Forest> >::iterator x = AFs.begin(); x != AFs.end(); x++) {
cout << "\tT1: ";
x->first.print_components();
cout << "\tT2: ";
x->second.print_components();
}
}
AFs.front().first.swap(T1);
AFs.front().second.swap(T2);
sync_twins(T1,T2);
}
delete sibling_groups;
if (final_k >= 0)
return k - final_k;
else
return final_k;
}
class bb_mult_recurse_data {
public:
Forest *T1;
Forest *T2;
list<Node*> *sibling_groups;
list<Node*> *singletons;
map<Node *, Node*> node_map;
};
/* Generates a copy of the data used to recurse on rSPR_branch_and_bound_mult_hlpr so
that original forests aren't clobbered,
updates pointers accordingly */
//TODO: 1 new not 5
bb_mult_recurse_data *generate_mult_recurse_data(Forest *T1, Forest *T2, list<Node*> *sibling_groups, list<Node*> *singletons) {
bb_mult_recurse_data *data = new bb_mult_recurse_data();
data->node_map = map<Node*, Node*>();
data->T1 = new Forest(T1, &data->node_map);
data->T2 = new Forest(T2, &data->node_map);
//TODO: smarter way of syncing
//for (auto n = node_map.begin(); n != node_map.end(); n++) {
//(*n).first->set_twin(node_map[(*n).first->get_twin()]);
/*
if ((*n).second->is_leaf()) {
(*n).second->set_twin(node_map[(*n).first->get_twin()]);
node_map[(*n).first->get_twin()]->set_twin((*n).second);
cout << "Synced twin : " << (*n).second->str() << " -> " << (*n).second->get_twin()->str() << endl;}*/
//}
sync_twins(data->T1, data->T2);
data->sibling_groups = new list<Node*>();
for (auto n = sibling_groups->begin(); n != sibling_groups->end(); n++) {
data->sibling_groups->push_back(data->node_map[*n]);
}
data->singletons = new list<Node*>();
for (auto n = singletons->begin(); n != singletons->end(); n++) {
data->singletons->push_back(data->node_map[*n]);
}
return data;
}
//Cuts to_cut, adds to components, conditionally adds to singletons
//Assumes parent is not null and no Null parameters
void mult_cut_and_cleanup(Node* to_cut, Forest *T2, list<Node*> *singletons) {
//Node* T2_a1_next = next_data->node_map[T2_a1];
Node* to_cut_p = to_cut->parent();
//Cut connections
to_cut->cut_parent();
//add as components
T2->add_component(to_cut);
//Just cut 1 of two children of a1 parent
if (to_cut_p->get_children().size() == 1) {
if (to_cut_p->parent() == NULL) {
to_cut_p->contract(true);
if (to_cut_p->is_singleton() && to_cut_p != T2->get_component(0)) {
singletons->push_front(to_cut_p);
}
}
else {
Node* to_cut_b = to_cut_p->get_children().front();
to_cut_p->contract(true);
if (to_cut_b->is_singleton() && to_cut_b != T2->get_component(0)) {
singletons->push_front(to_cut_b);
}
}
}
//Check for singletons
if (to_cut->is_singleton())
singletons->push_front(to_cut);
}
//cuts everything except node, possibly expanding, adds to components, conditionally adds to singletons
//Assumes parent is not null and no Null parameters
//NOTE: potentially unsafe for preorder numbers
void mult_cut_all_except_and_cleanup(Node* T2_a1, Forest *T2, list<Node*> *singletons) {
Node* T2_b1;
Node* parent = T2_a1->parent();
if (parent->get_children().size() == 2) {
T2_b1 = parent->get_children().front() == T2_a1 ?
parent->get_children().back() :
parent->get_children().front();
}
else {
list<Node*> all_but_a1 = list<Node*>(parent->get_children());
all_but_a1.remove(T2_a1);
T2_b1 = parent->expand_children_out(all_but_a1);
T2_b1->set_preorder_number(parent->get_preorder_number());
}
//hack for now
//We immediately contract a1 up so we know the parent's preorder number is available
//Cut connections
T2_b1->cut_parent();
//add as components
T2->add_component(T2_b1);
//Just cut 1 of two children of a2 parent (will always be in this case?)
if (parent->get_children().size() == 1) {
if (parent->parent() == NULL) {
parent->contract(true);
if (parent->is_singleton() && parent != T2->get_component(0)) {
singletons->push_front(parent);
}
}
else {
parent = parent->contract(true);
if (T2_a1->is_singleton() && T2_a1 != T2->get_component(0))
singletons->push_front(T2_a1);
}
}
if (T2_b1->is_singleton())
singletons->push_front(T2_b1);
}
//Adds rho and a certain singleton to cut. Basically its for realising when we have cut rho but it is already a singleton so we explicitly continue on
#define MULT_RHO_CUT_AND_RESOLVE(rho_to_singleton, node_to_protect) { \
bb_mult_recurse_data *next_data = generate_mult_recurse_data(T1, T2, sibling_groups, singletons); \