-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrspr.cpp
3766 lines (3503 loc) · 150 KB
/
rspr.cpp
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 "rspr.hpp"
std::map<std::string, ProblemSolution> SPR_memoized_clusters = std::map<std::string, ProblemSolution>();
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_SPR_depth = false;
bool check_all_pairs = true;
bool PREFER_NONBRANCHING = false;
int CLUSTER_TUNE = -1;
int SIMPLE_UNROOTED_LEAF = 0;
int rSPR_branch_and_bound_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++)
{
exact_spr = rSPR_branch_and_bound(T1,T2, k);
if (exact_spr >= 0)
{
break;
}
}
if (k > end_k)
k = -1;
return k;
}
/* rSPR_branch_and_bound
* Calculate a maximum agreement forest and SPR distance
* Uses a branch and bound optimization to not explore paths
* guaranteed to be incorrect based on rspr_3_approx
* RETURN The rSPR distance
* NOTE: destructive. The computed forests replace T1 and T2.
*/
int rSPR_branch_and_bound(Forest *T1, Forest *T2, int k)
{
// find sibling pairs of T1
if (!sync_twins(T1, T2))
return 0;
if (PREORDER_SIBLING_PAIRS &&
T1->get_component(0)->get_preorder_number() == -1)
{
T1->get_component(0)->preorder_number();
T2->get_component(0)->preorder_number();
}
if (DEEPEST_PROTECTED_ORDER
&& T1->get_component(0)->get_edge_pre_start() == -1)
{
T1->get_component(0)->edge_preorder_interval();
T2->get_component(0)->edge_preorder_interval();
}
set<SiblingPair> *sibling_pairs;
list<SPRNode *> singletons;
list<pair<Forest,Forest> > AFs = list<pair<Forest,Forest> >();
sibling_pairs = find_sibling_pairs_set(T1);
singletons = T2->find_singletons();
list<SPRNode *> protected_stack = list<SPRNode *>();
int num_ties = 2;
int final_k = rSPR_branch_and_bound_hlpr(T1, T2, k, sibling_pairs, &singletons, false, &AFs, &protected_stack, &num_ties);
if (!AFs.empty())
{
AFs.front().first.swap(T1);
AFs.front().second.swap(T2);
sync_twins(T1,T2);
}
if (final_k >= 0)
final_k = k - final_k;
delete sibling_pairs;
return final_k;
}
void add_sibling_pair(set<SiblingPair> *sibling_pairs, SPRNode *a, SPRNode *c, UndoMachine *um)
{
SiblingPair sp = SiblingPair(a,c);
pair< set<SiblingPair>::iterator, bool> ins =
sibling_pairs->insert(sp);
if (ins.second == false)
{
um->add_event(new RemoveSetSiblingPairs(sibling_pairs, *(ins.first)));
sibling_pairs->erase(ins.first);
ins = sibling_pairs->insert(sp);
}
um->add_event(new AddToSetSiblingPairs(sibling_pairs, *(ins.first)));
}
SiblingPair pop_sibling_pair(set<SiblingPair> *sibling_pairs, UndoMachine *um)
{
set<SiblingPair>::iterator s = sibling_pairs->begin();
SiblingPair spair = SiblingPair(*s);
um->add_event(new RemoveSetSiblingPairs(sibling_pairs, spair));
sibling_pairs->erase(s);
return spair;
}
SiblingPair pop_sibling_pair(set<SiblingPair>::iterator s, set<SiblingPair> *sibling_pairs, UndoMachine *um)
{
SiblingPair spair = SiblingPair(*s);
um->add_event(new RemoveSetSiblingPairs(sibling_pairs, spair));
sibling_pairs->erase(s);
return spair;
}
/* return true if T1_SPRNode matches the chain between T2_SPRNode and
T2_SPRNode_end */
bool chain_match(SPRNode *T1_SPRNode, SPRNode *T2_SPRNode, SPRNode *T2_SPRNode_end)
{
SPRNode *T1_pendant;
SPRNode *T2_pendant;
//bool pendant_found = false;
if (T2_SPRNode->is_leaf())
return false;
// T1_SPRNode is a leaf
if (T1_SPRNode->is_leaf())
{
T1_pendant = T1_SPRNode;
if (T1_pendant->get_twin() == T2_SPRNode->lchild())
{
if (T2_SPRNode->rchild() == T2_SPRNode_end)
return true;
}
else if (T1_pendant->get_twin() == T2_SPRNode->rchild())
{
if (T2_SPRNode->lchild() == T2_SPRNode_end)
return true;
}
return false;
}
// T1_pendant is T1_SPRNode->lchild()
T1_pendant = T1_SPRNode->lchild();
if (T1_pendant->is_leaf())
{
T2_pendant = T2_SPRNode->lchild();
if (T2_pendant->is_leaf() && T1_pendant->get_twin() == T2_pendant)
{
return chain_match(T1_pendant->get_sibling(),
T2_pendant->get_sibling(), T2_SPRNode_end);
}
T2_pendant = T2_SPRNode->rchild();
if (T2_pendant->is_leaf() && T1_pendant->get_twin() == T2_pendant)
{
return chain_match(T1_pendant->get_sibling(),
T2_pendant->get_sibling(), T2_SPRNode_end);
}
}
// T1_pendant is T1_SPRNode->rchild()
if (T1_pendant->is_leaf())
{
T2_pendant = T2_SPRNode->lchild();
if (T2_pendant->is_leaf() && T1_pendant->get_twin() == T2_pendant)
{
return chain_match(T1_pendant->get_sibling(),
T2_pendant->get_sibling(), T2_SPRNode_end);
}
T2_pendant = T2_SPRNode->rchild();
if (T2_pendant->is_leaf() && T1_pendant->get_twin() == T2_pendant)
{
return chain_match(T1_pendant->get_sibling(),
T2_pendant->get_sibling(), T2_SPRNode_end);
}
}
return false;
}
// rSPR_worse_3_approx recursive helper function
int rSPR_worse_3_approx_hlpr(Forest *T1, Forest *T2, list<SPRNode *> *singletons, list<SPRNode *> *sibling_pairs, Forest **F1, Forest **F2, bool save_forests)
{
int num_cut = 0;
UndoMachine um = UndoMachine();
while(!singletons->empty() || !sibling_pairs->empty())
{
// Case 1 - Remove singletons
while(!singletons->empty())
{
SPRNode *T2_a = singletons->back();
singletons->pop_back();
// find twin in T1
SPRNode *T1_a = T2_a->get_twin();
if (T2_a == T2->get_component(0))
continue;
SPRNode *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);
ContractEvent(&um, T1_a_parent);
SPRNode *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());
}
}
if(!sibling_pairs->empty())
{
SPRNode *T1_a = sibling_pairs->back();
sibling_pairs->pop_back();
SPRNode *T1_c = sibling_pairs->back();
sibling_pairs->pop_back();
um.add_event(new PopSiblingPair(T1_a, T1_c, sibling_pairs));
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;
SPRNode *T1_ac = T1_a->parent();
// lookup in T2 and determine the case
SPRNode *T2_a = T1_a->get_twin();
SPRNode *T2_c = T1_c->get_twin();
// Case 2 - Contract identical sibling pair
if (T2_a->parent() != NULL && T2_a->parent() == T2_c->parent())
{
SPRNode *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));
SPRNode *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 CreateSPRNode(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);
// check if T2_ac is a singleton
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
{
// ensure T2_a is below T2_c
if ((T2_a->get_SPR_depth() < T2_c->get_SPR_depth()
&& T2_c->parent() != NULL)
|| T2_a->parent() == NULL)
{
swap(&T1_a, &T1_c);
swap(&T2_a, &T2_c);
}
else if (T2_a->get_SPR_depth() == T2_c->get_SPR_depth())
{
if (T2_a->parent() && T2_c->parent() &&
(T2_a->parent()->get_SPR_depth() <
T2_c->parent()->get_SPR_depth()))
{
swap(&T1_a, &T1_c);
swap(&T2_a, &T2_c);
}
}
// get T2_b
bool multi_SPRNode = false;
SPRNode *T2_ab = T2_a->parent();
SPRNode *T2_b = T2_ab;
if (T2_ab->get_children().size() > 2)
multi_SPRNode = true;
else
{
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_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_SPRNode && (!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()))
{
SPRNode *T1_s = T1_ac->get_sibling();
if (T1_s->is_leaf())
{
SPRNode *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)
{
SPRNode *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;
SPRNode *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))))
{
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))))
{
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());
SPRNode *T2_ab_parent = T2_ab->parent();
node = T2_ab;
if (cut_a)
{
um.add_event(new CutParent(T2_a));
T2_a->cut_parent();
}
bool cut_b = false;
if (same_component && T2_ab_parent != NULL
&& !cut_a_only && !cut_c_only
&& (!APPROX_EDGE_PROTECTION
|| (!T2_b->is_protected() )))
{
if (multi_SPRNode)
{
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();
}
cut_b = true;
}
// T2_b will move up after contraction
else if (!multi_SPRNode)
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)
{
SPRNode *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
if (add_T2_c)
{
um.add_event(new AddComponent(T2));
T2->add_component(T2_c);
}
// may have already been added
if (T2_b->is_leaf() && cut_b)
singletons->push_back(T2_b);
num_cut+=3;
if (cut_a == false && cut_b == false && cut_c == false)
num_cut = INT_MAX-3;
}
}
}
// 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())
{
um.add_event(new AddRho(T1));
um.add_event(new AddRho(T2));
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);
}
um.undo_all();
return num_cut;
}
int rSPR_branch_and_bound_range(Forest *T1, Forest *T2, int end_k)
{
string problem_key;
map<string,ProblemSolution>::iterator i;
if (MEMOIZE)
{
problem_key = T1->str() + ":" + T2->str();
i = SPR_memoized_clusters.find(problem_key);
if (i != SPR_memoized_clusters.end())
{
Forest *new_T1 = build_finished_forest(i->second.T1);
Forest *new_T2 = build_finished_forest(i->second.T2);
T1->swap(new_T1);
T2->swap(new_T2);
sync_twins(T1, T2);
delete new_T1;
delete new_T2;
return i->second.k;
}
}
Forest F1 = Forest(T1);
Forest F2 = Forest(T2);
int approx_spr = rSPR_worse_3_approx(&F1, &F2);
int min_spr = approx_spr / 3;
int exact_spr = rSPR_branch_and_bound_range(T1, T2, min_spr, end_k);
if (MEMOIZE && exact_spr >= 0 && i == SPR_memoized_clusters.end())
{
SPR_memoized_clusters.insert(make_pair(problem_key,
ProblemSolution(T1,T2,exact_spr)));
}
return exact_spr;
}
inline int rSPR_branch_and_bound_hlpr(Forest *T1, Forest *T2, int k,
set<SiblingPair> *sibling_pairs, list<SPRNode *> *singletons,
bool cut_b_only, list<pair<Forest,Forest> > *AFs,
list<SPRNode *> *protected_stack, int *num_ties)
{
return rSPR_branch_and_bound_hlpr(T1, T2, k, sibling_pairs,
singletons, cut_b_only, AFs, protected_stack, num_ties, NULL, NULL);
}
// rSPR_branch_and_bound recursive helper function
int rSPR_branch_and_bound_hlpr(Forest *T1, Forest *T2, int k,
set<SiblingPair> *sibling_pairs, list<SPRNode *> *singletons,
bool cut_b_only, list<pair<Forest,Forest> > *AFs,
list<SPRNode *> *protected_stack, int *num_ties, SPRNode *prev_T1_a, SPRNode *prev_T1_c)
{
UndoMachine um = UndoMachine();
while(!singletons->empty() || !sibling_pairs->empty())
{
// Case 1 - Remove singletons
while(!singletons->empty())
{
SPRNode *T2_a = singletons->back();
singletons->pop_back();
// find twin in T1
SPRNode *T1_a = T2_a->get_twin();
// if this is in the first component of T_2 then
// it is not really a singleton.
SPRNode *T1_a_parent = T1_a->parent();
if (T1_a_parent == NULL)
continue;
bool potential_new_sibling_pair = T1_a_parent->is_sibling_pair();
if (T2_a == T2->get_component(0))
{
// TODO: should we do this when it happens?
if (!T1->contains_rho())
{
um.add_event(new AddRho(T1));
um.add_event(new AddRho(T2));
T1->add_rho();
T2->add_rho();
k--;
}
}
// 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);
ContractEvent(&um, T1_a_parent);
SPRNode *node = T1_a_parent->contract();
if (node != NULL && potential_new_sibling_pair && node->is_sibling_pair())
{
add_sibling_pair(sibling_pairs, node->lchild(), node->rchild(), &um);
}
}
if(!sibling_pairs->empty())
{
SPRNode *T1_a;
SPRNode *T1_c;
set<SiblingPair>::iterator deepest_valid = sibling_pairs->end();
int deepest_SPR_depth = INT_MAX;
int deepest_SPR_depth_2 = INT_MAX;
//SPRNode *best_a = NULL;
//SPRNode *best_c = NULL;
/* pop protected_stack when out of order sibling pairs
have already contracted it */
if(!protected_stack->empty()
&& protected_stack->back()->get_twin()->parent() == NULL
&& protected_stack->back()->get_twin() != T1->get_component(0))
{
um.undo_all();
return -1;
}
while(!protected_stack->empty()
&& (protected_stack->back()->is_contracted()
// this shouldn't happen
|| protected_stack->back()->get_twin()->parent() == NULL))
{
um.add_event(new ListPopBack(protected_stack));
protected_stack->pop_back();
}
if (LEAF_REDUCTION && !cut_b_only)
{
bool found = false;
set<SiblingPair>::iterator sp_i = sibling_pairs->begin();
// correct in case sibling pair involves previous
while (sp_i != sibling_pairs->end())
{
T1_a = (*sp_i).a;
T1_c = (*sp_i).c;
if (T1_a->parent() == NULL || T1_a->parent() != T1_c->parent())
{
um.add_event(new RemoveSetSiblingPairs(sibling_pairs,
SiblingPair(T1_a, T1_c)));
set<SiblingPair>::iterator rem = sp_i;
sp_i++;
sibling_pairs->erase(rem);
continue;
}
SPRNode *T2_a = T1_a->get_twin();
SPRNode *T2_c = T1_c->get_twin();
// select if this is a Case 2 or, optionally, nonbranching
if ((T2_a->parent() != NULL && T2_a->parent() == T2_c->parent())
|| (!cut_b_only && PREFER_NONBRANCHING
&& is_nonbranching(T1, T2, T1_a, T1_c, T2_a, T2_c)))
{
um.add_event(new RemoveSetSiblingPairs(sibling_pairs,
SiblingPair(T1_a, T1_c)));
set<SiblingPair>::iterator rem = sp_i;
sp_i++;
sibling_pairs->erase(rem);
found = true;
break;
}
if (DEEPEST_ORDER) {
int SPR_depth;
int SPR_depth2;
if (T1_a->get_SPR_depth() < T1_c->get_SPR_depth())
SPR_depth = T1_c->get_SPR_depth();
else
SPR_depth = T1_a->get_SPR_depth();
if (T2_a->get_SPR_depth() < T2_c->get_SPR_depth())
SPR_depth2 = T2_c->get_SPR_depth();
else
SPR_depth2 = T2_a->get_SPR_depth();
if (deepest_valid == sibling_pairs->end()
|| deepest_SPR_depth < SPR_depth
|| (deepest_SPR_depth == SPR_depth && deepest_SPR_depth_2 < SPR_depth2))
{
// TODO: this crashes on bigtest2
// Why can we end up cutting the protected SPRNode?
if (!DEEPEST_PROTECTED_ORDER
|| protected_stack->empty()
|| (protected_stack->back()->get_twin()->parent()->get_edge_pre_start()
<= T1_a->get_preorder_number()
&& protected_stack->back()->get_twin()->parent()->get_edge_pre_end()
>= T1_a->get_preorder_number()))
{
deepest_valid = sp_i;
deepest_SPR_depth = SPR_depth;
deepest_SPR_depth_2 = SPR_depth2;
}
}
}
/* TODO: create a stack of protected SPRNodes (intervals?) and only
accept the deepest sibling pair within the interval
*/
/* TODO: remember to pop the stack when we include the protected
SPRNode */
sp_i++;
}
if (!found) {
if (sibling_pairs->empty())
continue;
else {
SiblingPair spair;
if (DEEPEST_ORDER && deepest_valid != sibling_pairs->end())
spair = pop_sibling_pair(deepest_valid, sibling_pairs, &um);
else
spair = pop_sibling_pair(sibling_pairs, &um);
T1_a = spair.a;
T1_c = spair.c;
}
}
}
else {
if (prev_T1_a != NULL && prev_T1_c != NULL)
{
T1_a = prev_T1_a;
T1_c = prev_T1_c;
prev_T1_a = NULL;
prev_T1_c = NULL;
}
else {
SiblingPair spair = pop_sibling_pair(sibling_pairs, &um);
T1_a = spair.a;
T1_c = spair.c;
}
}
if (T1_a->parent() == NULL || T1_a->parent() != T1_c->parent())
{
continue;
}
if (!T1_a->can_be_sibling() || !T1_c->can_be_sibling()) {
continue;
}
SPRNode *T1_ac = T1_a->parent();
// lookup in T2 and determine the case
SPRNode *T2_a = T1_a->get_twin();
SPRNode *T2_c = T1_c->get_twin();
if (T2_a->parent() != NULL && T2_a->parent() == T2_c->parent())
{
SPRNode *T2_ac = T2_a->parent();
if (CHECK_MERGE_SPR_depth &&
(T2_a->get_max_merge_SPR_depth() > T2_ac->get_SPR_depth()
|| T2_c->get_max_merge_SPR_depth() > T2_ac->get_SPR_depth())) {
um.undo_all();
return -1;
}
if (!protected_stack->empty() &&
(T2_a == protected_stack->back()
|| T2_c == protected_stack->back())) {
um.add_event(new ListPopBack(protected_stack));
protected_stack->pop_back();
}
// CAN THIS HAPPEN TWICE?
if (!protected_stack->empty() &&
(T2_a == protected_stack->back()
|| T2_c == protected_stack->back())) {
um.add_event(new ListPopBack(protected_stack));
protected_stack->pop_back();
}
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));
SPRNode *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 CreateSPRNode(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);
// 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()) {
add_sibling_pair(sibling_pairs, T1_ac->parent()->lchild(), T1_ac->parent()->rchild(),
&um);
}
}
/* need to copy trees and lists for branching
* use forest copy constructor for T1 and T2 giving T1' and T2'
* T1' twins are in T2, and same for T2' and T1.
* singleton list will be empty except for maybe above the cut,
* so this can be created.
* fix one set of twins (T2->T1' or T1->T2' not sure)
* exploit chained twin relationship to copy sibling pair list
* fix other set of twins
* swap T2 and T2' root SPRNodes
* now do the cut
*
* note: don't copy for 3rd cut, is a waste
*/
// Case 3
// note: guaranteed that singleton list is empty
else {
if (k <= 0) {
if ((!CUT_LOST || k < 0 ||
(T1_a->num_lost_children() == 0 &&
T1_c->num_lost_children() == 0))
&& ((T2_c->parent() != NULL && T2_a->parent() != NULL)|| !T2->contains_rho())) {
singletons->clear();
um.undo_all();
return k-1;
}
}
Forest *best_T1;
Forest *best_T2;
int best_k = -1;
int answer_a = -1;
int answer_b = -1;
int answer_c = -1;
bool cut_ab_only = false;
bool cut_a_only = false;
bool cut_c_only = false;
bool cut_a_or_merge_ac = false;
bool same_component = true;
int lca_SPR_depth = -1;
int path_length = -1;
bool cut_b_only_if_not_a_or_c = false;
bool cob = false;
int undo_state = um.num_events();
// ensure T2_a is below T2_c
if ((T2_a->get_SPR_depth() < T2_c->get_SPR_depth()
&& T2_c->parent() != NULL)
|| T2_a->parent() == NULL) {
swap(&T1_a, &T1_c);
swap(&T2_a, &T2_c);
}
else if (T2_a->get_SPR_depth() == T2_c->get_SPR_depth()) {
if (T2_a->parent() && T2_c->parent() &&
(T2_a->parent()->get_SPR_depth() <
T2_c->parent()->get_SPR_depth()
))
{
swap(&T1_a, &T1_c);
swap(&T2_a, &T2_c);
}
}
SPRNode *T2_b = T2_a->parent()->rchild();
if (T2_b == T2_a)
T2_b = T2_a->parent()->lchild();
bool multi_SPRNode = false;
if (T2_a->parent()->get_children().size() > 2)
multi_SPRNode = true;
if (CUT_ONE_B) {
if (T2_a->parent()->parent() == T2_c->parent()
&& T2_c->parent() != NULL && !cut_b_only)
cut_b_only=true;
cob = true;
}
else if (CUT_ONE_AB) {
if (T2_a->parent()->parent() == T2_c->parent()
&& T2_c->parent() != NULL)
cut_ab_only=true;
}
if (CUT_TWO_B && !cut_b_only && T1_ac->parent() != NULL) {
SPRNode *T1_s = T1_ac->get_sibling();
if (T1_s->is_leaf()) {
SPRNode *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)
|| T1_s->get_twin()->is_protected())) {
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)
|| T1_s->get_twin()->is_protected())) {
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 (REVERSE_CUT_ONE_B && (!cut_b_only || (cob && multi_SPRNode)) &&
T1_ac->parent() != NULL) {
SPRNode *T1_s = T1_ac->get_sibling();
if (T1_s->is_leaf()) {
SPRNode *T2_s = T1_s->get_twin();
if (T2_s->parent() == T2_a->parent()) {
cut_c_only=true;
cut_b_only=false;
cob=false;
}
else if (T2_s->parent() == T2_c->parent()) {
if (T2_c->parent()->get_children().size() <= 2) {
cut_a_only=true;
cut_b_only=false;
cob=false;
}
else {
cut_a_or_merge_ac=true;
cut_b_only=false;
cob=false;
}
}
else if (REVERSE_CUT_ONE_B_3
// TODO: there is a chance for an additional optimization
// here. If T2_s is not protected then we can cut c or
// have to cut a (and s?)
// && T2_c->parent()->parent() != NULL
// TODO: buggy? Do we also have to consider cutting b_1 through the other b's?
&& T2_s->is_protected()
&& T2_s->parent() != NULL
&& T2_s->parent()->parent() == T2_a->parent()
&& T2_s->parent()->get_children().size() <= 2) {
//cut_c_only = true;
cut_b_only=false;