-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall_sorting_reversals.c
2364 lines (2141 loc) · 89.6 KB
/
all_sorting_reversals.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $Id: all_sorting_reversals.c,v 1.36 2001/12/19 22:29:30 acs Exp $
Written by Adam Siepel, Summer 2001
Copyright 2001, Adam Siepel */
/* Code to enumerate all sorting reversals of one signed permutation
with respect to another. Algorithm is derived in Siepel, A., "An
Algorithm to Find All Sorting Reversals," RECOMB 2002, and in
Siepel, A., "Exact Algorithms for the Reversal Median Problem,"
Master's Thesis, University of New Mexico, 2001. */
#include "all_sorting_reversals.h"
#include <stdlib.h>
#include "simpleio.h"
#include "assert.h"
#include "lists.h"
/* Pass genome_structs for permutation "pi" and target "origin", with
accompanying "ngenes". Calling code is expected to allocate and
initialize the lists l and m, in which this function will record
sorting and neutral reversals, respectively (items in lists will be
of type "Reversal"). If NULL is passed for "m", then neutral
reversals will not be enumerated. Calling code may either pass a
"ReversalSortingMemory" struct, or pass NULL, in which case a new
one will be created. */
void
find_all_sorting_reversals ( List * l, List * m, struct genome_struct *pi,
struct genome_struct *origin, int ngenes,
ReversalSortingMemory * rsm )
{
int nhurdles, nsuperhurdles, nfortresses, nuoc;
int i, n, j, c, k, h1, h2, ncomp, npseudohurdles, neutral_mode;
List uoc_list;
Reversal *rev;
ReversalSortingMemory *mem;
if ( rsm == NULL )
mem = new_reversal_sorting_memory ( ngenes );
else
mem = rsm;
if ( m != NULL )
neutral_mode = 1;
else
neutral_mode = 0;
/* convert to frame of reference in which origin = identity;
simultaneously expand to equiv unsigned perm */
for ( i = 0; i < ngenes; i++ )
{
if ( origin->genes[i] < 0 )
mem->inv[abs ( origin->genes[i] )] = -1 * ( i + 1 );
else
mem->inv[origin->genes[i]] = i + 1;
}
n = 2 * ngenes + 2;
mem->upi[0] = 0;
mem->upi[n - 1] = n - 1;
for ( i = 0; i < ngenes; i++ )
{
int tmp;
if ( pi->genes[i] > 0 )
tmp = mem->inv[pi->genes[i]];
else
tmp = -1 * mem->inv[abs ( pi->genes[i] )];
if ( tmp > 0 )
{
mem->upi[2 * i + 1] = 2 * tmp - 1;
mem->upi[2 * i + 2] = 2 * tmp;
}
else
{
mem->upi[2 * i + 1] = 2 * abs ( tmp );
mem->upi[2 * i + 2] = 2 * abs ( tmp ) - 1;
}
}
/* find cycles and relative orientations */
for ( i = 0; i < n; i++ )
{
if ( i % 2 == 0 )
mem->be[i / 2] = mem->cycle[i / 2] = -1;
mem->inv2[mem->upi[i]] = i;
}
c = 0;
for ( i = 0; i < n; i += 2 )
{
if ( mem->be[i / 2] != -1 )
continue;
j = i;
while ( mem->be[j / 2] == -1 )
{
if ( j % 2 != 0 )
{ /* (odd index) */
j--; /* follow black edge */
mem->be[j / 2] = 1; /* record orientation */
mem->cycle[j / 2] = c; /* mark with cycle number */
}
else
{ /* (even index) */
mem->be[j / 2] = 0; /* record orientation */
mem->cycle[j / 2] = c; /* mark with cycle number */
j++; /* follow black edge */
}
if ( mem->upi[j] % 2 == 0 ) /* follow gray edge */
j = mem->inv2[mem->upi[j] + 1];
else
j = mem->inv2[mem->upi[j] - 1];
}
c++;
}
/* create a struct representing each connected component */
/* first get labels describing membership of gray edges in connected
components */
find_connected_components ( 0, n, mem->inv2, mem->upi,
mem->comp_label_by_pos, NULL, -1, mem );
/* get the component to which each cycle belongs */
ncomp = 0;
for ( i = 0; i <= ngenes; i++ )
{
mem->comp_label_by_cycle[mem->cycle[i]] =
mem->comp_label_by_pos[i * 2];
/* note black edge i corresponds to
position 2*i, given our
conventions here */
if ( mem->comp_label_by_cycle[mem->cycle[i]] >= ncomp )
ncomp = mem->comp_label_by_cycle[mem->cycle[i]] + 1;
}
/* create lists of edges of each orientation for each cycle */
for ( i = 0; i <= c; i++ )
{
clear_list ( &mem->cyclelist[i] );
clear_list ( &mem->cyclelist[c + 1 + i] );
}
for ( i = 0; i <= ngenes; i++ )
{
push ( &mem->cyclelist[mem->be[i] * ( c + 1 ) + mem->cycle[i]],
( void * ) i );
}
/* initialize connected component structs */
for ( i = 0; i < ncomp; i++ )
{
clear_list ( &mem->conn_comp[i].cyclelist );
clear_list ( &mem->conn_comp[i].double_superhurdle_partners );
mem->conn_comp[i].type = SIMPLEHURDLE; /* start with this assumption;
we will correct as we
proceed */
mem->conn_comp[i].protecting_superhurdle =
mem->conn_comp[i].protected_pseudohurdle = -1;
mem->conn_comp[i].chain = mem->conn_comp[i].anchor = i;
mem->conn_comp[i].mult_protected = 0;
}
/* add each cycle to the list of the component to which it
belongs; simultaneously update orientations of components */
for ( i = 0; i < c; i++ )
{
push ( &mem->conn_comp[mem->comp_label_by_cycle[i]].cyclelist,
( void * ) i );
if ( mem->conn_comp[mem->comp_label_by_cycle[i]].type != ORIENTED
&& list_size ( &mem->cyclelist[i] ) > 0
&& list_size ( &mem->cyclelist[c + 1 + i] ) > 0 )
mem->conn_comp[mem->comp_label_by_cycle[i]].type = ORIENTED;
}
/* identify trivial components */
for ( i = 0; i < ncomp; i++ )
{
if ( list_size ( &mem->conn_comp[i].cyclelist ) == 1 )
{
j = ( int ) list_get ( &mem->conn_comp[i].cyclelist, 0 );
if ( list_size ( &mem->cyclelist[j] ) +
list_size ( &mem->cyclelist[c + 1 + j] ) == 1 )
mem->conn_comp[i].type = TRIVIAL;
}
}
/* build overlap matrices for oriented components; simultaneously
count unoriented components */
for ( i = 0, nuoc = 0; i < ncomp; i++ )
{
if ( mem->conn_comp[i].type == ORIENTED )
{
#ifdef BITWISE_DETECT
build_overlap_matrix ( &mem->conn_comp[i], mem->upi, mem->inv2,
n, c, mem->cyclelist );
#endif
}
else if ( mem->conn_comp[i].type != TRIVIAL )
nuoc++;
}
nsuperhurdles = 0;
nhurdles = nuoc;
if ( nuoc >= 3 )
{ /* if two or less, all must be simple
hurdles */
/* first find sequence in which unoriented components appear;
reduce repeated appearances to single instances */
init_list ( &uoc_list, n, sizeof ( int ) );
build_uoc_list ( &uoc_list, mem->conn_comp, mem->comp_label_by_pos,
n );
/* now find and label superhurdles and double superhurdles */
find_superhurdles ( &uoc_list, mem->conn_comp, nuoc, ncomp,
&nhurdles, &nsuperhurdles );
free_list ( &uoc_list );
}
if ( nsuperhurdles == nhurdles && nhurdles % 2 == 1 )
nfortresses = 1;
else
nfortresses = 0;
/* I have not added code to enumerate neutral reversals when there
exists a fortresses. This omission is of little practical
importance because fortresses almost never are encountered except
in contrived test cases; however, if one should appear, we need
to make it abundantly clear to the user, because in such a case
the current code would return an answer certain to be very wrong */
if ( neutral_mode == 1 && nfortresses == 1 )
{
fprintf ( stderr,
"ERROR: Can't handle fortresses in neutral mode.\n" );
assert ( 0 );
}
#ifdef DEBUG
printf ( "distance = %d (c = %d; h = %d; f = %d)\n", ngenes + 1 - c +
nhurdles + nfortresses, c, nhurdles, nfortresses );
for ( i = 0; i < ncomp; i++ )
{
int j;
printf ( "Component %d is ", i );
if ( mem->conn_comp[i].type == ORIENTED )
printf ( "ORIENTED" );
else if ( mem->conn_comp[i].type == TRIVIAL )
printf ( "TRIVIAL" );
else if ( mem->conn_comp[i].type == SIMPLEHURDLE )
printf ( "a SIMPLEHURDLE" );
else if ( mem->conn_comp[i].type == SUPHURDLE )
printf ( "a SUPHURDLE" );
else if ( mem->conn_comp[i].type == PROTNHURDLE )
printf ( "a PROTNHURDLE" );
else if ( mem->conn_comp[i].type == PSEUDOHURDLE )
printf ( "a PSEUDOHURDLE" );
if ( list_size ( &mem->conn_comp[i].double_superhurdle_partners ) >
0 )
{
printf ( " (double superhurdle with %d",
( int ) list_get ( &mem->conn_comp[i].
double_superhurdle_partners, 0 ) );
if ( list_size ( &mem->conn_comp[i].double_superhurdle_partners )
> 1 )
printf ( " and %d",
( int ) list_get ( &mem->conn_comp[i].
double_superhurdle_partners,
1 ) );
printf ( ")" );
}
if ( mem->conn_comp[i].protecting_superhurdle != -1 )
printf ( " (protecting superhurdle is %d)",
mem->conn_comp[i].protecting_superhurdle );
printf ( ", spans %d to %d,", mem->conn_comp[i].begidx,
mem->conn_comp[i].endidx );
printf ( " and contains the following cycles:\n" );
for ( j = 0; j < list_size ( &mem->conn_comp[i].cyclelist ); j++ )
{
int x;
x = ( int ) list_get ( &mem->conn_comp[i].cyclelist, j );
printf ( "(%d: %d x 0, %d x 1)\n", x,
list_size ( &mem->cyclelist[x] ),
list_size ( &mem->cyclelist[c + 1 + x] ) );
}
}
#endif
/* enumerate sorting reversals that split cycles */
for ( i = 0; i <= c; i++ )
{
for ( j = 0; j < list_size ( &mem->cyclelist[i] ); j++ )
{
for ( k = 0; k < list_size ( &mem->cyclelist[c + 1 + i] ); k++ )
{
int a, b, nhf;
rev = ( Reversal * ) malloc ( sizeof ( Reversal ) );
a = ( int ) list_get ( &mem->cyclelist[i], j );
b = ( int ) list_get ( &mem->cyclelist[c + 1 + i], k );
rev->start = ( a <= b ? a : b );
rev->stop = ( a <= b ? b : a );
#ifdef BITWISE_DETECT
nhf =
new_nhurdles_plus_nfortresses ( mem->
comp_label_by_cycle[i],
rev, n, ncomp, nuoc,
nhurdles, nfortresses,
mem );
#else
nhf =
new_nhurdles_plus_nfortresses_cc ( mem->
comp_label_by_cycle[i],
rev, n, ncomp, nuoc,
nhurdles, nfortresses,
mem );
#endif
if ( nhf <= nhurdles + nfortresses )
push ( l, rev );
else if ( neutral_mode && nhf == nhurdles + nfortresses + 1 )
push ( m, rev );
else
free ( rev );
}
}
}
/* enumerate neutral reversals that act on convergent edges of the
same cycle */
if ( neutral_mode )
{
for ( i = 0; i <= c; i++ )
{
int i2;
for ( i2 = 0; i2 < 2; i2++ )
{
for ( j = 0;
j < list_size ( &mem->cyclelist[i2 * ( c + 1 ) + i] );
j++ )
{
for ( k = j + 1;
k <
list_size ( &mem->cyclelist[i2 * ( c + 1 ) + i] );
k++ )
{
int a, b, nhf;
rev = ( Reversal * ) malloc ( sizeof ( Reversal ) );
a = ( int ) list_get ( &mem->
cyclelist[i2 * ( c + 1 ) + i],
j );
b = ( int ) list_get ( &mem->
cyclelist[i2 * ( c + 1 ) + i],
k );
rev->start = ( a <= b ? a : b );
rev->stop = ( a <= b ? b : a );
if ( mem->conn_comp[mem->comp_label_by_cycle[i]].
type == ORIENTED )
{
#ifdef BITWISE_DETECT
nhf =
new_nhurdles_plus_nfortresses ( mem->
comp_label_by_cycle
[i], rev, n,
ncomp, nuoc,
nhurdles,
nfortresses,
mem );
#else
nhf =
new_nhurdles_plus_nfortresses_cc ( mem->
comp_label_by_cycle
[i], rev,
n, ncomp,
nuoc,
nhurdles,
nfortresses,
mem );
#endif
if ( nhf == nhurdles + nfortresses )
push ( m, rev );
else
free ( rev );
}
else if ( mem->conn_comp[mem->comp_label_by_cycle[i]].
type != SIMPLEHURDLE )
/* in the case of a trivial comp, we
can't get this far */
push ( m, rev );
else
free ( rev );
}
}
}
}
}
/* enumerate sorting reversals that cut simple hurdles */
npseudohurdles = 0;
if ( nsuperhurdles < nhurdles || nfortresses == 1 )
{
/* only bother if there exists at
least one simple hurdle; in the
case of a fortress, however, we
have to go on because the cutting
rules are changed */
if ( nfortresses == 1 || nsuperhurdles < 3 || nsuperhurdles % 2 == 0
|| nhurdles - nsuperhurdles > 1 )
{
/* avoid walking into a fortress! */
for ( i = 0; i < ncomp; i++ )
{
if ( mem->conn_comp[i].type == SIMPLEHURDLE )
add_all_cutting_reversals ( l, &mem->conn_comp[i],
mem->cyclelist );
else if ( nfortresses == 1
&& mem->conn_comp[i].type == PSEUDOHURDLE )
{
npseudohurdles++;
add_all_cutting_reversals ( l, &mem->conn_comp[i],
mem->cyclelist );
add_all_cutting_reversals ( l,
&mem->conn_comp[mem->
conn_comp[i].
protecting_superhurdle],
mem->cyclelist );
}
}
}
}
/* enumerate neutral reversals that act on different cycles of
simple hurdles */
if ( neutral_mode && nsuperhurdles < nhurdles )
{
if ( nfortresses == 1 || nsuperhurdles < 3 || nsuperhurdles % 2 == 0
|| nhurdles - nsuperhurdles > 1 )
{
for ( i = 0; i < ncomp; i++ )
{
if ( mem->conn_comp[i].type == SIMPLEHURDLE )
{
int j2, k2, a, b;
List *edges1, *edges2;
Reversal *rev;
for ( j = 0;
j < list_size ( &mem->conn_comp[i].cyclelist );
j++ )
{
for ( j2 = j + 1;
j2 < list_size ( &mem->conn_comp[i].cyclelist );
j2++ )
{
edges1 =
&mem->
cyclelist[( int )
list_get ( &mem->conn_comp[i].
cyclelist, j )];
edges2 =
&mem->
cyclelist[( int )
list_get ( &mem->conn_comp[i].
cyclelist, j2 )];
for ( k = 0; k < list_size ( edges1 ); k++ )
{
for ( k2 = 0; k2 < list_size ( edges2 );
k2++ )
{
a = ( int ) list_get ( edges1, k );
b = ( int ) list_get ( edges2, k2 );
rev =
( Reversal * )
malloc ( sizeof ( Reversal ) );
rev->start = ( a <= b ? a : b );
rev->stop = ( a <= b ? b : a );
push ( m, rev );
}
}
}
}
}
}
}
}
/* build list of hurdles to save time in subsequent loops */
clear_list ( mem->mhurdles );
for ( i = 0; i < ncomp; i++ )
{
if ( mem->conn_comp[i].type == SIMPLEHURDLE ||
mem->conn_comp[i].type == SUPHURDLE )
push ( mem->mhurdles, ( void * ) i );
}
/* The following steps all relate to merging of separate components,
in one form or another. The handling here is not as clean as the
corresponding theory (the code was developed before the theory
was complete), but I believe a close reading will show the code
and the theory to be equivalent. */
/* enumerate sorting reversals that merge hurdles */
if ( nhurdles >= 2 )
{
for ( i = 0; i < list_size ( mem->mhurdles ); i++ )
{
for ( j = i + 1; j < list_size ( mem->mhurdles ); j++ )
{
h1 = ( int ) list_get ( mem->mhurdles, i );
h2 = ( int ) list_get ( mem->mhurdles, j );
if ( nfortresses == 0 &&
eliminating_components_creates_fortress ( h1, h2,
mem->conn_comp,
nhurdles,
nsuperhurdles ) )
continue;
if ( form_double_superhurdle ( h1, h2, mem->conn_comp ) &&
nfortresses == 0 )
continue; /* avoid merging a double superhurdle */
add_all_merging_reversals ( l, &mem->conn_comp[h1],
&mem->conn_comp[h2],
mem->cyclelist, c );
}
}
}
/* enumerate sorting reversals that merge oriented components (with
other oriented components or with unoriented components) and
simultaneously orient hurdles */
if ( neutral_mode || nhurdles >= 2 )
{
int is_separator[ncomp], sepclass[ncomp], bc_hurdchain[ncomp],
lidx[ncomp], llabel[nhurdles], is_separated[ncomp];
int nseparators = 0, h;
List *ocs = NULL;
/* find all separating hurdles and the benign components that they
separate */
find_separating_hurdles ( is_separator, sepclass, bc_hurdchain,
&nseparators, n, ncomp, mem, &ocs, lidx,
llabel, is_separated, neutral_mode,
nfortresses );
if ( neutral_mode || nseparators > 0 || nfortresses > 0 )
{
/* merge each of the identified ocs with every hurdle EXCEPT the
one that separates it */
for ( i = 0; i < nseparators; i++ )
{
int separator, oc;
separator = llabel[i];
for ( k = 0; k < list_size ( &ocs[i] ); k++ )
{
oc = ( int ) list_get ( &ocs[i], k );
for ( j = 0; j < list_size ( mem->mhurdles ); j++ )
{
h = ( int ) list_get ( mem->mhurdles, j );
if ( h == separator )
continue;
if ( nfortresses == 0 &&
eliminating_components_creates_fortress ( h,
separator,
mem->
conn_comp,
nhurdles,
nsuperhurdles ) )
continue;
if ( form_double_superhurdle
( h, separator, mem->conn_comp )
&& nfortresses == 0 )
continue; /* avoid eliminating a double superhurdle */
add_all_merging_reversals ( l, &mem->conn_comp[oc],
&mem->conn_comp[h],
mem->cyclelist, c );
}
}
}
/* merge pairs of ocs that are separated by different hurdles */
for ( i = 0; i < nseparators; i++ )
{
int h1 = llabel[i];
for ( j = i + 1; j < nseparators; j++ )
{
int k, k2;
int h2 = llabel[j];
if ( nfortresses == 0 &&
eliminating_components_creates_fortress ( h1, h2,
mem->
conn_comp,
nhurdles,
nsuperhurdles ) )
continue;
if ( form_double_superhurdle ( h1, h2, mem->conn_comp ) &&
nfortresses == 0 )
continue; /* avoid eliminating a double superhurdle */
for ( k = 0; k < list_size ( &ocs[i] ); k++ )
{
int o1 = ( int ) list_get ( &ocs[i], k );
for ( k2 = 0; k2 < list_size ( &ocs[j] ); k2++ )
{
int o2 = ( int ) list_get ( &ocs[j], k2 );
add_all_merging_reversals ( l,
&mem->conn_comp[o1],
&mem->conn_comp[o2],
mem->cyclelist, c );
}
}
}
}
/* in the cases of "neutral mode" and of a fortress, we have
several additional classes of merges to consider */
if ( neutral_mode || nfortresses == 1 )
{
/* we will need a list of benign components that have no
separating hurdle */
List non_separated_ocs;
init_list ( &non_separated_ocs, ncomp, sizeof ( int ) );
for ( i = 0; i < ncomp; i++ )
if ( is_separated[i] == 0
&& ( mem->conn_comp[i].type == ORIENTED
|| mem->conn_comp[i].type == TRIVIAL ) )
push ( &non_separated_ocs, ( void * ) i );
if ( nfortresses == 1 )
{
/* we will construct two lists, to_merge1 and to_merge2,
then merge each component of one list with each component
of the other */
List to_merge1, to_merge2;
init_list ( &to_merge1, ncomp, sizeof ( int ) );
init_list ( &to_merge2, ncomp, sizeof ( int ) );
for ( i = 0; i < list_size ( mem->mhurdles ); i++ )
{
int h = ( int ) list_get ( mem->mhurdles, i );
push ( &to_merge1, ( void * ) h );
for ( j = 0;
lidx[h] != -1
&& j < list_size ( &ocs[lidx[h]] ); j++ )
{
int oc = ( int ) list_get ( &ocs[lidx[h]], j );
push ( &to_merge1, ( void * ) oc );
}
/* anchor of chain */
if ( mem->conn_comp[h].anchor != -1 )
{
push ( &to_merge2,
( void * ) mem->conn_comp[h].anchor );
}
/* all protected nonhurdles not in chain */
for ( j = 0; j < ncomp; j++ )
{
if ( ( mem->conn_comp[j].type == PROTNHURDLE ||
mem->conn_comp[j].type == PSEUDOHURDLE ) &&
mem->conn_comp[j].chain !=
mem->conn_comp[h].chain )
{
push ( &to_merge2, ( void * ) j );
}
}
/* merge with benign components that do not have
separating hurdles */
for ( j = 0; j < list_size ( &non_separated_ocs );
j++ )
{
int bc =
( int ) list_get ( &non_separated_ocs, j );
/* don't add if the benign component falls in the middle
of hurdle h's chain */
if ( bc_hurdchain[bc] != mem->conn_comp[h].chain )
push ( &to_merge2, ( void * ) bc );
}
for ( j = 0; j < list_size ( &to_merge1 ); j++ )
{
int c1 = ( int ) list_get ( &to_merge1, j );
for ( k = 0; k < list_size ( &to_merge2 ); k++ )
{
int c2 = ( int ) list_get ( &to_merge2, k );
add_all_merging_reversals ( l,
&mem->
conn_comp[c1],
&mem->
conn_comp[c2],
mem->cyclelist,
c );
}
}
clear_list ( &to_merge1 );
clear_list ( &to_merge2 );
}
free_list ( &to_merge1 );
free_list ( &to_merge2 );
}
/* enumerate neutral reversals that merge components */
if ( neutral_mode )
{
List tmpl;
init_list ( &tmpl, ncomp, sizeof ( int ) );
for ( i = 0; i < list_size ( mem->mhurdles ); i++ )
{
int hrdl = ( int ) list_get ( mem->mhurdles, i );
/* anchor of chain */
if ( mem->conn_comp[hrdl].anchor != -1 )
push ( &tmpl,
( void * ) mem->conn_comp[hrdl].anchor );
/* hurdle itself, if it's a simple hurdle but not the
anchor of its own chain (rare but possible) */
if ( mem->conn_comp[hrdl].type == SIMPLEHURDLE &&
mem->conn_comp[hrdl].anchor != hrdl )
push ( &tmpl, ( void * ) hrdl );
/* protected nonhurdles not in chain */
for ( j = 0; j < ncomp; j++ )
{
if ( mem->conn_comp[j].type == PROTNHURDLE ||
mem->conn_comp[j].type == PSEUDOHURDLE )
{
/* unanchored case */
if ( mem->conn_comp[hrdl].anchor == -1 )
{
/* only merge if j is the penultimate UOC on the
_other_ end of the hurdle chain; our convention
is to set the "chain" attribute of penultimate
UOCs to -1 * the label of their adjacent hurdles
- 1 (to ensure negativity) */
if ( mem->conn_comp[j].chain < 0 &&
mem->conn_comp[j].chain !=
-1 * hrdl - 1 )
push ( &tmpl, ( void * ) j );
}
/* anchored case: merge with any PNH of another chain */
else
{
if ( mem->conn_comp[j].chain !=
mem->conn_comp[hrdl].chain
&& j != mem->conn_comp[hrdl].anchor )
push ( &tmpl, ( void * ) j );
}
}
}
/* benign comps w/o separating hurdles */
for ( j = 0; j < list_size ( &non_separated_ocs );
j++ )
{
int bc =
( int ) list_get ( &non_separated_ocs, j );
/* have to distinguish between anchored and unanchored cases */
if ( ( mem->conn_comp[hrdl].anchor != -1 &&
bc_hurdchain[bc] !=
mem->conn_comp[hrdl].chain )
|| ( mem->conn_comp[hrdl].anchor == -1
&& bc_hurdchain[bc] < 0
&& bc_hurdchain[bc] != -1 * hrdl - 1 ) )
push ( &tmpl, ( void * ) bc );
}
/* double superhurdle partners */
for ( j = 0;
j <
list_size ( &mem->conn_comp[hrdl].
double_superhurdle_partners ); j++ )
if ( ( int )
list_get ( &mem->conn_comp[hrdl].
double_superhurdle_partners,
j ) > hrdl )
{
/* '>' to be sure we only add once */
int partner =
( int ) list_get ( &mem->conn_comp[hrdl].
double_superhurdle_partners,
j );
push ( &tmpl, ( void * ) partner );
/* also add separated benign components */
for ( k = 0; lidx[partner] != -1 &&
k < list_size ( &ocs[lidx[partner]] );
k++ )
push ( &tmpl,
list_get ( &ocs[lidx[partner]],
k ) );
}
/* now merge everything in tmpl with hrdl and with each of
the BCs that hrdl separates */
for ( j = 0; j < list_size ( &tmpl ); j++ )
{
int comp = ( int ) list_get ( &tmpl, j );
if ( hrdl != comp )
add_all_merging_reversals ( m,
&mem->
conn_comp[hrdl],
&mem->
conn_comp[comp],
mem->cyclelist,
c );
if ( lidx[hrdl] != -1 )
{
for ( k = 0;
k < list_size ( &ocs[lidx[hrdl]] );
k++ )
{
int c1 =
( int ) list_get ( &ocs[lidx[hrdl]],
k );
add_all_merging_reversals ( m,
&mem->
conn_comp[c1],
&mem->
conn_comp
[comp],
mem->
cyclelist,
c );
}
}
}
/* benign comps of different sep classes (only if simple hurdle) */
if ( lidx[hrdl] != -1
&& mem->conn_comp[hrdl].type == SIMPLEHURDLE )
{
for ( k = 0; k < list_size ( &ocs[lidx[hrdl]] );
k++ )
{
int k2, c1 =
( int ) list_get ( &ocs[lidx[hrdl]], k );
for ( k2 = k + 1;
k2 < list_size ( &ocs[lidx[hrdl]] );
k2++ )
{
int c2 =
( int ) list_get ( &ocs[lidx[hrdl]],
k2 );
if ( sepclass[c1] != sepclass[c2] )
add_all_merging_reversals ( m,
&mem->
conn_comp
[c1],
&mem->
conn_comp
[c2],
mem->
cyclelist,
c );
}
}
}
clear_list ( &tmpl );
}
free_list ( &tmpl );
}
free_list ( &non_separated_ocs );
}
for ( i = 0; i < nseparators; i++ )
free_list ( &ocs[i] );
free ( ocs );
}
}
if ( rsm == NULL )
free_reversal_sorting_memory ( mem );
}
/* To run in normal mode, set source_comp_label to NULL and
restrict_to to -1. To restrict to consideration of a certain
component (as when searching for a new unoriented component), set
source_comp_label to an array indicating present component labels
by position and set restrict_to the the label of the desired
component */
void
find_connected_components ( int begidx, int len, int *inv2, int *upi,
int *dest_comp_label,
int *source_comp_label, int restrict_to,
ReversalSortingMemory * mem )
{
int extent_b, extent_e;
int i, top, comp;
if ( source_comp_label == NULL ) /* be sure that they're
consistent ... */
restrict_to = -1;
for ( i = begidx; i < len + begidx; i++ )
{
if ( restrict_to != -1 && source_comp_label[i] != restrict_to )
continue;
if ( upi[i] % 2 == 0 )
{
mem->cc_e[i] = upi[i];
mem->cc_beg[mem->cc_e[i]] = ( i < inv2[upi[i] + 1] ? i
: inv2[upi[i] + 1] );
mem->cc_end[mem->cc_e[i]] = ( i < inv2[upi[i] + 1] ?
inv2[upi[i] + 1] : i );
}
else
{
mem->cc_e[i] = upi[i] - 1;
mem->cc_beg[upi[i]] = mem->cc_end[upi[i]] = -1;
/* leave undef for odds */
}
}
for ( i = begidx; i < len + begidx; i++ )
{
if ( restrict_to != -1 && source_comp_label[i] != restrict_to )
continue;
dest_comp_label[i] = mem->cc_beg[mem->cc_e[i]];
mem->cc_parent[i] = -1;
}
clear_list ( mem->stack );
for ( i = begidx; i < len + begidx; i++ )
{
if ( restrict_to != -1 && source_comp_label[i] != restrict_to )
continue;
if ( i == mem->cc_beg[mem->cc_e[i]] )
push ( mem->stack, ( void * ) mem->cc_e[i] );
top = ( int ) peek_stack ( mem->stack );
extent_b = mem->cc_beg[mem->cc_e[i]];
extent_e = mem->cc_end[mem->cc_e[i]];
while ( mem->cc_beg[top] > mem->cc_beg[mem->cc_e[i]] )
{
extent_b =
( extent_b < mem->cc_beg[top] ? extent_b : mem->cc_beg[top] );
extent_e =
( extent_e > mem->cc_end[top] ? extent_e : mem->cc_end[top] );
mem->cc_parent[mem->cc_beg[top]] = mem->cc_beg[mem->cc_e[i]];
pop_stack ( mem->stack );
top = ( int ) peek_stack ( mem->stack );
}
mem->cc_beg[top] = ( extent_b < mem->cc_beg[top] ? extent_b :
mem->cc_beg[top] );
mem->cc_end[top] = ( extent_e > mem->cc_end[top] ? extent_e :
mem->cc_end[top] );
if ( i == mem->cc_end[top] )
pop_stack ( mem->stack );
}
comp = 0;
for ( i = begidx; i < len + begidx; i++ )
{
if ( restrict_to != -1 && source_comp_label[i] != restrict_to )
continue;
if ( mem->cc_parent[dest_comp_label[i]] == -1 )
{
if ( i == dest_comp_label[i] )
{
/* beginning of new component;
increment label */
dest_comp_label[i] = comp;
if ( restrict_to == -1 ) /* don't do this in the "detect" case */
mem->conn_comp[comp].begidx =
mem->conn_comp[comp].endidx = i;
comp++;
}
else
{ /* refer to label at beginning of this
component */
dest_comp_label[i] = dest_comp_label[dest_comp_label[i]];
if ( restrict_to == -1 ) /* don't do this in the "detect" case */
mem->conn_comp[dest_comp_label[i]].endidx = i + 1;
}