forked from lonnen/nauty
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcubhamg.c
1985 lines (1748 loc) · 51.2 KB
/
cubhamg.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
/* cubhamg.c : pick those inputs that are nonhamiltonian and
have max degree <= 3.
Usage:
cubhamg [-#] [-v|-V] [-n#-#|-y#-#|-i|-I|-o|-x|-e|-E] [-b|-t] [infile [outfile]]
infile is the name of the input file in graph6/sparse6 format
outfile is the name of the output file in the same format
stdin and stdout are the defaults for infile and outfile
The output file will have a header >>graph6<< or >>sparse6<<
if and only if the input file does.
Optional switches:
-# A parameter useful for tuning (default 100)
-v Report nonhamiltonian graphs and noncubic graphs
-V .. in addition give a cycle for the hamiltonian ones
-n#-# If the two numbers are v and i, then the i-th edge
out of vertex v is required to be not in the cycle.
It must be that i=1..3 and v=0..n-1.
-y#-# If the two numbers are v and i, then the i-th edge
out of vertex v is required to be in the cycle.
It must be that i=1..3 and v=0..n-1.
You can use any number of -n/-y switches to force
edges. Out of range first arguments are ignored.
If -y and -n give same edge, -y wins.
-i Test + property: for each edge e, there is a hamiltonian
cycle using e.
-I Test ++ property: for each pair of edges e,e', there is
a hamiltonian cycle which uses both e and e'.
-o Test - property: for each edge e, there is a hamiltonian
cycle avoiding e.
-x Test +- property: for each pair of edges e,e', there is
a hamiltonian cycle which uses e but avoids e'.
-e Test 3/4 property: for each edge e, at least 3 of the 4
paths of length 3 passing through e lie on hamiltonian cycles.
-E Test 3/4+ property: for each edge e failing the 3/4 property,
all three ways of joining e to the rest of the graph are
hamiltonian avoiding e.
-T# Specify a timeout, being a limit on how many search tree
nodes are made. If the timeout occurs, the graph is
written to the output as if it is nonhamiltonian.
-R# Specify the number of repeat attempts for each stage.
-F Analyze covering paths from 2 or 4 vertices of degree 2.
-b Require biconnectivity
-t Require triconnectivity (note: quadratic algorithm)
-y, -n, -#, -R and -T are ignored for -i, -I, -x, -o, -e, -E, -F
B. D. McKay, Nov 1995 + Aug 1996 + Feb 2002 + Jul 2008 + Nov 2015
**************************************************************************/
#ifndef MAXN
#define MAXN 30002 /* 2 more than largest graph size! */
#endif
#if MAXN==0
#error MAXN cannot be zero for cubhamg
#endif
#include "gtools.h"
#include "naurng.h"
/**************************************************************************/
#define RANPERM TRUE
/* cubham.h */
#define MAXNE ((3 * MAXN) / 2)
#define FALSE 0
#define TRUE 1
#define YES 1
#define DUNNO 0
#define NO (-1)
#define HABORT 5
#define BADLIM 100 /* limit on number of bad things to report per graph */
#define MAXA 100 /* max number of -y or -n switches */
typedef int cubgraph[MAXN][4];
typedef int vertvec[MAXN];
typedef int edgevec[MAXNE+1];
static long nodecount,maxnodes,totalnodes;
static long timeout;
static long repeats;
static int verbose;
#define NO_LIMIT 0x7FFFFFFFL
static long standard[]
= {30,40,50,60,100,200,300,400,500,1000,2000,3000,5000,
10000,20000,30000,100000,300000,1000000};
#define NUMMAXNODES (sizeof(standard)/sizeof(long))
static nauty_counter numtries[NUMMAXNODES+1];
/* cubham.c */
/***************************************************************************
***** Instructions for using cubham. *****
cubham can find hamiltonian cycles in graphs of maximum
degree at most 3, with specifed edges either required or forbidden.
To use it:
(1) Check that MAXN (defined in cubham.h) is at least equal to the
number of vertices.
(2) #include cubham.h.
(3) The cubic graph must be stored in an object of type cubgraph. Say
you call it g. The vertices are numbered 0,1,..., and the neighbours of
vertex i are g[i][0], g[i][1] and g[i][2]. Entries of the form g[i][3]
are unused. If the degree is less than 3, -1 is used as padding.
(4) Call cubinit(g,eno,v1,v2,nv,&ne), where
g = the cubic graph (input)
eno = another object of type cubgraph (output)
v1,v2 = objects of type edgevec (output)
nv = the number of vertices (input)
ne = the number of edges (output)
This numbers the edges 0,1,...ne-1, and defines
eno[i][j] = the number of the edge {i, g[i][j]}
{v1[j], v2[j]} = the vertices of the j-th edge.
(5) Call cubham(g,eno,initclass,v1,v2,cycle,outclass,nv,ne), where
g,eno,v1,v2,nv = as above (input)
initclass = initial edge classification (input)
outclass = final edge classifiction (output)
cycle = the hamiltonian cycle found, if any (output)
The value returned by cubham is either YES (hamiltonian cycle found)
or NO (there isn't any).
The initial edge classification is specified by you in the edgevec
initclass[]. For each edge j, (0 <= j < 3*nv/2), set
initclass[j] = NO, YES or DUNNO, if edge j must not be in the cycle,
must be in the cycle, or don't care, respectively. Passing NULL as
the initclass parameter is equivalent to setting each edge to DUNNO.
All initial classifications are legal, even those clearly impossible.
The final edge classification is set by cubham in the edgevec
outclass[], if a hamiltonian cycle is found. Each entry should be
either NO or YES. No final classification is provided if NULL is passed
as the outclass parameter.
The hamiltonian cycle itself, if any, is returned as cycle[0], cycle[1],
..., cycle[nv-1], cycle[0]. If the cycle is not needed, you can pass
NULL for this parameter.
Step (4) need not be repeated if the same graph is processed again with
a different initial classification.
****************************************************************************/
#define POP(x) (onstack[x = *(--stackptr)] = 0)
#define PUSH(x) if(onstack[x]!=stacklev){onstack[x]=stacklev;*(stackptr++)=x;}
#define RESETSTACK {stacklev++; stackptr = stack;}
typedef struct
{
edgevec class;
vertvec din,dout,farend;
} nodedata;
static nodedata hcnodat;
static cubgraph eno;
static vertvec onstack,stack; /* stack contains vertex numbers */
static int *stackptr,stacklev; /* stackptr points above top */
static int classstack[4*MAXNE]; /* stack of classifications */
/* x >= 0 : edge number
(x < 0 above y : farend[-x-1] = y */
static int *classstackptr; /* points above top of classstack */
static int classout(cubgraph,nodedata*,int,int,int);
static int classin(cubgraph,cubgraph,nodedata*,int,int,int,int*,int);
#define MAXES 0
#if MAXES
static int maxlevel,maxclassstack;
#endif
static void
dummy(void)
{
}
static void
check_it(int index, cubgraph g, cubgraph eno, edgevec v1, edgevec v2,
int *din, int *dout, int *class, int *farend, int nin, int nv,
int stable)
/* Check some things */
{
int xdin[MAXN],xdout[MAXN],xnin,v,i,j,k,l,*gv,xin,xout,has1;
for (i = 0; i < nv; ++i) xdin[i] = xdout[i] = 0;
xnin = has1 = 0;
for (v = 0; v < nv; ++v)
{
gv = g[v];
xin = xout = 0;
for (i = 0; i < 3; ++i)
if (gv[i] >= 0)
{
j = eno[v][i];
if (class[j] == NO) ++xout;
if (class[j] == YES) ++xin;
}
if (xout != dout[v] || xin != din[v])
{
fprintf(stderr,">E%d degrees of %d: din,dout=%d,%d really %d,%d\n",
index,v,din[v],dout[v],xin,xout);
dummy();
}
if (xin == 1) ++has1;
xnin += xin;
}
xnin /= 2;
if (xnin != nin)
{
fprintf(stderr,">E%d nin=%d actually %d\n",index,nin,xnin);
dummy();
}
if (nin != 0 && !has1)
{
fprintf(stderr,">E%d nin=%d has no in=1\n",index,nin);
dummy();
}
for (i = 0; i < nv; ++i)
if (din[i] == 0)
{
if (farend[i] != i)
{
fprintf(stderr,">E%d farend[isolate %d]=%d\n",
index,i,farend[i]);
dummy();
}
}
else if (din[i] == 1)
{
k = -1;
j = i;
do
{
for (l = 0; l < 3; ++l)
if (g[j][l] >= 0 && g[j][l] != k && class[eno[j][l]] == YES)
break;
k = j;
if (l < 3) j = g[j][l];
} while (l < 3);
if (farend[i] != j)
{
fprintf(stderr,">E%d farend[%d]=%d really %d\n",
index,i,farend[i],j);
dummy();
}
}
if (stable)
for (i = 0; i < nv; ++i)
if ((dout[i] == 1 && din[i] != 2) || (din[i] == 2 && dout[i] != 1)
|| dout[i] > 1 || din[i] > 2)
{
fprintf(stderr,">E%d din[%d]=%d dout[%d]=%d\n",
index,i,din[i],i,dout[i]);
dummy();
}
}
static void
cubinit(cubgraph g, cubgraph eno, edgevec v1, edgevec v2, int nv, int ne)
/* initialise edge numbers, etc. */
{
int *gpx,*gpy,*enop,x,y,i,j,n,en;
n = nv;
en = 0;
for (x = 0; x < n; ++x)
{
gpx = g[x];
enop = eno[x];
for (i = 0; i < 3; ++i)
if ((y = gpx[i]) < 0)
enop[i] = ne;
else if (y > x)
{
v1[en] = x;
v2[en] = y;
enop[i] = en++;
}
else
{
gpy = g[y];
for (j = 0; gpy[j] != x; j++)
{}
enop[i] = eno[y][j];
}
}
if (en != ne)
fprintf(stderr,"%% cubinit got en=%d when ne=%d\n",en,ne);
}
static int
propagate(cubgraph g, cubgraph eno, nodedata *ndptr, int *nin, int nv)
/* propagate classifications: */
/* ans = YES, NO or DUNNO */
{
int v,w,i,status;
nodedata *np;
int *gp,*enop,*class,*din,*dout;
status = DUNNO;
np = ndptr;
class = np->class;
din = np->din;
dout = np->dout;
while (status == DUNNO && stackptr > stack)
{
POP(v);
gp = g[v];
enop = eno[v];
if (dout[v] == 0)
{
if (din[v] == 2)
{
if (class[enop[0]] == DUNNO) i = 0;
else if (class[enop[1]] == DUNNO) i = 1;
else i = 2;
w = gp[i];
status = classout(g,np,v,w,enop[i]);
PUSH(w);
}
else if (din[v] == 3)
status = NO;
}
else if (dout[v] == 1)
{
for (i = 0; i < 3; ++i)
if (class[enop[i]] == DUNNO)
{
w = gp[i];
if ((status = classin(g,eno,np,v,w,enop[i],nin,nv))
!= DUNNO)
break;
else
PUSH(w);
}
}
else
status = NO;
}
if (status != NO && *nin == nv) return YES;
else return status;
}
static int
classout(cubgraph g, nodedata *nodat, int v, int w, int en)
/* classify edge en = vw out */
{
nodedata *np;
np = nodat;
++np->dout[v];
++np->dout[w];
np->class[en] = NO;
*classstackptr++ = en;
#if MAXES
if (classstackptr-classstack > maxclassstack)
maxclassstack = classstackptr-classstack;
#endif
return DUNNO;
}
static int
classin(cubgraph g, cubgraph eno, nodedata *nodat,
int v, int w, int en, int *nin, int nv)
/* classify edge en = vw in */
{
nodedata *np;
int *farend,*gp,fv,fw,i;
np = nodat;
++np->din[v];
++np->din[w];
np->class[en] = YES;
*classstackptr++ = en;
#if MAXES
if (classstackptr-classstack > maxclassstack)
maxclassstack = classstackptr-classstack;
#endif
++*nin;
if (*nin == nv)
{
return DUNNO;
}
farend = np->farend;
fv = farend[v];
fw = farend[w];
*classstackptr++ = farend[fv];
*classstackptr++ = -fv-1;
*classstackptr++ = farend[fw];
*classstackptr++ = -fw-1;
#if MAXES
if (classstackptr-classstack > maxclassstack)
maxclassstack = classstackptr-classstack;
#endif
farend[fv] = fw;
farend[fw] = fv;
gp = g[fv];
if (gp[0] == fw) i = 0;
else if (gp[1] == fw) i = 1;
else if (gp[2] == fw) i = 2;
else return DUNNO;
i = eno[fv][i];
if (np->class[i] == DUNNO)
{
PUSH(fv);
PUSH(fw);
if (*nin == nv - 1)
return classin(g,eno,np,fv,fw,i,nin,nv);
else
return classout(g,np,fv,fw,i);
}
return DUNNO;
}
static int
hamnode(cubgraph g, cubgraph eno, edgevec v1, edgevec v2,
nodedata *nodat, int level, int nin, int nv)
/* main node for recursion */
{
int i,p,q,status;
int v,w,en,*gv,*enov;
int *csptr;
#if MAXES
if (level > maxlevel) maxlevel = level;
#endif
if (++nodecount > maxnodes && maxnodes != NO_LIMIT) return HABORT;
status = propagate(g,eno,nodat,&nin,nv);
if (status != DUNNO) return status;
for (v = nv; --v >= 0;)
if (nodat->din[v] == 1) break;
if (v < 0) v = 0;
gv = g[v];
enov = eno[v];
for (i = 0; i < 3; ++i)
{
en = enov[i];
if (nodat->class[en] == DUNNO)
{
w = gv[i];
csptr = classstackptr;
status = classout(g,nodat,v,w,en);
if (status == YES) break;
if (status == NO)
{
while (classstackptr > csptr)
{
p = *--classstackptr;
if (p >= 0)
{
if (nodat->class[p] == YES)
{
--nodat->din[v1[p]];
--nodat->din[v2[p]];
}
else
{
--nodat->dout[v1[p]];
--nodat->dout[v2[p]];
}
nodat->class[p] = DUNNO;
}
else
{
q = *--classstackptr;
nodat->farend[-p-1] = q;
}
}
continue;
}
RESETSTACK;
PUSH(v);
PUSH(w);
status = hamnode(g,eno,v1,v2,nodat,level+1,nin,nv);
if (status == YES) break;
while (classstackptr > csptr)
{
p = *--classstackptr;
if (p >= 0)
{
if (nodat->class[p] == YES)
{
--nodat->din[v1[p]];
--nodat->din[v2[p]];
}
else
{
--nodat->dout[v1[p]];
--nodat->dout[v2[p]];
}
nodat->class[p] = DUNNO;
}
else
{
q = *--classstackptr;
nodat->farend[-p-1] = q;
}
}
if (status == HABORT) return HABORT;
}
}
if (status == DUNNO)
fprintf(stderr,"hamnode returning DUNNO, this can't happen\n");
return status;
}
static int
cubham(cubgraph g, cubgraph eno, edgevec initclass, edgevec v1, edgevec v2,
vertvec cycle, edgevec outclass, int nv, int ne)
/* external interface */
{
int i,j,status,nin,v,w;
for (i = ne; --i >= 0;)
hcnodat.class[i] = DUNNO;
if (3*nv > 2*ne) hcnodat.class[ne] = NO;
for (i = nv; --i >= 0;)
{
hcnodat.din[i] = hcnodat.dout[i] = 0;
hcnodat.farend[i] = i;
onstack[i] = 0;
}
nin = 0;
stacklev = 0;
RESETSTACK;
for (i = nv; --i >= 0;)
{
if (g[i][1] < 0) return NO;
if (g[i][2] < 0)
{
hcnodat.dout[i] = 1;
PUSH(i);
}
}
status = DUNNO;
classstackptr = classstack;
if (initclass)
for (i = 0; i < ne; ++i)
if (initclass[i] != DUNNO)
{
v = v1[i];
w = v2[i];
if (initclass[i] == NO)
{
if (hcnodat.class[i] == YES)
status = NO;
else if (hcnodat.class[i] == DUNNO)
{
if (hcnodat.dout[v] == 0)
{
status = classout(g,&hcnodat,v,w,i);
PUSH(v);
PUSH(w);
}
else
status = NO;
}
}
else if (initclass[i] == YES)
{
if (hcnodat.class[i] == NO)
status = NO;
else if (hcnodat.class[i] == DUNNO)
{
if (hcnodat.din[v] < 2)
{
status = classin(g,eno,&hcnodat,v,w,i,&nin,nv);
PUSH(v);
PUSH(w);
}
else
status = NO;
}
}
if (status != DUNNO) break;
}
if (status == DUNNO)
status = hamnode(g,eno,v1,v2,&hcnodat,0,nin,nv);
if (status == YES && cycle)
{
w = -1;
v = 0;
cycle[0] = 0;
for (i = 1; i < nv; ++i)
{
for (j = 0; g[v][j] == w || hcnodat.class[eno[v][j]] != YES; ++j)
{}
w = v;
v = g[v][j];
cycle[i] = v;
}
}
if (status == YES && outclass)
for (i = 0; i < ne; ++i)
outclass[i] = hcnodat.class[i];
return status;
}
/********************************************************************/
static int
isham(cubgraph cub,
int n, int ne, int weight,
int *vv, int *vi, int nvv,
int *yy, int *yi, int nyy, int *cyc)
/* test if hamiltonian; optionally return a cycle
Forbid the vi[i]-th nbr of vv[i], for i=0..nvv-1
Force the yi[i]-th nbr of yy[i], for i=0..nyy-1
WARNING: vi[i]/yi[i] is numbered starting at 1 */
{
int i,j,k;
int nmax,ch;
cubgraph cubcopy;
edgevec v1,v2,initclass,outclass;
int perm[MAXN],pinv[MAXN];
double tmp;
#if !RANPERM
maxnodes = NO_LIMIT;
nodecount = 0;
cubinit(cub,eno,v1,v2,n,ne);
for (i = 0; i < ne; ++i)
initclass[i] = DUNNO;
for (i = 0; i < nvv; ++i)
if (vv[i] < n) initclass[eno[vv[i]][vi[i]-1]] = NO;
for (i = 0; i < nyy; ++i)
if (yy[i] < n) initclass[eno[yy[i]][yi[i]-1]] = YES;
ch = cubham(cub,eno,initclass,v1,v2,cyc,outclass,n,ne);
totalnodes += nodecount;
++numtries[0];
#else
ch = HABORT;
maxnodes = -1;
for (nmax = 0; ch == HABORT && maxnodes != timeout; ++nmax)
{
if (nmax/repeats < NUMMAXNODES)
{
tmp = (double)standard[nmax/repeats] * (double)weight
* (double)n/ 10000.0;
if (tmp >= (double)NO_LIMIT) maxnodes = NO_LIMIT;
else maxnodes = tmp;
if (timeout > 0 && timeout < maxnodes) maxnodes = timeout;
}
else if (timeout > 0)
maxnodes = timeout;
else
maxnodes = NO_LIMIT;
if (nmax != 0)
{
for (i = n; --i > 0;)
{
k = KRAN(i+1);
j = perm[i];
perm[i] = perm[k];
perm[k] = j;
}
}
else
{
for (i = 0; i < n; ++i)
perm[i] = i;
}
for (i = 0; i < n; ++i)
{
j = perm[i];
cubcopy[j][0] = cub[i][0] < 0 ? -1 : perm[cub[i][0]];
cubcopy[j][1] = cub[i][1] < 0 ? -1 : perm[cub[i][1]];
cubcopy[j][2] = cub[i][2] < 0 ? -1 : perm[cub[i][2]];
}
cubinit(cubcopy,eno,v1,v2,n,ne);
nodecount = 0;
for (i = 0; i < ne; ++i)
initclass[i] = DUNNO;
for (i = 0; i < nvv; ++i)
if (vv[i] < n) initclass[eno[perm[vv[i]]][vi[i]-1]] = NO;
for (i = 0; i < nyy; ++i)
if (yy[i] < n) initclass[eno[perm[yy[i]]][yi[i]-1]] = YES;
ch = cubham(cubcopy,eno,initclass,v1,v2,cyc,outclass,n,ne);
totalnodes += nodecount;
++numtries[nmax/repeats];
}
if (cyc != NULL && ch == YES)
{
for (i = 0; i < n; ++i)
pinv[perm[i]] = i;
for (i = 0; i < n; ++i)
cyc[i] = pinv[cyc[i]];
}
#endif
return ch;
}
/**************************************************************************/
static int
optadd(cubgraph cub, int v1, int v2)
/* v1 and v2 must have degree 2 and be distinct.
Add edge v1-v2 if not already present; return index of edge in cub[v1]. */
{
if (cub[v1][0] == v2) return 0;
if (cub[v1][1] == v2) return 1;
cub[v1][2] = v2;
cub[v2][2] = v1;
return 2;
}
/**************************************************************************/
static void
dofragment(nauty_counter id, cubgraph cub, int n, int ne, int weight)
/* Test for coverage by one or two paths between vertices of degree 2 */
{
int i,i1,i2,i3,i4;
int v1,v2,v3,v4,j1,j3;
int deg2[MAXN],ndeg2;
int yy[3],yi[3],newne;
int cyc[MAXN];
int status;
ndeg2 = 0;
for (i = 0; i < n; ++i)
{
if (cub[i][0] < 0 || cub[i][1] < 0)
gt_abort(">E -F forbids degree 0,1\n");
if (cub[i][2] < 0) deg2[ndeg2++] = i;
}
printf("Input " COUNTER_FMT ":",id);
for (i = 0; i < ndeg2; ++i) printf(" %d",deg2[i]);
printf("\n");
printf(" Pairs: ");
for (i1 = 0; i1 < ndeg2; ++i1)
for (i2 = i1+1; i2 < ndeg2; ++i2)
{
v1 = deg2[i1]; v2 = deg2[i2];
j1 = optadd(cub,v1,v2);
yy[0] = v1; yi[0] = j1+1;
newne = ne + (j1==2);
status = isham(cub,n,newne,weight,NULL,NULL,0,yy,yi,1,cyc);
if (status == HABORT)
printf(" T%d-%d",v1,v2);
if (status == NO)
printf(" N%d-%d",v1,v2);
else
{
printf(" Y%d-%d",v1,v2);
if (verbose > 1)
{
printf("[");
for (i = 0; i < n; ++i) printf(" %d",cyc[i]);
printf("]\n");
}
}
cub[v1][2] = cub[v2][2] = -1;
}
printf("\n");
printf(" Quartets: ");
for (i1 = 0; i1 < ndeg2; ++i1)
for (i2 = i1+1; i2 < ndeg2; ++i2)
for (i3 = i1+1; i3 < ndeg2; ++i3)
for (i4 = i3+1; i4 < ndeg2; ++i4)
{
if (i3 == i2 || i4 == i2) continue;
v1 = deg2[i1]; v2 = deg2[i2];
j1 = optadd(cub,v1,v2);
v3 = deg2[i3]; v4 = deg2[i4];
j3 = optadd(cub,v3,v4);
yy[0] = v1; yi[0] = j1+1;
yy[1] = v3; yi[1] = j3+1;
newne = ne + (j1==2) + (j3==2);
status = isham(cub,n,newne,weight,NULL,NULL,0,yy,yi,2,cyc);
if (status == HABORT)
printf(" T%d-%d,%d-%d",v1,v2,v3,v4);
if (status == NO)
printf(" N%d-%d,%d-%d",v1,v2,v3,v4);
else
{
printf(" Y%d-%d,%d-%d",v1,v2,v3,v4);
if (verbose > 1)
{
printf("[");
for (i = 0; i < n; ++i) printf(" %d",cyc[i]);
printf("]\n");
}
}
cub[v1][2] = cub[v2][2] = -1;
cub[v3][2] = cub[v4][2] = -1;
}
printf("\n");
}
/********************************************************************/
static int
hasinout(cubgraph cub,
int n, int ne, int *x0, int *x1, int *y0, int *y1, int limit)
/* test if cub has in-out (+-) property */
{
edgevec v1,v2,initclass,outclass;
set *d0,*di,*dii;
int i,ii,j,jj;
int me,nbad;
DYNALLSTAT(graph,done,done_sz);
me = (ne + WORDSIZE - 1) / WORDSIZE;
DYNALLOC2(graph,done,done_sz,ne,me,"hasinout");
d0 = (set*)done;
EMPTYSET(d0,me);
for (j = 0; j < ne; ++j)
ADDELEMENT(d0,j);
for (i = 1, di = d0 + me; i < ne; ++i, di += me)
{
for (j = 0; j < me; ++j)
di[j] = d0[j];
DELELEMENT(di,i);
}
DELELEMENT(d0,0);
cubinit(cub,eno,v1,v2,n,ne);
for (i = 0; i < ne; ++i)
initclass[i] = DUNNO;
maxnodes = NO_LIMIT;
nbad = 0;
for (i = 0, di = (set*)done; i < ne; ++i, di += me)
for (j = -1; (j = nextelement(di,me,j)) >= 0;)
{
initclass[i] = NO;
initclass[j] = YES;
++numtries[0];
if (cubham(cub,eno,initclass,v1,v2,NULL,
outclass,n,ne) == NO)
{
x0[nbad] = v1[i]; x1[nbad] = v2[i];
y0[nbad] = v1[j]; y1[nbad] = v2[j];
++nbad;
if (nbad >= limit) return nbad;
}
else
{
for (ii = i, dii = di; ii < ne; ++ii, dii += me)
if (outclass[ii] == NO)
for (jj = 0; jj < ne; ++jj)
if (outclass[jj] == YES)
DELELEMENT(dii,jj);
}
initclass[i] = DUNNO;
initclass[j] = DUNNO;
}
return nbad;
}
/**************************************************************************/
static int
hasinin(cubgraph cub,
int n, int ne, int *x0, int *x1, int *y0, int *y1, int limit)
/* test if cub has in-in (++) property */
{
edgevec v1,v2,initclass,outclass;
set *d0,*di,*dii;
int i,ii,j,jj;
int me,nbad;
DYNALLSTAT(graph,done,done_sz);
me = (ne + WORDSIZE - 1) / WORDSIZE;
DYNALLOC2(graph,done,done_sz,ne,me,"hasinin");
d0 = (set*)done;
EMPTYSET(d0,me);
for (j = 0; j < ne; ++j)
ADDELEMENT(d0,j);
for (i = 1, di = d0 + me; i < ne; ++i, di += me)
{
for (j = 0; j < me; ++j)
di[j] = d0[j];
DELELEMENT(di,i);
}
DELELEMENT(d0,0);
cubinit(cub,eno,v1,v2,n,ne);
for (i = 0; i < ne; ++i)
initclass[i] = DUNNO;
maxnodes = NO_LIMIT;
nbad = 0;
for (i = 0, di = (set*)done; i < ne; ++i, di += me)
for (j = i; (j = nextelement(di,me,j)) >= 0;)
{
initclass[i] = YES;
initclass[j] = YES;
++numtries[0];
if (cubham(cub,eno,initclass,v1,v2,NULL,outclass,n,ne) == NO)
{
x0[nbad] = v1[i]; x1[nbad] = v2[i];
y0[nbad] = v1[j]; y1[nbad] = v2[j];
++nbad;
if (nbad >= limit) return nbad;
}
else
{
for (ii = i, dii = di; ii < ne; ++ii, dii += me)
if (outclass[ii] == YES)
for (jj = ii; jj < ne; ++jj)
if (outclass[jj] == YES)
DELELEMENT(dii,jj);
}
initclass[i] = DUNNO;
initclass[j] = DUNNO;
}
return nbad;
}
/**************************************************************************/
static int
hasin(cubgraph cub, int n, int ne, int *x0, int *x1, int limit)
/* test if cub has "in" property */
{
edgevec v1,v2,initclass,outclass;
booleann done[MAXNE];
int i,ii;
int nbad;
cubinit(cub,eno,v1,v2,n,ne);
for (i = 0; i < ne; ++i)
{
initclass[i] = DUNNO;