forked from cwhidden/rspr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rspr.h
executable file
·4800 lines (4450 loc) · 141 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
#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_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_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(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);
/*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 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;
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_3_approx
* Calculate an approximate maximum agreement forest and SPR distance
* RETURN At most 3 times the rSPR distance
* NOTE: destructive. The computed forests replace T1 and T2.
*/
int rSPR_3_approx(Forest *T1, Forest *T2) {
// find sibling pairs of T1
// match up nodes of T1 and T2
if (!sync_twins(T1, T2))
return 0;
// find singletons of T2
list<Node *> *sibling_pairs = T1->find_sibling_pairs();
list<Node *> singletons = T2->find_singletons();
int ans = rSPR_3_approx_hlpr(T1, T2, &singletons, sibling_pairs);
delete sibling_pairs;
return ans;
}
// rSPR_3_approx recursive helper function
int rSPR_3_approx_hlpr(Forest *T1, Forest *T2, list<Node *> *singletons,
list<Node *> *sibling_pairs) {
int num_cut = 0;
while(!singletons->empty() || !sibling_pairs->empty()) {
// Case 1 - Remove singletons
while(!singletons->empty()) {
Node *T2_a = singletons->back();
singletons->pop_back();
// 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.
if (T2_a == T2->get_component(0))
continue;
Node *T1_a_parent = T1_a->parent();
if (T1_a_parent == NULL)
continue;
bool potential_new_sibling_pair = T1_a_parent->is_sibling_pair();
// cut the edge above T1_a
T1_a->cut_parent();
T1->add_component(T1_a);
if (T1_a->get_sibling_pair_status() > 0)
T1_a->clear_sibling_pair(sibling_pairs);
//delete(T1_a);
Node *node = T1_a_parent->contract();
if (node != NULL && potential_new_sibling_pair && node->is_sibling_pair()){
node->rchild()->add_to_front_sibling_pairs(sibling_pairs, 2);
node->lchild()->add_to_front_sibling_pairs(sibling_pairs, 1);
}
}
if(!sibling_pairs->empty()) {
Node *T1_a = sibling_pairs->back();
sibling_pairs->pop_back();
Node *T1_c = sibling_pairs->back();
sibling_pairs->pop_back();
T1_a->clear_sibling_pair_status();
T1_c->clear_sibling_pair_status();
if (T1_a->parent() == NULL || T1_a->parent() != T1_c->parent()) {
continue;
}
Node *T1_ac = T1_a->parent();
// lookup in T2 and determine the case
Node *T2_a = T1_a->get_twin();
Node *T2_c = T1_c->get_twin();
// Case 2 - Contract identical sibling pair
if (T2_a->parent() != NULL && T2_a->parent() == T2_c->parent()) {
Node *T2_ac = T2_a->parent();
T1_ac->contract_sibling_pair();
T2_ac->contract_sibling_pair();
T1_ac->set_twin(T2_ac);
T2_ac->set_twin(T1_ac);
T1->add_deleted_node(T1_a);
T1->add_deleted_node(T1_c);
T2->add_deleted_node(T2_a);
T2->add_deleted_node(T2_c);
// check if T2_ac is a singleton
if (T2_ac->is_singleton() && !T1_ac->is_singleton() && T2_ac != T2->get_component(0))
singletons->push_back(T2_ac);
// check if T1_ac is part of a sibling pair
if (T1_ac->parent() != NULL && T1_ac->parent()->is_sibling_pair()) {
T1_ac->parent()->lchild()->add_to_sibling_pairs(sibling_pairs, 1);
T1_ac->parent()->rchild()->add_to_sibling_pairs(sibling_pairs, 2);
}
}
// Case 3
else {
// ensure T2_a is below T2_c
if (T2_a->get_depth() < T2_c->get_depth()) {
swap(&T1_a, &T1_c);
swap(&T2_a, &T2_c);
}
else if (T2_a->get_depth() == T2_c->get_depth()) {
if (T2_a->parent() && T2_c->parent() &&
(T2_a->parent()->get_depth() <
T2_c->parent()->get_depth())) {
swap(&T1_a, &T1_c);
swap(&T2_a, &T2_c);
}
}
// get T2_b
Node *T2_ab = T2_a->parent();
Node *T2_b = T2_ab->rchild();
if (T2_b == T2_a)
T2_b = T2_ab->lchild();
// cut T1_a, T1_c, T2_a, T2_b, T2_c
bool cut_b_only = false;
if (T2_a->parent() != NULL && T2_a->parent()->parent() != NULL && T2_a->parent()->parent() == T2_c->parent()) {
cut_b_only = true;
T1_a->add_to_sibling_pairs(sibling_pairs,1);
T1_c->add_to_sibling_pairs(sibling_pairs,2);
}
if (!cut_b_only) {
T1_a->cut_parent();
T1_c->cut_parent();
// contract parents
Node *node = T1_ac->contract();
// check for T1_ac sibling pair
if (node != NULL && node && node->is_sibling_pair()){
node->lchild()->add_to_sibling_pairs(sibling_pairs,1);
node->rchild()->add_to_sibling_pairs(sibling_pairs,2);
}
}
bool same_component = true;
if (APPROX_CHECK_COMPONENT)
same_component = (T2_a->find_root() == T2_c->find_root());
if (!cut_b_only) {
T2_a->cut_parent();
num_cut++;
}
bool cut_b = false;
if (same_component && T2_ab->parent() != NULL) {
T2_b->cut_parent();
num_cut++;
cut_b = true;
}
// T2_b will move up after contraction
else {
T2_b = T2_b->parent();
}
// check for T2 parents as singletons
Node *node = T2_ab->contract();
if (node != NULL && node->is_singleton()
&& node != T2->get_component(0))
singletons->push_back(node);
// if T2_c is gone then its replacement is in singleton list
// contract might delete old T2_c, see where it is
bool add_T2_c = true;
T2_c = T1_c->get_twin();
// ignore T2_c if it is a singleton
if (T2_c != node && T2_c->parent() != NULL && !cut_b_only) {
Node *T2_c_parent = T2_c->parent();
T2_c->cut_parent();
num_cut++;
node = T2_c_parent->contract();
if (node != NULL && node->is_singleton()
&& node != T2->get_component(0))
singletons->push_back(node);
}
else {
add_T2_c = false;
}
if (!cut_b_only)
T1->add_component(T1_a);
if (!cut_b_only)
T1->add_component(T1_c);
// put T2 cut parts into T2
if (!cut_b_only) {
T2->add_component(T2_a);
}
// may have already been added
if (cut_b) {
T2->add_component(T2_b);
}
// problem if c is deleted
if (add_T2_c) {
T2->add_component(T2_c);
}
// may have already been added
if (T2_b->is_leaf())
singletons->push_back(T2_b);
}
}
}
// if the first component of the forests differ then we have to cut p
if (T1->get_component(0)->get_twin() != T2->get_component(0)) {
num_cut++;
T1->add_rho();
T2->add_rho();
}
return num_cut;
}
/*******************************************************************************
RSPR WORSE_3_APPROX
*******************************************************************************/
/* rSPR_worse_3_approx
* Calculate an approximate maximum agreement forest and SPR distance
* RETURN At most 3 times the rSPR distance
* NOTE: destructive. The computed forests replace T1 and T2.
* T1 must be a binary tree. T2 can be a multifurcating forest.
*/
int rSPR_worse_3_approx(Forest *T1, Forest *T2) {
return rSPR_worse_3_approx(T1, T2, true);
}
int rSPR_worse_3_approx(Forest *T1, Forest *T2, bool sync) {
// match up nodes of T1 and T2
if (sync) {
if (!sync_twins(T1, T2))
return 0;
}
// cout << "T1: "; T1->print_components();
// cout << "T2: "; T2->print_components();
// find sibling pairs of T1
list<Node *> *sibling_pairs = T1->find_sibling_pairs();
// find singletons of T2
list<Node *> singletons = T2->find_singletons();
list<pair<Forest,Forest> > AFs = list<pair<Forest,Forest> >();
Forest *F1;
Forest *F2;
int ans = rSPR_worse_3_approx_hlpr(T1, T2, &singletons, sibling_pairs, &F1, &F2, true);
F1->swap(T1);
F2->swap(T2);
sync_twins(T1,T2);
delete sibling_pairs;
delete F1;
delete F2;
return ans;
}
int rSPR_worse_3_approx_distance_only(Forest *T1, Forest *T2) {
if (!sync_twins(T1, T2))
return 0;
list<Node *> *sibling_pairs = T1->find_sibling_pairs();
list<Node *> singletons = T2->find_singletons();
list<pair<Forest,Forest> > AFs = list<pair<Forest,Forest> >();
int ans = rSPR_worse_3_approx_hlpr(T1, T2, &singletons, sibling_pairs, NULL, NULL, false);
delete sibling_pairs;
return ans;
}
int rSPR_worse_3_approx(Node *subtree, Forest *T1, Forest *T2) {
return rSPR_worse_3_approx(subtree, T1, T2, true);
}
int rSPR_worse_3_approx(Node *subtree, Forest *T1, Forest *T2, bool sync) {
// match up nodes of T1 and T2
if (sync) {
if (!sync_twins(T1, T2))
return 0;
}
// cout << "T1: "; T1->print_components();
// cout << "T2: "; T2->print_components();
// find sibling pairs of T1
list<Node *> *sibling_pairs = subtree->find_sibling_pairs();
// find singletons of T2
list<Node *> singletons = T2->find_singletons();
list<pair<Forest,Forest> > AFs = list<pair<Forest,Forest> >();
Forest *F1;
Forest *F2;
int ans = rSPR_worse_3_approx_hlpr(T1, T2, &singletons, sibling_pairs, &F1, &F2, true);
F1->swap(T1);
F2->swap(T2);
sync_twins(T1,T2);
delete sibling_pairs;
delete F1;
delete F2;
return ans;
}
// rSPR_worse_3_approx recursive helper function
int rSPR_worse_3_approx_hlpr(Forest *T1, Forest *T2, list<Node *> *singletons, list<Node *> *sibling_pairs, Forest **F1, Forest **F2, bool save_forests) {
#ifdef DEBUG_APPROX
cout << "rSPR_worse_3_approx_hlpr" << endl;
cout << "\tT1: ";
T1->print_components_with_twins();
cout << "\tT2: ";
T2->print_components_with_twins();
cout << "sibling pairs:";
for (list<Node *>::iterator i = sibling_pairs->begin(); i != sibling_pairs->end(); i++) {
cout << " ";
(*i)->print_subtree_hlpr();
}
cout << endl;
#endif
int num_cut = 0;
UndoMachine um = UndoMachine();
while(!singletons->empty() || !sibling_pairs->empty()) {
// Case 1 - Remove singletons
while(!singletons->empty()) {
#ifdef DEBUG_APPROX
cout << "Case 1" << endl;
#endif
Node *T2_a = singletons->back();
singletons->pop_back();
// 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?
if (T2_a == T2->get_component(0))
continue;
Node *T1_a_parent = T1_a->parent();
if (T1_a_parent == NULL)
continue;
bool potential_new_sibling_pair = T1_a_parent->is_sibling_pair();
// cut the edge above T1_a
um.add_event(new CutParent(T1_a));
T1_a->cut_parent();
um.add_event(new AddComponent(T1));
T1->add_component(T1_a);
//if (T1_a->get_sibling_pair_status() > 0)
// T1_a->clear_sibling_pair(sibling_pairs);
//delete(T1_a);
ContractEvent(&um, T1_a_parent);
Node *node = T1_a_parent->contract();
if (node != NULL && potential_new_sibling_pair &&
node->is_sibling_pair()){
um.add_event(new AddToFrontSiblingPairs(sibling_pairs));
sibling_pairs->push_front(node->rchild());
sibling_pairs->push_front(node->lchild());
}
#ifdef DEBUG_APPROX
cout << "\tT1: ";
T1->print_components();
cout << "\tT2: ";
T2->print_components();
#endif
}
if(!sibling_pairs->empty()) {
/*
if (PREORDER_SIBLING_PAIRS) {
T1->get_component(0)->preorder_number();
list<Node *>::iterator c;
list<Node *>::iterator best_sib = sibling_pairs->end();
int best_prenum = INT_MAX;
list<Node *>::iterator T1_a_i;
list<Node *>::iterator T1_c_i;
for(c = sibling_pairs->begin(); c != sibling_pairs->end(); ) {
T1_c_i = c;
T1_c = *c;
c++;
T1_a_i = c;
T1_a = *c;
c++;
cout << T1_a->str_subtree() << endl;
cout << T1_c->str_subtree() << endl;
// if (T1_a->parent() == NULL || T1_a->parent() != T1_c->parent()) {
// cout << "invalid" << endl;
// sibling_pairs->erase(T1_c_i);
// sibling_pairs->erase(T1_a_i);
// um.add_event(new PopSiblingPair(T1_a, T1_c, sibling_pairs));
// continue;
// }
// else {
//int prenum = T1_a->parent()->get_preorder_number();
int prenum = T1_a->get_preorder_number();
cout << "prenum=" << prenum << endl;
cout << "old_prenum=" << best_prenum << endl;
if (prenum < best_prenum) {
best_sib = T1_c_i;
best_prenum = prenum;
}
cout << "new_prenum=" << best_prenum << endl;
// }
}
cout << endl;
if (best_prenum == INT_MAX)
continue;
else {
T1_c_i = best_sib;
T1_c = *T1_c_i;
best_sib++;
T1_a_i = best_sib;
T1_a = *T1_a_i;
sibling_pairs->erase(T1_a_i);
sibling_pairs->erase(T1_c_i);
}
}
else {
*/
Node *T1_a = sibling_pairs->back();
sibling_pairs->pop_back();
Node *T1_c = sibling_pairs->back();
sibling_pairs->pop_back();
um.add_event(new PopSiblingPair(T1_a, T1_c, sibling_pairs));
//if (T1_a->get_sibling_pair_status() == 0 ||
// T1_c->get_sibling_pair_status() == 0) {
// continue;
// }
//T1_a->clear_sibling_pair_status();
//T1_c->clear_sibling_pair_status();
if (T1_a->parent() == NULL || T1_c->parent() == NULL || T1_a->parent() != T1_c->parent()) {
continue;
}
if (!T1_a->can_be_sibling() || !T1_c->can_be_sibling()
|| num_cut >= INT_MAX - 3) {
continue;
}
Node *T1_ac = T1_a->parent();
// lookup in T2 and determine the case
Node *T2_a = T1_a->get_twin();
Node *T2_c = T1_c->get_twin();
#ifdef DEBUG_APPROX
cout << "Fetching sibling pair" << endl;
T1_ac->print_subtree();
cout << "T2_a" << ": ";
cout << " d=" << T2_a->get_depth() << " ";
T2_a->print_subtree();
cout << "T1_c" << ": ";
T1_c->print_subtree();
cout << "T2_c" << ": ";
cout << " d=" << T2_c->get_depth() << " ";
T2_c->print_subtree();
#endif
// Case 2 - Contract identical sibling pair
if (T2_a->parent() != NULL && T2_a->parent() == T2_c->parent()) {
#ifdef DEBUG_APPROX
cout << "Case 2" << endl;
T1->print_components();
T2->print_components();
#endif
Node *T2_ac = T2_a->parent();
um.add_event(new ContractSiblingPair(T1_ac));
T1_ac->contract_sibling_pair_undoable();
um.add_event(new ContractSiblingPair(T2_ac, T2_a, T2_c, &um));
Node *T2_ac_new = T2_ac->contract_sibling_pair_undoable(T2_a, T2_c);
if (T2_ac_new != NULL && T2_ac_new != T2_ac) {
T2_ac = T2_ac_new;
um.add_event(new CreateNode(T2_ac));
um.add_event(new ContractSiblingPair(T2_ac));
T2_ac->contract_sibling_pair_undoable();
}
um.add_event(new SetTwin(T1_ac));
um.add_event(new SetTwin(T2_ac));
T1_ac->set_twin(T2_ac);
T2_ac->set_twin(T1_ac);
//T2_ac->fix_contracted_order();
//T1->add_deleted_node(T1_a);
//T1->add_deleted_node(T1_c);
//T2->add_deleted_node(T2_a);
//T2->add_deleted_node(T2_c);
// check if T2_ac is a singleton
//if (T2_ac->is_singleton() && !T1_ac->is_singleton() && T2_ac != T2->get_component(0))
if (T2_ac->is_singleton() && T1_ac != T1->get_component(0) && T2_ac != T2->get_component(0))
singletons->push_back(T2_ac);
// check if T1_ac is part of a sibling pair
if (T1_ac->parent() != NULL && T1_ac->parent()->is_sibling_pair()) {
um.add_event(new AddToSiblingPairs(sibling_pairs));
sibling_pairs->push_back(T1_ac->parent()->lchild());
sibling_pairs->push_back(T1_ac->parent()->rchild());
}
}
// Case 3
else {
#ifdef DEBUG_APPROX
cout << "Case 3" << endl;
#endif
// ensure T2_a is below T2_c
if ((T2_a->get_depth() < T2_c->get_depth()
&& T2_c->parent() != NULL)
|| T2_a->parent() == NULL) {
#ifdef DEBUG_APPROX
cout << "swapping" << endl;
#endif
swap(&T1_a, &T1_c);
swap(&T2_a, &T2_c);
}
else if (T2_a->get_depth() == T2_c->get_depth()) {
if (T2_a->parent() && T2_c->parent() &&
(T2_a->parent()->get_depth() <
T2_c->parent()->get_depth())) {
swap(&T1_a, &T1_c);
swap(&T2_a, &T2_c);
}
}
// get T2_b
bool multi_node = false;
Node *T2_ab = T2_a->parent();
Node *T2_b = T2_ab;
if (T2_ab->get_children().size() > 2) {
multi_node = true;
}
else {
T2_b = T2_ab->rchild();
if (T2_b == T2_a)
T2_b = T2_ab->lchild();
}
#ifdef DEBUG_APPROX
cout << "T2_b" << ": ";
cout.flush();
T2_b->print_subtree();
#endif
// cut T1_a, T1_c, T2_a, T2_b, T2_c
bool cut_a_only = false;
bool cut_b_only = false;
bool cut_c_only = false;
bool cut_b_only_if_not_a_or_c = false;
if (APPROX_CUT_ONE_B && T2_a->parent() != NULL && T2_a->parent()->parent() != NULL && T2_a->parent()->parent() == T2_c->parent() && !multi_node
&& (!APPROX_EDGE_PROTECTION || !T2_b->is_protected())) {
cut_b_only = true;
um.add_event(new AddToSiblingPairs(sibling_pairs));
sibling_pairs->push_back(T1_c);
sibling_pairs->push_back(T1_a);
}
if (APPROX_CUT_TWO_B && !cut_b_only && T1_ac->parent() != NULL
&& (!APPROX_EDGE_PROTECTION || !T2_b->is_protected())) {
Node *T1_s = T1_ac->get_sibling();
if (T1_s->is_leaf()) {
Node *T2_l = T2_a->parent()->parent();
if (T2_l != NULL) {
if (T2_c->parent() != NULL && T2_c->parent()->parent() == T2_l
&& T2_a->parent()->get_children().size() > 2
&& T2_c->parent()->get_children().size() > 2) {
if (T2_l->get_sibling() == T1_s->get_twin()) {
cut_b_only=true;
}
else if (T2_l->parent() == NULL &&
(T2->contains_rho() ||
T2->get_component(0) != T2_l)) {
cut_b_only_if_not_a_or_c=true;
}
}
else if ((T2_l = T2_l->parent()) != NULL
&& T2_c->parent() == T2_l
&& T2_a->parent()->get_children().size() > 2
&& T2_a->parent()->parent()->get_children().size() > 2) {
if (T2_l->get_sibling() == T1_s->get_twin()) {
cut_b_only=true;
}
else if (T2_l->parent() == NULL &&
(T2->contains_rho() ||
T2->get_component(0) != T2_l)) {
cut_b_only_if_not_a_or_c=true;
}
}
}
}
}
if (APPROX_REVERSE_CUT_ONE_B && !cut_b_only && T1_ac->parent() != NULL) {
Node *T1_s = T1_ac->get_sibling();
if (T1_s->is_leaf()) {
if (T1_s->get_twin()->parent() == T2_a->parent()//) {
&& (!APPROX_EDGE_PROTECTION || !T2_c->is_protected())) {
cut_c_only=true;
}
else if (T1_s->get_twin()->parent() == T2_c->parent()//) {
&& (!APPROX_EDGE_PROTECTION || !T2_a->is_protected())
&& T2_c->parent()->get_children().size() <= 2) {
cut_a_only=true;
}
}
else if (APPROX_REVERSE_CUT_ONE_B_2) {
if (T2_c->parent() != NULL
&& chain_match(T1_s, T2_c->get_sibling(), T2_a) //)
&& (!APPROX_EDGE_PROTECTION || !T2_a->is_protected()))
cut_a_only = true;
}
}
if (APPROX_CUT_TWO_B_ROOT && cut_a_only == false && cut_c_only == false
&& cut_b_only_if_not_a_or_c == true) {
cut_b_only = true;
}
/*
if (CUT_LOST) {
if (T1_a->num_lost_children() > 0
|| T2_a->num_lost_children() > 0) {
cut_a_only = true;
cut_b_only = false;
cut_c_only = false;
num_cut-=3;
}
else if (T1_c->num_lost_children() > 0
|| T2_c->num_lost_children() > 0) {
cut_a_only = false;
cut_b_only = false;
cut_c_only = true;
num_cut-=3;
}
else if (T2_b->is_leaf()) {
if (T2_b->num_lost_children() > 0
|| T2_b->get_twin()->num_lost_children() > 0) {
cut_a_only = false;
cut_b_only = true;
cut_c_only = false;
num_cut-=3;
}
}
}
*/
Node *node;
bool cut_a = false;
bool cut_c = false;
if (!cut_b_only || T2_a->parent()->get_children().size() > 2) {
if (!cut_c_only &&
(!APPROX_EDGE_PROTECTION
|| (!T2_a->is_protected()
&& (T2_a->parent()->parent() != NULL
|| !T2_b->is_protected()
|| T2_a->parent()->get_children().size() > 2)))) {
// || cut_a_only)) {
um.add_event(new CutParent(T1_a));
T1_a->cut_parent();
cut_a = true;
ContractEvent(&um, T1_ac);
node = T1_ac->contract();
}
else
node = T1_ac;
if (!cut_a_only &&
(!APPROX_EDGE_PROTECTION
|| (!T2_c->is_protected()
&& (T2_c->parent() == NULL
|| T2_c->parent()->parent() != NULL
|| !T2_c->get_sibling()->is_protected()
|| T2_c->parent()->get_children().size() > 2)))) {// &&
// || cut_c_only)) {
um.add_event(new CutParent(T1_c));
T1_c->cut_parent();
cut_c = true;
if (node) {
ContractEvent(&um, node);
node = node->contract();
}
}
// contract parents
// check for T1_ac sibling pair
if (node && node->is_sibling_pair()){
um.add_event(new AddToSiblingPairs(sibling_pairs));
sibling_pairs->push_back(node->lchild());
sibling_pairs->push_back(node->rchild());
}
}
bool same_component = true;
if (APPROX_CHECK_COMPONENT && !cut_a_only && !cut_c_only)
same_component = (T2_a->find_root() == T2_c->find_root());
Node *T2_ab_parent = T2_ab->parent();
node = T2_ab;
if (cut_a) {
um.add_event(new CutParent(T2_a));
T2_a->cut_parent();
//ContractEvent(&um, T2_ab);
//node = T2_ab->contract();
}
bool cut_b = false;
if (same_component && T2_ab_parent != NULL
&& !cut_a_only && !cut_c_only
&& (!APPROX_EDGE_PROTECTION
|| (!T2_b->is_protected() ))) {
// && (T2_b->parentT2_a->parent()->parent() != NULL
// || !T2_a->is_protected())))) {
// || cut_b_only)) {
if (multi_node) {
T2_b = T2_ab;
um.add_event(new CutParent(T2_ab));
T2_ab->cut_parent();
if (T2_a->parent() != NULL) {
um.add_event(new CutParent(T2_a));
T2_a->cut_parent();
um.add_event(new AddChild(T2_a));
T2_ab_parent->add_child(T2_a);
}
else
node = T2_ab_parent;
}
else {
um.add_event(new CutParent(T2_b));
T2_b->cut_parent();
//ContractEvent(&um, node);
//node = node->contract();
}
cut_b = true;
}
// T2_b will move up after contraction
else if (!multi_node) {
T2_b = T2_b->parent();
}
if (node != NULL) {
ContractEvent(&um, node);
node = node->contract();
// check for T2 parents as singletons
if (node != NULL && node->is_singleton()
&& node != T2->get_component(0))
singletons->push_back(node);
}
// if T2_c is gone then its replacement is in singleton list
// contract might delete old T2_c, see where it is
bool add_T2_c = true;
T2_c = T1_c->get_twin();
// ignore T2_c if it is a singleton
if (cut_c && T2_c != node && T2_c->parent() != NULL) {
Node *T2_c_parent = T2_c->parent();
um.add_event(new CutParent(T2_c));
T2_c->cut_parent();
ContractEvent(&um, T2_c_parent);
node = T2_c_parent->contract();
if (node != NULL && node->is_singleton()
&& node != T2->get_component(0))
singletons->push_back(node);
}
else {
add_T2_c = false;
}
if (cut_a) {
um.add_event(new AddComponent(T1));
T1->add_component(T1_a);
um.add_event(new AddComponent(T2));
T2->add_component(T2_a);
}
if (cut_c) {
um.add_event(new AddComponent(T1));
T1->add_component(T1_c);
}
if (cut_b) {
um.add_event(new AddComponent(T2));
T2->add_component(T2_b);
}
// problem if c is deleted