summaryrefslogtreecommitdiff
path: root/nauty/geng.c
blob: 8ebd629810a3703b693d1109f257fa8be420cf69 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
/* TODO:
 *        add complements for ordinary graphs
 *        add 5-cycle rejection
 */

/* geng.c  version 3.6; B D McKay, October 2022. */

#define USAGE \
"geng [-cCmtfkbd#D#] [-uygsnh] [-lvq] \n\
              [-x#X#] n [mine[:maxe]] [res/mod] [file]"

#define HELPTEXT \
" Generate all graphs of a specified class.\n\
\n\
      n    : the number of vertices\n\
 mine:maxe : a range for the number of edges\n\
              #:0 means '# or more' except in the case 0:0\n\
   res/mod : only generate subset res out of subsets 0..mod-1\n\
\n\
     -c    : only write connected graphs\n\
     -C    : only write biconnected graphs\n\
     -t    : only generate triangle-free graphs\n\
     -f    : only generate 4-cycle-free graphs\n\
     -k    : only generate K4-free graphs\n\
     -T    : only generate chordal graphs\n\
     -S    : only generate split graphs\n\
     -P    : only generate perfect graphs\n\
     -F    : only generate claw-free graphs\n\
     -b    : only generate bipartite graphs\n\
                (-t, -f and -b can be used in any combination)\n\
     -m    : save memory at the expense of time (only makes a\n\
                difference in the absence of -b, -t, -f and n <= 28).\n\
     -d#   : a lower bound for the minimum degree\n\
     -D#   : an upper bound for the maximum degree\n\
     -v    : display counts by number of edges\n\
     -l    : canonically label output graphs\n\
\n\
     -u    : do not output any graphs, just generate and count them\n\
     -g    : use graph6 output (default)\n\
     -s    : use sparse6 output\n\
     -h    : for graph6 or sparse6 format, write a header too\n\
\n\
     -q    : suppress auxiliary output (except from -v)\n\
\n\
  See program text for much more information.\n"


/*  Parameters:

             n    = the number of vertices (1..MAXN)
                        Note that MAXN is limited to min(WORDSIZE,64)
             mine = the minimum number of edges (no bounds if missing)
             maxe = the maximum number of edges (same as mine if missing)
                    0 means "infinity" except in the case "0-0"
             mod, res = a way to restrict the output to a subset.
                        All the graphs in G(n,mine..maxe) are divided into
                        disjoint classes C(0,mod),C(1,mod),...,C(mod-1,mod),
                        of very approximately equal size.
                        Only the class C(res,mod) is written.

                        If the -x or -X switch is used, they must have the 
                        same value for different values of res; otherwise 
                        the partitioning may not be valid.  In this case
                        (-x,-X with constant value), the usual relationships 
                        between modulo classes are obeyed; for example 
                        C(3,4) = C(3,8) union C(7,8).  This is not true
                        if 3/8 and 7/8 are done with -x or -X values
                        different from those used for 3/4.

             file = a name for the output file (stdout if missing or "-")

             All switches can be concatenated or separate.  However, the
             value of -d must be attached to the "d", and similarly for "x".

             -c    : only write connected graphs
             -C    : only write biconnected graphs
             -t    : only generate triangle-free graphs
             -f    : only generate 4-cycle-free graphs
             -b    : only generate bipartite graphs
                        (-t, -f and -b can be used in any combination)
             -m    : save memory at expense of time (only makes a
                        difference in the absence of -b, -t, -f and n <= 30).
             -D<int> : specify an upper bound for the maximum degree.
                     The value of the upper bound must be adjacent to
                     the "D".  Example: -D6
             -d<int> : specify a lower bound for the minimum degree.
                     The value of the upper bound must be adjacent to
                     the "d".  Example: -d6
             -v    : display counts by number of edges
             -l    : canonically label output graphs

             -u    : do not output any graphs, just generate and count them
             -g    : use graph6 output (default)
             -s    : use sparse6 output
             -n    : use nauty format instead of graph6 format for output
             -y    : use the obsolete y-format for output
             -h    : for graph6 or sparse6 format, write a header too

             -q    : suppress auxiliary output (except from -v)

             -x<int> : specify a parameter that determines how evenly
                     the res/mod facility splits the graphs into subsets.
                     High values mean more even splitting at slight cost
                     to the total time.  The default is 20*mod, and the
                     the legal minimum is 3*mod.  More information is given 
                     under "res/mod" above.
             -X<lev> : move the initial splitting level higher by <lev>,
                     in order to force more even splitting at the cost
                     of speed.  Default is -X0.  More information is given
                     under "res/mod" above.

Output formats.

  The output format is determined by the mutually exclusive switches
  -u, -n, -y, -g and -s.  The default is -g.

  -u suppresses output of graphs completely.

  -s and -g specify sparse6 and graph6 format, defined elsewhere.
  In this case a header is also written if -h is present.

  If -y is present, graphs will be written in y-format.
  y-format is obsolete and only provided for backwards compatibility.

    Each graph occupies one line with a terminating newline.
    Except for the newline, each byte has the format  01xxxxxx, where
    each "x" represents one bit of data.
    First byte:  xxxxxx is the number of vertices n
    Other ceiling(n(n-1)/12) bytes:  These contain the upper triangle of
    the adjacency matrix in column major order.  That is, the entries
    appear in the order (0,1),(0,2),(1,2),(0,3),(1,3),(2,3),(0,4),... .
    The bits are used in left to right order within each byte.
    Any unused bits on the end are set to zero.

  If -n is present, any output graphs are written in nauty format.

    For a graph of n vertices, the output consists of one int giving
    the number of vertices, and n setwords containing the adjacency
    matrix.  Note that this is system dependent (i.e. don't use it).
    It will not work properly if the output is to stdout and your
    system distinguishes binary and text files.

OUTPROC feature.

   By defining the C preprocessor variable OUTPROC at compile time
   (for Unix the syntax is -DOUTPROC=procname on the cc command),
   geng can be made to call a procedure of your manufacture with
   each output graph instead of writing anything. Your procedure
   needs to have type void and the argument list (FILE *f, graph
   *g, int n). f is a stream open for writing, g is the graph in
   nauty format, and n is the number of vertices. Your procedure
   can be in a separate file so long as it is linked with geng. The
   global variables sparse6, graph6, quiet, nooutput, nautyformat,
   and canonise (all type boolean) can be used to test
   for the presence of the flags -s, -g, -q, -u, -n, and -l,
   respectively. If -l is present, the group size and similar
   details can be found in the global variable nauty_stats.

PRUNE feature.

   By defining the C preprocessor variable PRUNE at compile time, geng
   can be made to call
        int PRUNE(graph *g,int n,int maxn) 
   for each intermediate (and final) graph, and reject it if 
   the value returned is nonzero.  The arguments are:

     g      = the graph in nauty format (m=1)
     n      = the number of vertices in g
     maxn   = the number of vertices for output 
              (the value you gave on the command line to geng)

   geng constructs the graph starting with vertex 0, then adding
   vertices 1,2,3,... in that order.  Each graph in the sequence is
   an induced subgraph of all later graphs in the sequence.

   A call is made for all orders from 1 to maxn.  In testing for
   a uniform property (such as a forbidden subgraph or forbidden
   induced subgraph) it might save time to notice that a call to
   PRUNE for n implies that the call for n-1 already passed. 

   For very fast tests, it might be worthwhile using PREPRUNE as
   well or instead. It has the same meaning but is applied earlier
   and more often.

   If -c or -C is given, the connectivity test is done before
   PRUNE but not necessarily before PREPRUNE.

   Some parameters are available in global variables:
   geng_mindeg, geng_maxdeg, geng_mine, geng_maxe, geng_connec;
  
SUMMARY

   If the C preprocessor variable SUMMARY is defined at compile time, the
   procedure SUMMARY(nauty_counter nout, double cpu) is called just before
   the program exits.  The purpose is to allow reporting of statistics
   collected by PRUNE or OUTPROC.  The values nout and cpu are the output
   count and cpu time reported on the >Z line.
   Output should be written to stderr.

INSTRUMENT feature.

   If the C preprocessor variable INSTRUMENT is defined at compile time,
   extra code is inserted to collect statistics during execution, and
   more information is written to stderr at termination.

CALLING FROM A PROGRAM

   It is possible to call geng from another program instead of using it
   as a stand-alone program.  The main requirement is to change the name
   of the main program to be other than "main".  This is done by defining
   the preprocessor variable GENG_MAIN.  You might also like to define
   OUTPROC to be the name of a procedure to receive the graphs. To call
   the program you need to define an argument list argv[] consistent with
   the usual one; don't forget that argv[0] is the command name and not
   the first argument.  The value of argc is the number of strings in
   argv[]; that is, one more than the number of arguments.  See the
   sample program callgeng.c.

   You can also call geng from multiple threads at once, see the sample
   program callgeng2.c.

**************************************************************************

Counts and sample performance statistics.

    Here we give some graph counts and approximate execution times
    on a Linux computer with Intel Core i7-4790 nominally 3.6GHz,
    compiled with gcc 6.2.0.
    Times are with the -u option (generate but don't write); add
    0.2-0.3 microseconds per graph for output to a file.


      General Graphs                     C3-free Graphs (-t)

      1              1                    1              1
      2              2                    2              2
      3              4                    3              3
      4             11                    4              7
      5             34                    5             14
      6            156                    6             38
      7           1044                    7            107
      8          12346                    8            410
      9         274668   0.08 sec         9           1897
     10       12005168   2.7 sec         10          12172  
     11     1018997864   207 sec         11         105071   0.09 sec
     12   165091172592   9 hr            12        1262180   0.8 sec
     13 50502031367952   108 days        13       20797002   11 sec
    These can be done in about half      14      467871369   220 sec
    the time by setting the edge limit   15    14232552452   1.7 hr
    half way then adding complements.    16   581460254001   65 hr
                                         17 31720840164950   145 days


     C4-free Graphs  (-f)              (C3,C4)-free Graphs (-tf)

      1             1                      1             1
      2             2                      2             2 
      3             4                      3             3 
      4             8                      4             6
      5            18                      5            11
      6            44                      6            23
      7           117                      7            48 
      8           351                      8           114 
      9          1230                      9           293
     10          5069                     10           869
     11         25181   0.04 sec          11          2963 
     12        152045   0.17 sec          12         12066   0.03 sec
     13       1116403   1.0 sec           13         58933   0.10 sec
     14       9899865   7.5 sec           14        347498   0.5 sec
     15     104980369   71 sec            15       2455693   2.7 sec
     16    1318017549   14 min            16      20592932   19 sec
     17   19427531763   3.4 hr            17     202724920   170 sec
     18  333964672216   56 hr             18    2322206466   32 min
     19 6660282066936   45 days           19   30743624324   7 hr
                                          20  468026657815   4 days
                                          21 8161170076257   106 days


     Bipartite Graphs (-b)            C4-free Bipartite Graphs (-bf)

      1              1                      1             1
      2              2                      2             2
      3              3                      3             3
      4              7                      4             6
      5             13                      5            10
      6             35                      6            21
      7             88                      7            39
      8            303                      8            86
      9           1119                      9           182
     10           5479                     10           440
     11          32303   0.03 sec          11          1074
     12         251135   2.3 sec           12          2941
     13        2527712   1.7 sec           13          8424   0.04 sec
     14       33985853   19 sec            14         26720   0.11 sec
     15      611846940   4.9 min           15         90883   0.33 sec
     16    14864650924   1.8 hr            16        340253   1.1 sec
     17   488222721992   2.4 days          17       1384567   3.7 sec
     18 21712049275198   105 days          18       6186907   14 sec
                                           19      30219769   59 sec
                                           20     161763233   280 sec
                                           21     946742190   24 min
                                           22    6054606722   2.5 hr
                                           23   42229136988   17 hr
                                           24  320741332093   125 hr
                                           25 2648348712904   58 days

If you know any more of these counts, please tell me.

**************************************************************************

Hints:

To make all the graphs of order n, without restriction on type,
it is fastest to make them up to binomial(n,2)/2 edges and append
the complement of those with strictly less than binomial(n,2)/2 edges.

If it is necessary to split the computation into pieces, it is more
efficient to use the res/mod feature than to split by numbers of edges.

**************************************************************************

    Author:   B. D. McKay, Sep 1991 and many later dates.
              Copyright  B. McKay (1991-2018).  All rights reserved.
              This software is subject to the conditions and waivers
              detailed in the file nauty.h.

    Changes:  Nov 18, 1991 : added -d switch
                             fixed operation for n=16
              Nov 26, 1991 : added OUTPROC feature
              Nov 29, 1991 : -c implies mine >= n-1
              Jan  8, 1992 : make writeny() not static
              Jan 10, 1992 : added -n switch
              Feb  9, 1992 : fixed case of n=1
              Feb 16, 1992 : changed mine,maxe,maxdeg testing
              Feb 19, 1992 : added -b, -t and -u options
                             documented OUTPROC and added external
                                 declaration for it.
              Feb 20, 1992 : added -v option
              Feb 22, 1992 : added INSTRUMENT compile-time option
              Feb 23, 1992 : added xbnds() for more effective pruning
              Feb 24, 1992 : added -l option
              Feb 25, 1992 : changed writenauty() to use fwrite()
              Mar 11, 1992 : completely revised many parts, incl
                             new refinement procedure for fast rejection,
                             distance invariant for regular graphs
              May 19, 1992 : modified userautomproc slightly.  xorb[]
                             is no longer idempotent but it doesn't matter.
                             Speed-up of 2-5% achieved.
              June 5, 1993 : removed ";" after "CPUDEFS" to avoid illegal
                             empty declaration.
              Nov 24, 1994 : tested for 0 <= res < mod

              Apr 13, 1996 : Major overhaul.  Renamed "geng".
                             Changed argument syntax.
                             Removed 16-vertex limit.
                             Added -s, -m, -x.  Allowed combinations.
                             Replaced code for non-general graphs.
                             Very many small changes.
              Jul 12, 1996 : Changed semantics of -x and res/mod.
                             Changed >A line and added fflush()/
                             All switches can be concatenated or not.
              Aug 16, 1996 : Added -X switch and PRUNE() feature.
                             Fixed case of argument 0-0.
              Sep 22, 1996 : Improved 1-2% by tweaking refinex().
              Jan 21, 1997 : Renamed to geng.  
                             Changed -s to -f, and added -sghq.
              Sep  7, 1997 : Fixed WORDSIZE=16 problems.
              Sep 22, 1997 : Use "wb" open for nautyformat.
              Jan 26, 1998 : Added SUMMARY feature.
              Mar  4, 1998 : Added -C.
              Mar 12, 1998 : Moved stats to nauty_stats.
              Jan  1, 2000 : Changed -d to -D and added -d.
              Feb 24, 2000 : Raised limit to 32 vertices.
              Mar  3, 2000 : Made some counts into unsigned long.
                             (Includes first arg to SUMMARY.)
              Mar 12, 2000 : Used bigint for counts that may exceed 2^32.
                             Now all counts from very long runs are ok.
              Oct 12, 2000 : Changed maxef[32] to 92 after confirmation
                             from Yang Yuansheng.  The old value of 93 was
                             valid but 92 is slightly more efficient.
              Nov 16, 2000 : Used fuction prototypes.
              Jul 31, 2001 : Added PREPRUNE
               May 7, 2004 : Complete all function prototypes
              Nov 24, 2004 : Force -m for very large sizes
                             Add -bf automatically if generating trees
               Apr 1, 2007 : Write >A in one fputs() to try to reduce
                             mixing of outputs in multi-process pipes.
              Sep 19, 2007 : Force -m for n > 28 regardless of word size.
              Nov 29, 2008 : Slightly improved connectivity testing.
              Mar 3,  2015 : Improve maxdeg tweaking.
              Jan 18, 2016 : Replace bigint by nauty_counter.
               Mar 8, 2018 : Can now compile for MAXN up to WORDSIZE.
                             Use setword instead of unsigned for xword.
                             Revised splitting level.
                             Updated sample execution times.
              Mar 10, 2018 : Fix overflow at impossibly large n, maxdeg.
              Jan 14, 2019 : Define geng_mindeg, geng_maxdeg, geng_mine, geng_maxe.
               Jun 1, 2021 : Define geng_connec.
               Jun 4, 2021 : Improve performance for -c and -C with small edge count.
              Jun 21, 2021 : K1 is not 2-connected.
              May 15, 2022 : findmax() now deposits -1 at the end of the extended
                              sequence in case geng is being called as a function.
              Oct 10, 2022 : Obsolete y-format removed

**************************************************************************/

#define NAUTY_PGM  1   /* 1 = geng, 2 = genbg, 3 = gentourng, 4 = gentreeg */

#ifndef MAXN
#define MAXN WORDSIZE         /* not more than WORDSIZE */
#endif

#if MAXN > WORDSIZE || MAXN > 64
 #error "Can't have MAXN greater than min(64,WORDSIZE)"
#endif

#define ONE_WORD_SETS
#include "gtools.h"   /* which includes nauty.h and stdio.h */

int generate_done;

/* No need for TLS if not calling from a program. */
#ifndef GENG_MAIN
#undef TLS_ATTR
#define TLS_ATTR
#endif

typedef setword xword;

static TLS_ATTR void (*outproc)(FILE*,graph*,int);

static TLS_ATTR FILE *outfile;           /* file for output graphs */
static TLS_ATTR int connec;              /* 1 for -c, 2 for -C, 0 for neither */
static TLS_ATTR boolean bipartite;       /* presence of -b */
static TLS_ATTR boolean trianglefree;    /* presence of -t */
static TLS_ATTR boolean squarefree;      /* presence of -f */
static TLS_ATTR boolean k4free;          /* presence of -k */
static TLS_ATTR boolean splitgraph;      /* presence of -S */
static TLS_ATTR boolean chordal;         /* presence of -T */
static TLS_ATTR boolean perfect;         /* presence of -P */
static TLS_ATTR boolean clawfree;        /* presence of -F */
static TLS_ATTR boolean savemem;         /* presence of -m */
static TLS_ATTR boolean verbose;         /* presence of -v */
boolean TLS_ATTR nautyformat;            /* presence of -n */
boolean TLS_ATTR graph6;                 /* presence of -g */
boolean TLS_ATTR sparse6;                /* presence of -s */
boolean TLS_ATTR nooutput;               /* presence of -u */
boolean TLS_ATTR canonise;               /* presence of -l */
boolean TLS_ATTR quiet;                  /* presence of -q */
boolean TLS_ATTR header;                 /* presence of -h */
statsblk TLS_ATTR nauty_stats;
static TLS_ATTR int mindeg,maxdeg,maxn,mine,maxe,mod,res;
#define PRUNEMULT 50   /* bigger -> more even split at greater cost */
static TLS_ATTR int min_splitlevel,odometer,splitlevel,multiplicity;
static TLS_ATTR graph gcan[MAXN];

#define XBIT(i) ((xword)1 << (i))
#define XPOPCOUNT(x) POPCOUNT(x)
#define XNEXTBIT(x) (WORDSIZE-1-FIRSTBITNZ(x))   /* Assumes non-zero */

typedef struct
{
    int ne,dmax;          /* values used for xlb,xub calculation */
    int xlb,xub;          /* saved bounds on extension degree */
    xword lo,hi;          /* work purposes for orbit calculation */
    xword xstart[MAXN+1]; /* index into xset[] for each cardinality */
    xword *xset;          /* array of all x-sets in card order */
    xword *xcard;         /* cardinalities of all x-sets */
    xword *xinv;          /* map from x-set to index in xset */
    xword *xorb;          /* min orbit representative */
    xword *xx;            /* (-b, -t, -s, -m) candidate x-sets */
                      /*   note: can be the same as xcard */
    xword xlim;           /* number of x-sets in xx[] */
} leveldata;

static TLS_ATTR leveldata data[MAXN];      /* data[n] is data for n -> n+1 */
static TLS_ATTR nauty_counter ecount[1+MAXN*(MAXN-1)/2];  /* counts by number of edges */
static TLS_ATTR nauty_counter nodes[MAXN];     /* nodes at each level */

#ifdef INSTRUMENT
static TLS_ATTR nauty_counter rigidnodes[MAXN],fertilenodes[MAXN];
static TLS_ATTR nauty_counter a1calls,a1nauty,a1succs;
static TLS_ATTR nauty_counter a2calls,a2nauty,a2uniq,a2succs;
#endif

/* The numbers below are actual maximum edge counts.
   geng works correctly with any upper bounds.
   To extend known upper bounds upwards:
       (n-1, E) -> (n, E + floor(2*E/(n-2))),
   which is done by the procedure findmaxe().
*/

static TLS_ATTR int maxeb[65] =     /* max edges for -b */
 {0,0,1,2,4, -1};
static TLS_ATTR int maxet[65] =     /* max edges for -t */
 {0,0,1,2,4, -1};
static TLS_ATTR int maxef[65] =     /* max edges for -f */
 {0,0,1,3,4, 6,7,9,11,13,
  16,18,21,24,27, 30,33,36,39,42,
  46,50,52,56,59, 63,67,71,76,80,
  85,90,92,96,102, 106,110,113,117,122,
  127, -1};
static TLS_ATTR int maxeft[65] =    /* max edges for -ft */
 {0,0,1,2,3, 5,6,8,10,12,
  15,16,18,21,23, 26,28,31,34,38,
  41,44,47,50,54, 57,61,65,68,72,
  76,80,85,87,90, 95,99,104,109,114,
  120,124,129,134,139, 145,150,156,162,168,
  175,176,178, -1};
static TLS_ATTR int maxebf[65] =    /* max edges for -bf */
  {0,0,1,2,3, 4,6,7,9,10,
  12,14,16,18,21, 22,24,26,29,31,
  34,36,39,42,45, 48,52,53,56,58,
  61,64,67,70,74, 77,81,84,88,92,
  96,100,105,106,108, 110,115,118,122,126,
  130,134,138,142,147, 151,156,160,165,170,
  175,180,186,187, -1};

#ifdef PLUGIN
#include PLUGIN
#endif

#ifdef OUTPROC
extern void OUTPROC(FILE*,graph*,int);
#endif
#ifdef PRUNE
extern int PRUNE(graph*,int,int);
#endif
#ifdef PREPRUNE
extern int PREPRUNE(graph*,int,int);
#endif
#ifdef SUMMARY
extern void SUMMARY(nauty_counter,double);
#endif

#if defined(PRUNE) || defined(PREPRUNE)
int TLS_ATTR geng_mindeg, geng_maxdeg, geng_mine, geng_maxe, geng_connec;
#endif

/************************************************************************/

#define EXTEND(table,n) ((n) <= 1 ? 0 : (n) == 2 ? 1 : \
     table[(n)-1] + (2*table[(n)-1]/((n)-2)))

static int
findmaxe(int *table, int n)
/* Extend table to MAXN vertices if necessary, and return table[n]. */
{
    int i;

    for (i = 0; i <= MAXN && table[i] >= 0; ++i) {}
    for ( ; i <= MAXN; ++i) table[i] = EXTEND(table,i);

    return table[n];
}

/************************************************************************/

void
writeg6x(FILE *f, graph *g, int n)
/* write graph g (n vertices) to file f in graph6 format */
{
    writeg6(f,g,1,n);
}

/************************************************************************/

void
writes6x(FILE *f, graph *g, int n)
/* write graph g (n vertices) to file f in sparse6 format */
{
    writes6(f,g,1,n);
}

/***********************************************************************/

static void
nullwrite(FILE *f, graph *g, int n)
/* don't write graph g (n vertices) to file f */
{
}

/***********************************************************************/

void
writenauty(FILE *f, graph *g, int n)
/* write graph g (n vertices) to file f in nauty format.
   Each graph is preceded by the number of vertices. */
{
    int nn;

    nn = n;

    if (fwrite((char *)&nn,sizeof(int),(size_t)1,f) != 1 ||
          fwrite((char*)g,sizeof(graph),(size_t)n,f) != n)
    {
        fprintf(stderr,">E writenauty : error on writing file\n");
        exit(2);
    }
}

/*********************************************************************/

static boolean
isconnected(graph *g, int n)
/* test if g is connected */
{
    setword seen,expanded,toexpand,allbits;
    int i;

    allbits = ALLMASK(n);

    expanded = bit[n-1];
    seen = expanded | g[n-1];

    while (seen != allbits && (toexpand = (seen & ~expanded))) /* not == */
    {
        i = FIRSTBITNZ(toexpand);
        expanded |= bit[i];
        seen |= g[i];
    }

    return  seen == allbits;
}

static boolean
connpreprune(graph *g, int n, int maxn)
/* This function speeds up the generation of connected graphs
   with not many edges. */
{
    setword notvisited,queue;
    int ne,nc,i;

    if (n == maxn || maxe - maxn >= 5) return 0;

    ne = 0;
    for (i = 0; i < n; ++i) ne += POPCOUNT(g[i]);
    ne /= 2;

    nc = 0;
    notvisited = ALLMASK(n);

    while (notvisited)
    {
        ++nc;
        queue = SWHIBIT(notvisited);
        notvisited &= ~queue;
        while (queue)
        {
            TAKEBIT(i,queue);
            notvisited &= ~bit[i];
            queue |= g[i] & notvisited;
        }
    }

    if (ne - n + nc > maxe - maxn + 1) return TRUE;

    return FALSE;
}

/**********************************************************************/
 
static boolean
isbiconnected(graph *g, int n)
/* test if g is biconnected */
{
    int sp,v,w;
    setword sw;
    setword visited;
    int numvis,num[MAXN],lp[MAXN],stack[MAXN];
 
    if (n <= 2) return FALSE;
 
    visited = bit[0];
    stack[0] = 0;
    num[0] = 0;
    lp[0] = 0;
    numvis = 1;
    sp = 0;
    v = 0;
 
    for (;;)
    {
        if ((sw = g[v] & ~visited))           /* not "==" */
        {
            w = v;
            v = FIRSTBITNZ(sw);       /* visit next child */
            stack[++sp] = v;
            visited |= bit[v];
            lp[v] = num[v] = numvis++;
            sw = g[v] & visited & ~bit[w];
            while (sw)
            {
                w = FIRSTBITNZ(sw);
                sw &= ~bit[w];
                if (num[w] < lp[v])  lp[v] = num[w];
            }
        }
        else
        {
            w = v;                  /* back up to parent */
            if (sp <= 1)          return numvis == n;
            v = stack[--sp];
            if (lp[w] >= num[v])  return FALSE;
            if (lp[w] < lp[v])    lp[v] = lp[w];
        }
    }
}

/**********************************************************************/

static void
gcomplement(graph *g, graph *gc, int n)
/* Take the complement of g and put it in gc */
{
    int i;
    setword all;

    all = ~(setword)BITMASK(n-1);
    for (i = 0; i < n; ++i)
        gc[i] = g[i] ^ all ^ bit[i];
}

/**********************************************************************/

static boolean
distinvar(graph *g, int *invar, int n)
/* make distance invariant
   return FALSE if n-1 not maximal else return TRUE */
{
    int w;
    setword workset,frontier;
    setword sofar;
    int inv,d,v;

    for (v = n-1; v >= 0; --v)
    {
        inv = 0;
        sofar = frontier = bit[v];
        for (d = 1; frontier != 0; ++d)
        {
            workset = 0;
            inv += POPCOUNT(frontier) ^ (0x57 + d);
            while (frontier)
            {
                w = FIRSTBITNZ(frontier);
                frontier ^= bit[w];
                workset |= g[w];
            }
            frontier = workset & ~sofar;
            sofar |= frontier;
        }
        invar[v] = inv;
        if (v < n-1 && inv > invar[n-1]) return FALSE;
    }
    return TRUE;
}

/**************************************************************************/

static void
makexgraph(graph *g, xword *h, int n)
/* make x-format graph from nauty format graph */
{
    setword gi;
    int i,j;
    xword hi;

    for (i = 0; i < n; ++i)
    {
        hi = 0;
        gi = g[i];
        while (gi)
        {
            j = FIRSTBITNZ(gi);
            gi ^= bit[j];
            hi |= XBIT(j);
        }
        h[i] = hi;
    }
}

/**************************************************************************/

static void
make0graph(graph *g, xword *h, int n)
/* make x-format graph without edges */
{
    int i;

    for (i = 0; i < n; ++i) h[i] = 0;
}

/**************************************************************************/

static void
makebgraph(graph *g, xword *h, int n)
/* make x-format graph of different colour graph */
{
    setword seen1,seen2,expanded,w;
    setword restv;
    xword xseen1,xseen2;
    int i;

    restv = 0;
    for (i = 0; i < n; ++i) restv |= bit[i];

    seen1 = seen2 = 0;
    expanded = 0;

    while (TRUE)
    {
        if ((w = ((seen1 | seen2) & ~expanded)) == 0)
        {
            xseen1 = 0;
            w = seen1;
            while (w)
            {
                i = FIRSTBITNZ(w);
                w ^= bit[i];
                xseen1 |= XBIT(i);
            }
            xseen2 = 0;
            w = seen2;
            while (w)
            {
                i = FIRSTBITNZ(w);
                w ^= bit[i];
                xseen2 |= XBIT(i);
            }

            w = seen1;
            while (w)
            {
                i = FIRSTBITNZ(w);
                w ^= bit[i];
                h[i] = xseen2;
            }
            w = seen2;
            while (w)
            {
                i = FIRSTBITNZ(w);
                w ^= bit[i];
                h[i] = xseen1;
            }

            restv &= ~(seen1 | seen2);
            if (restv == 0) return;
            i = FIRSTBITNZ(restv);
            seen1 = bit[i];
            seen2 = 0;
        }
        else
            i = FIRSTBITNZ(w);

        expanded |= bit[i];
        if (bit[i] & seen1) seen2 |= g[i];
        else                seen1 |= g[i];
    }
}

/**************************************************************************/
 
static void
makeb6graph(graph *g, xword *h, int n)
/* make x-format bipartite girth 6 graph */
{
    setword w,x;
    xword hi;
    int i,j;

    makebgraph(g,h,n);

    for (i = 0; i < n; ++i)
    {
        w = g[i];
        x = 0;
        while (w)
        {
            j = FIRSTBITNZ(w);
            w ^= bit[j];
            x |= g[j];
        }
        x &= ~bit[i];
        hi = h[i];
        while (x)
        {
            j = FIRSTBITNZ(x);
            x ^= bit[j];
            hi |= XBIT(j);
        }
        h[i] = hi;
    }
}

/**************************************************************************/
 
static void
makesgraph(graph *g, xword *h, int n)
/* make x-format square graph */
{
    setword w,x;
    xword hi;
    int i,j;

    for (i = 0; i < n; ++i)
    {
        w = g[i];
        x = 0;
        while (w)
        {
            j = FIRSTBITNZ(w);
            w ^= bit[j];
            x |= g[j];
        }
        x &= ~bit[i];
        hi = 0;
        while (x)
        {
            j = FIRSTBITNZ(x);
            x ^= bit[j];
            hi |= XBIT(j);
        }
        h[i] = hi;
    }
}

/**************************************************************************/ 
 
static void 
makeg5graph(graph *g, xword *h, int n)
/* make x-format girth-5 graph */
{
    setword w,x; 
    xword hi;
    int i,j;
 
    for (i = 0; i < n; ++i)
    { 
        w = g[i]; 
        x = g[i];
        while (w) 
        {
            j = FIRSTBITNZ(w);
            w ^= bit[j];
            x |= g[j];
        } 
        x &= ~bit[i]; 
        hi = 0; 
        while (x) 
        { 
            j = FIRSTBITNZ(x); 
            x ^= bit[j]; 
            hi |= XBIT(j); 
        } 
        h[i] = hi; 
    } 
} 

/**************************************************************************/  

static xword
arith(xword a, xword b, xword c)
/* Calculate a*b/c, assuming a*b/c and (c-1)*b are representable integers */
{
    return (a/c)*b + ((a%c)*b)/c;
}

/**************************************************************************/  

static void
makeleveldata(boolean restricted)
/* make the level data for each level */
{
    long h;
    int n,nn;
    xword ncj;
    leveldata *d;
    xword *xcard,*xinv;
    xword *xset,xw,nxsets;
    xword cw;
    xword i,ilast,j;
    size_t tttn;

    for (n = 1; n < maxn; ++n)
    {
        nn = maxdeg <= n ? maxdeg : n;
        ncj = nxsets = 1;
        for (j = 1; j <= nn; ++j)
        { 
            ncj = arith(ncj,n-j+1,j);
            nxsets += ncj;
        }

        d = &data[n];
        d->ne = d->dmax = d->xlb = d->xub = -1;

        if (restricted)
        {
            d->xorb = (xword*) calloc(nxsets,sizeof(xword));
            d->xx = (xword*) calloc(nxsets,sizeof(xword));
            if (d->xorb == NULL || d->xx == NULL)
            {
                fprintf(stderr,
                   ">E geng: calloc failed in makeleveldata()\n");
                exit(2);
            }
            continue;   /* <--- NOTE THIS! */
        }

        tttn = (size_t)1 << n;
        d->xset = xset = (xword*) calloc(nxsets,sizeof(xword));
        d->xcard = xcard = (xword*) calloc(nxsets,sizeof(xword));
        d->xinv = xinv = (xword*) calloc(tttn,sizeof(xword));
        d->xorb = (xword*) calloc(nxsets,sizeof(xword));
        d->xx = d->xcard;

        if (xset==NULL || xcard==NULL || xinv==NULL || d->xorb==NULL)
        {
            fprintf(stderr,">E geng: calloc failed in makeleveldata()\n");
            exit(2);
        }

        j = 0;

        ilast = (n == WORDSIZE ? ~(setword)0 : XBIT(n)-1);
        for (i = 0;; ++i)
        {
            if ((h = XPOPCOUNT(i)) <= maxdeg)
            {
                xset[j] = i;
                xcard[j] = h;
                ++j;
            }
            if (i == ilast) break;
        }

        if (j != nxsets)
        {
            fprintf(stderr,">E geng: j=" SETWORD_DEC_FORMAT 
                               " nxsets=" SETWORD_DEC_FORMAT "\n",
                    j,nxsets);
            exit(2);
        }

        h = 1;
        do
            h = 3 * h + 1;
        while (h < nxsets);

        do     /* Shell sort, consider replacing */
        {
            for (i = h; i < nxsets; ++i)
            {
                xw = xset[i];
                cw = xcard[i];
                for (j = i; xcard[j-h] > cw ||
                            (xcard[j-h] == cw && xset[j-h] > xw); )
                {
                    xset[j] = xset[j-h];
                    xcard[j] = xcard[j-h];
                    if ((j -= h) < h) break;
                }
                xset[j] = xw;
                xcard[j] = cw;
            }
            h /= 3;
        }
        while (h > 0);

        for (i = 0; i < nxsets; ++i) xinv[xset[i]] = i;

        d->xstart[0] = 0;
        for (i = 1; i < nxsets; ++i)
            if (xcard[i] > xcard[i-1]) d->xstart[xcard[i]] = i;
        d->xstart[xcard[nxsets-1]+1] = nxsets;
    }
}

/**************************************************************************/

static void
userautomproc(int count, int *p, int *orbits,
          int numorbits, int stabvertex, int n)
/* form orbits on powerset of VG
   called by nauty;  operates on data[n] */
{
    xword i,j1,j2,moved,pi,pxi;
    xword lo,hi;
    xword *xorb,*xinv,*xset,w;

    xorb = data[n].xorb;
    xset = data[n].xset;
    xinv = data[n].xinv;
    lo = data[n].lo;
    hi = data[n].hi;

    if (count == 1)                         /* first automorphism */
        for (i = lo; i < hi; ++i) xorb[i] = i;

    moved = 0;
    for (i = 0; i < n; ++i)
        if (p[i] != i) moved |= XBIT(i);

    for (i = lo; i < hi; ++i)
    {
        if ((w = xset[i] & moved) == 0) continue;
        pxi = xset[i] & ~moved;
        while (w)
        {
            j1 = XNEXTBIT(w);
            w ^= XBIT(j1);
            pxi |= XBIT(p[j1]);
        }
        pi = xinv[pxi];

        j1 = xorb[i];
        while (xorb[j1] != j1) j1 = xorb[j1];
        j2 = xorb[pi];
        while (xorb[j2] != j2) j2 = xorb[j2];

        if      (j1 < j2) xorb[j2] = xorb[i] = xorb[pi] = j1;
        else if (j1 > j2) xorb[j1] = xorb[i] = xorb[pi] = j2;
    }
}

/**************************************************************************/

static void
userautomprocb(int count, int *p, int *orbits,
          int numorbits, int stabvertex, int n)
/* form orbits on powerset of VG
   called by nauty;  operates on data[n] */
{
    xword j1,j2,moved,pi,pxi,lo,hi,x;
    xword i,*xorb,*xx,w,xlim,xlb;

    xorb = data[n].xorb;
    xx = data[n].xx;
    xlim = data[n].xlim;

    if (count == 1)                         /* first automorphism */
    {
        j1 = 0;
        xlb = data[n].xlb;

        for (i = 0; i < xlim; ++i)
        {
            x = xx[i];
            if (XPOPCOUNT(x) >= xlb)
            {
                xx[j1] = x;
                xorb[j1] = j1;
                ++j1;
            }
        }
        data[n].xlim = xlim = j1;
    }

    moved = 0;
    for (i = 0; i < n; ++i)
        if (p[i] != i) moved |= XBIT(i);

    for (i = 0; i < xlim; ++i)
    {
        if ((w = xx[i] & moved) == 0) continue;
        pxi = xx[i] & ~moved;
        while (w)
        {
            j1 = XNEXTBIT(w);
            w ^= XBIT(j1);
            pxi |= XBIT(p[j1]);
        }
        /* pi = position of pxi */

        lo = 0;
        hi = xlim - 1;

        for (;;)
        {
            pi = (lo + hi) >> 1;
            if (xx[pi] == pxi) break;
            else if (xx[pi] < pxi) lo = pi + 1;
            else                   hi = pi - 1;
        }

        j1 = xorb[i];
        while (xorb[j1] != j1) j1 = xorb[j1];
        j2 = xorb[pi];
        while (xorb[j2] != j2) j2 = xorb[j2];

        if      (j1 < j2) xorb[j2] = xorb[i] = xorb[pi] = j1;
        else if (j1 > j2) xorb[j1] = xorb[i] = xorb[pi] = j2;
    }
}

/*****************************************************************************
*                                                                            *
*  refinex(g,lab,ptn,level,numcells,count,active,goodret,code,m,n) is a      *
*  custom version of refine() which can exit quickly if required.            *
*                                                                            *
*  Only use at level==0.                                                     *
*  goodret : whether to do an early return for code 1                        *
*  code := -1 for n-1 not max, 0 for maybe, 1 for definite                   *
*                                                                            *
*****************************************************************************/

static void
refinex(graph *g, int *lab, int *ptn, int level, int *numcells,
     int *count, set *active, boolean goodret, int *code, int m, int n)
{
    int i,c1,c2,labc1;
    setword x,lact;
    int split1,split2,cell1,cell2;
    int cnt,bmin,bmax;
    set *gptr;
    setword workset;
    int workperm[MAXN];
    int bucket[MAXN+2];

    if (n == 1)
    {
        *code = 1;
        return;
    }

    *code = 0;
    lact = *active;

    while (*numcells < n && lact)
    {
        TAKEBIT(split1,lact);
        
        for (split2 = split1; ptn[split2] > 0; ++split2) {}
        if (split1 == split2)       /* trivial splitting cell */
        {
            gptr = GRAPHROW(g,lab[split1],1);
            for (cell1 = 0; cell1 < n; cell1 = cell2 + 1)
            {
                for (cell2 = cell1; ptn[cell2] > 0; ++cell2) {}
                if (cell1 == cell2) continue;

                c1 = cell1;
                c2 = cell2;
                while (c1 <= c2)
                {
                    labc1 = lab[c1];
                    if (ISELEMENT1(gptr,labc1))
                        ++c1;
                    else
                    {
                        lab[c1] = lab[c2];
                        lab[c2] = labc1;
                        --c2;
                    }
                }
                if (c2 >= cell1 && c1 <= cell2)
                {
                    ptn[c2] = 0;
                    ++*numcells;
                    lact |= bit[c1];
                }
            }
        }

        else        /* nontrivial splitting cell */
        {
            workset = 0;
            for (i = split1; i <= split2; ++i) workset |= bit[lab[i]];

            for (cell1 = 0; cell1 < n; cell1 = cell2 + 1)
            {
                for (cell2 = cell1; ptn[cell2] > 0; ++cell2) {}
                if (cell1 == cell2) continue;
                i = cell1;
                if ((x = workset & g[lab[i]]) != 0) cnt = POPCOUNT(x);
                else                                cnt = 0;
                count[i] = bmin = bmax = cnt;
                bucket[cnt] = 1;
                while (++i <= cell2)
                {
                    if ((x = workset & g[lab[i]]) != 0)
                        cnt = POPCOUNT(x);
                    else
                        cnt = 0;

                    while (bmin > cnt) bucket[--bmin] = 0;
                    while (bmax < cnt) bucket[++bmax] = 0;
                    ++bucket[cnt];
                    count[i] = cnt;
                }
                if (bmin == bmax) continue;
                c1 = cell1;
                for (i = bmin; i <= bmax; ++i)
                    if (bucket[i])
                    {
                        c2 = c1 + bucket[i];
                        bucket[i] = c1;
                        if (c1 != cell1)
                        {
                            lact |= bit[c1];
                            ++*numcells;
                        }
                        if (c2 <= cell2) ptn[c2-1] = 0;
                        c1 = c2;
                    }
                for (i = cell1; i <= cell2; ++i)
                    workperm[bucket[count[i]]++] = lab[i];
                for (i = cell1; i <= cell2; ++i) lab[i] = workperm[i];
            }
        }

        if (ptn[n-2] == 0)
        {
            if (lab[n-1] == n-1)
            {
                *code = 1;
                if (goodret) return;
            }
            else
            {
                *code = -1;
                return;
            }
        }
        else
        {
            i = n - 1;
            while (TRUE)
            {
                if (lab[i] == n-1) break;
                --i;
                if (ptn[i] == 0)
                {
                    *code = -1;
                    return;
                }
            }
        }
    }
}

/**************************************************************************/

static void
makecanon(graph *g, graph *gcan, int n)
/* gcan := canonise(g) */
{
    int lab[MAXN],ptn[MAXN],orbits[MAXN];
    static TLS_ATTR DEFAULTOPTIONS_GRAPH(options);
    setword workspace[50];

    options.getcanon = TRUE;

    nauty(g,lab,ptn,NULL,orbits,&options,&nauty_stats,
          workspace,50,1,n,gcan);
}

/**************************************************************************/

static boolean
hask4(graph *g, int n, int maxn)
/* Return TRUE iff there is a K4 including the last vertex */
{
    setword gx,w;
    int i,j;

    gx = g[n-1];
    while (gx)
    {
        TAKEBIT(i,gx);
        w = g[i] & gx;
        while (w)
        {
            TAKEBIT(j,w);
            if ((g[j] & w)) return TRUE;
        }
    }
    return FALSE;
}

/**************************************************************************/

static boolean
hasclaw(graph *g, int n, int maxn)
/* Return TRUE if there is a claw (induced K(1,3)) involving the last vertex */
{
    int i,j,k;
    setword x,y;

    x = g[n-1];
    while (x)
    {
        TAKEBIT(j,x);
        y = x & ~g[j];
        while (y)
        {
            TAKEBIT(k,y);
            if (y & ~g[k]) return TRUE;
        }
    }

    x = g[n-1];
    while (x)
    {
        TAKEBIT(i,x);
        y = g[i] & ~(bit[n-1]|g[n-1]);
        while (y)
        {
            TAKEBIT(k,y);
            if (y & ~g[k]) return TRUE;
        }
    }

    return FALSE;
}

static boolean
hasinducedpath(graph *g, int start, setword body, setword last)
/* return TRUE if there is an induced path in g starting at start,
   extravertices within body and ending in last.
 * {start}, body and last should be disjoint. */
{
    setword gs,w;
    int i;

    gs = g[start];
    if ((gs & last)) return TRUE;

    w = gs & body;
    while (w)
    {
        TAKEBIT(i,w);
        if (hasinducedpath(g,i,body&~gs,last&~bit[i]&~gs))
            return TRUE;
    }

    return FALSE;
}

static boolean
notchordal(graph *g, int n, int maxn)
/* g is a graph of order n. Return TRUE if there is a
   chordless cycle of length at least 4 that includes
   the last vertex. */
{
    setword all,gn,w,gs;
    int v,s;

    all = ALLMASK(n);
    gn = g[n-1];

    while (gn)
    {
        TAKEBIT(v,gn);
        gs = g[v] & ~(bit[n-1]|g[n-1]);
        while (gs)
        {
            TAKEBIT(s,gs);
            if (hasinducedpath(g,s,all&~(g[n-1]|g[v]),gn&~g[v]))
                return TRUE;
        }
    }

    return FALSE;
}

static boolean
notsplit(graph *g, int n, int maxn)
/* g is a graph of order n. Return TRUE if either g or its
   complement has a chordless cycle of length at least 4 that
   includes the last vertex. */
{
    graph gc[MAXN];
    setword w;
    int i;

    if (notchordal(g,n,maxn)) return TRUE;

    w = ALLMASK(n);
    for (i = 0; i < n; ++i) gc[i] = g[i] ^ w ^ bit[i];
    return notchordal(gc,n,maxn);
}

static boolean
hasinducedoddpath(graph *g, int start, setword body, setword last, boolean parity)
/* return TRUE if there is an induced path of odd length >= 3 in g
   starting at start, extravertices within body and ending in last.
   {start}, body and last should be disjoint. */
{
    setword gs,w;
    int i;

    gs = g[start];
    if ((gs & last) && parity) return TRUE;

    w = gs & body;
    while (w)
    {
        TAKEBIT(i,w);
        if (hasinducedoddpath(g,i,body&~gs,last&~bit[i]&~gs,!parity))
            return TRUE;
    }

    return FALSE;
}

static boolean
oddchordless(graph *g, int n, int maxn)
/* g is a graph of order n. Return TRUE if there is a
   chordless cycle of odd length at least 5 that includes
   the last vertex. */
{
    setword all,gn,w,gs;
    int v,s;

    all = ALLMASK(n);
    gn = g[n-1];

    while (gn)
    {
        TAKEBIT(v,gn);
        gs = g[v] & ~(bit[n-1]|g[n-1]);
        while (gs)
        {
            TAKEBIT(s,gs);
            if (hasinducedoddpath(g,s,all&~(g[n-1]|g[v]),gn&~g[v],FALSE))
                return TRUE;
        }
    }

    return FALSE;
}

static boolean
notperfect(graph *g, int n, int maxn)
/* g is a graph of order n. Return TRUE if either g or its
   complement has a chordless cycle of odd length at least 5 that
   includes the last vertex. I.e., if it is not perfect. */
{
    graph gc[MAXN];
    setword w;
    int i;

    if (oddchordless(g,n,maxn)) return TRUE;

    w = ALLMASK(n);
    for (i = 0; i < n; ++i) gc[i] = g[i] ^ w ^ bit[i];
    return oddchordless(gc,n,maxn);
}

/**************************************************************************/

static boolean
accept1(graph *g, int n, xword x, graph *gx, int *deg, boolean *rigid)
/* decide if n in theta(g+x) -  version for n+1 < maxn */
{
    int i;
    int lab[MAXN],ptn[MAXN],orbits[MAXN];
    int count[MAXN];
    graph h[MAXN];
    xword xw;
    int nx,numcells,code;
    int i0,i1,degn;
    set active[MAXM];
    statsblk stats;
    static TLS_ATTR DEFAULTOPTIONS_GRAPH(options);
    setword workspace[50];

#ifdef INSTRUMENT
    ++a1calls;
#endif

    nx = n + 1;
    for (i = 0; i < n; ++i) gx[i] = g[i];
    gx[n] = 0;
    deg[n] = degn = XPOPCOUNT(x);

    xw = x;
    while (xw)
    {
        i = XNEXTBIT(xw);
        xw ^= XBIT(i);
        gx[i] |= bit[n];
        gx[n] |= bit[i];
        ++deg[i];
    }

    if (k4free && hask4(gx,n+1,maxn)) return FALSE;
    if (clawfree && hasclaw(gx,n+1,maxn)) return FALSE;
#ifdef PREPRUNE
    if (PREPRUNE(gx,n+1,maxn)) return FALSE;
#endif
    if (connec == 2 && n+2 == maxn && !isconnected(gx,n+1)) return FALSE;
    if (((connec ==2 && n+2 < maxn) || (connec == 1 && n+2 <= maxn))
           && connpreprune(gx,n+1,maxn)) return FALSE;

    i0 = 0;
    i1 = n;
    for (i = 0; i < nx; ++i)
    {
        if (deg[i] == degn) lab[i1--] = i;
        else                lab[i0++] = i;
        ptn[i] = 1;
    }
    ptn[n] = 0;
    if (i0 == 0)
    {
        numcells = 1;
        active[0] = bit[0];
    }
    else
    {
        numcells = 2;
        active[0] = bit[0] | bit[i1+1];
        ptn[i1] = 0;
    }
    refinex(gx,lab,ptn,0,&numcells,count,active,FALSE,&code,1,nx);

    if (code < 0) return FALSE;

    if (numcells == nx)
    {
        *rigid = TRUE;
#ifdef INSTRUMENT
        ++a1succs;
#endif
        return TRUE;
    }

    options.getcanon = TRUE;
    options.defaultptn = FALSE;
    options.userautomproc = userautomproc;

    active[0] = 0;
#ifdef INSTRUMENT
    ++a1nauty;
#endif
    nauty(gx,lab,ptn,active,orbits,&options,&stats,workspace,50,1,nx,h);

    if (orbits[lab[n]] == orbits[n])
    {
        *rigid = stats.numorbits == nx;
#ifdef INSTRUMENT
        ++a1succs;
#endif
        return TRUE;
    }
    else
        return FALSE;
}

/**************************************************************************/

static boolean
accept1b(graph *g, int n, xword x, graph *gx, int *deg, boolean *rigid,
     void (*makeh)(graph*,xword*,int))
/* decide if n in theta(g+x)  --  version for n+1 < maxn */
{
    int i,v;
    xword z,hv,bitv,ixx;
    int lab[MAXN],ptn[MAXN],orbits[MAXN];
    int count[MAXN];
    graph gc[MAXN];
    xword h[MAXN],xw,jxx,kxx,*xx;
    int nx,numcells,code;
    int i0,i1,degn,xubx;
    set active[MAXM];
    statsblk stats;
    static TLS_ATTR DEFAULTOPTIONS_GRAPH(options);
    setword workspace[50];

#ifdef INSTRUMENT
    ++a1calls;
#endif

    nx = n + 1;
    for (i = 0; i < n; ++i) gx[i] = g[i];
    gx[n] = 0;
    deg[n] = degn = XPOPCOUNT(x);

    xw = x;
    while (xw)
    {
        i = XNEXTBIT(xw);
        xw ^= XBIT(i);
        gx[i] |= bit[n];
        gx[n] |= bit[i];
        ++deg[i];
    }

    if (k4free && hask4(gx,n+1,maxn)) return FALSE;
    if (clawfree && hasclaw(gx,n+1,maxn)) return FALSE;
#ifdef PREPRUNE
    if (PREPRUNE(gx,n+1,maxn)) return FALSE;
#endif
    if (connec == 2 && n+2 == maxn && !isconnected(gx,n+1)) return FALSE;
    if (((connec ==2 && n+2 < maxn) || (connec == 1 && n+2 <= maxe))
           && connpreprune(gx,n+1,maxn)) return FALSE;

    i0 = 0;
    i1 = n;
    for (i = 0; i < nx; ++i)
    {
        if (deg[i] == degn) lab[i1--] = i;
        else                lab[i0++] = i;
        ptn[i] = 1;
    }
    ptn[n] = 0;
    if (i0 == 0)
    {
        numcells = 1;
        active[0] = bit[0];
    }
    else
    {
        numcells = 2;
        active[0] = bit[0] | bit[i1+1];
        ptn[i1] = 0;
    }
    refinex(gx,lab,ptn,0,&numcells,count,active,FALSE,&code,1,nx);

    if (code < 0) return FALSE;

    (*makeh)(gx,h,nx);
    xx = data[nx].xx;
    xubx = data[nx].xub;

    xx[0] = 0;
    kxx = 1;
    for (v = 0; v < nx; ++v)
    {
        bitv = XBIT(v);
        hv = h[v];
        jxx = kxx;
        for (ixx = 0; ixx < jxx; ++ixx)
        if ((hv & xx[ixx]) == 0)
        {
            z = xx[ixx] | bitv;
            if (XPOPCOUNT(z) <= xubx) xx[kxx++] = z;
        }
    }
    data[nx].xlim = kxx;

    if (numcells == nx)
    {
        *rigid = TRUE;
#ifdef INSTRUMENT
        ++a1succs;
#endif
        return TRUE;
    }

    options.getcanon = TRUE;
    options.defaultptn = FALSE;
    options.userautomproc = userautomprocb;

    active[0] = 0;
#ifdef INSTRUMENT
    ++a1nauty;
#endif
    nauty(gx,lab,ptn,active,orbits,&options,&stats,workspace,50,1,nx,gc);

    if (orbits[lab[n]] == orbits[n])
    {
        *rigid = stats.numorbits == nx;
#ifdef INSTRUMENT
        ++a1succs;
#endif
        return TRUE;
    }
    else
        return FALSE;
}

/**************************************************************************/

static boolean
accept2(graph *g, int n, xword x, graph *gx, int *deg, boolean nuniq)
/* decide if n in theta(g+x)  --  version for n+1 == maxn */
{
    int i;
    int lab[MAXN],ptn[MAXN],orbits[MAXN];
    int degx[MAXN],invar[MAXN];
    setword vmax,gv;
    int qn,qv;
    int count[MAXN];
    xword xw;
    int nx,numcells,code;
    int degn,i0,i1,j,j0,j1;
    set active[MAXM];
    statsblk stats;
    static TLS_ATTR DEFAULTOPTIONS_GRAPH(options);
    setword workspace[50];
    boolean cheapacc;

#ifdef INSTRUMENT
    ++a2calls;
    if (nuniq) ++a2uniq;
#endif
    nx = n + 1;
    for (i = 0; i < n; ++i)
    {
        gx[i] = g[i];
        degx[i] = deg[i];
    }
    gx[n] = 0;
    degx[n] = degn = XPOPCOUNT(x);

    xw = x;
    while (xw)
    {
        i = XNEXTBIT(xw);
        xw ^= XBIT(i);
        gx[i] |= bit[n];
        gx[n] |= bit[i];
        ++degx[i];
    }

    if (k4free && hask4(gx,n+1,maxn)) return FALSE;
    if (clawfree && hasclaw(gx,n+1,maxn)) return FALSE;
#ifdef PREPRUNE
    if (PREPRUNE(gx,n+1,maxn)) return FALSE;
#endif
    if (connec == 2 && n+2 == maxn && !isconnected(gx,n+1)) return FALSE;
    if (((connec ==2 && n+2 < maxn) || (connec == 1 && n+2 <= maxe))
           && connpreprune(gx,n+1,maxn)) return FALSE;

    if (nuniq)
    {
#ifdef INSTRUMENT
        ++a2succs;
#endif
        if (canonise) makecanon(gx,gcan,nx);
        return TRUE;
    }

    i0 = 0;
    i1 = n;
    for (i = 0; i < nx; ++i)
    {
        if (degx[i] == degn) lab[i1--] = i;
        else                 lab[i0++] = i;
        ptn[i] = 1;
    }
    ptn[n] = 0;
    if (i0 == 0)
    {
        numcells = 1;
        active[0] = bit[0];

        if (!distinvar(gx,invar,nx)) return FALSE;
        qn = invar[n];
        j0 = 0;
        j1 = n;
        while (j0 <= j1)
        {
            j = lab[j0];
            qv = invar[j];
            if (qv < qn)
                ++j0;
            else
            {
                lab[j0] = lab[j1];
                lab[j1] = j;
                --j1;
            }
        }
        if (j0 > 0)
        {
            if (j0 == n)
            {
#ifdef INSTRUMENT
                ++a2succs;
#endif
                if (canonise) makecanon(gx,gcan,nx);
                return TRUE;
            }
            ptn[j1] = 0;
            ++numcells;
            active[0] |= bit[j0];
        }
    }
    else
    {
        numcells = 2;
        ptn[i1] = 0;
        active[0] = bit[0] | bit[i1+1];

        vmax = 0;
        for (i = i1+1; i < nx; ++i) vmax |= bit[lab[i]];

        gv = gx[n] & vmax;
        qn = POPCOUNT(gv);

        j0 = i1+1;
        j1 = n;
        while (j0 <= j1)
        {
            j = lab[j0];
            gv = gx[j] & vmax;
            qv = POPCOUNT(gv);
            if (qv > qn)
                return FALSE;
            else if (qv < qn)
                ++j0;
            else
            {
                lab[j0] = lab[j1];
                lab[j1] = j;
                --j1;
            }
        }
        if (j0 > i1+1)
        {
            if (j0 == n)
            {
#ifdef INSTRUMENT
                ++a2succs;
#endif
                if (canonise) makecanon(gx,gcan,nx);
                return TRUE;
            }
            ptn[j1] = 0;
            ++numcells;
            active[0] |= bit[j0];
        }
    }

    refinex(gx,lab,ptn,0,&numcells,count,active,TRUE,&code,1,nx);

    if (code < 0) return FALSE;

    cheapacc = FALSE;
    if (code > 0 || numcells >= nx-4)
        cheapacc = TRUE;
    else if (numcells == nx-5)
    {
        for (j1 = nx-2; j1 >= 0 && ptn[j1] > 0; --j1) {}
        if (nx - j1 != 5) cheapacc = TRUE;
    }
    else
    {
        j1 = nx;
        j0 = 0;
        for (i1 = 0; i1 < nx; ++i1)
        {
            --j1;
            if (ptn[i1] > 0)
            {
                ++j0;
                while (ptn[++i1] > 0) {}
            }
        }
        if (j1 <= j0 + 1) cheapacc = TRUE;
    }
    
    if (cheapacc)
    {
#ifdef INSTRUMENT
        ++a2succs;
#endif
        if (canonise) makecanon(gx,gcan,nx);
        return TRUE;
    }

    options.getcanon = TRUE;
    options.defaultptn = FALSE;

    active[0] = 0;
#ifdef INSTRUMENT
    ++a2nauty;
#endif
    nauty(gx,lab,ptn,active,orbits,&options,&stats,workspace,50,1,nx,gcan);

    if (orbits[lab[n]] == orbits[n])
    {
#ifdef INSTRUMENT
        ++a2succs;
#endif
        if (canonise) makecanon(gx,gcan,nx);
        return TRUE;
    }
    else
        return FALSE;
}

/**************************************************************************/

static void
xbnds(int n, int ne, int dmax)
/* find bounds on extension degree;  store answer in data[*].*  */
{
    int xlb,xub,d,nn,m,xc;

    xlb = n == 1 ? 0 : (dmax > (2*ne + n - 2)/(n - 1) ?
                        dmax : (2*ne + n - 2)/(n - 1));
    xub = n < maxdeg ? n : maxdeg;

    for (xc = xub; xc >= xlb; --xc)
    {
        d = xc;
        m = ne + d;
        for (nn = n+1; nn < maxn; ++nn)
        {
            if (d < (2*m + nn - 2)/(nn - 1)) d = (2*m + nn - 2)/(nn - 1);
            m += d;
        }
        if (d > maxdeg || m > maxe) xub = xc - 1;
        else                        break;
    }

    if (ne + xlb < mine)
        for (xc = xlb; xc <= xub; ++xc)
        {
            m = ne + xc;
            for (nn = n + 1; nn < maxn; ++nn)
                m += maxdeg < nn ? maxdeg : nn;
            if (m < mine) xlb = xc + 1;
            else          break;
        }

    data[n].ne = ne;
    data[n].dmax = dmax;
    data[n].xlb = xlb;
    data[n].xub = xub;
}

/**************************************************************************/

static void
spaextend(graph *g, int n, int *deg, int ne, boolean rigid,
      int xlb, int xub, void (*makeh)(graph*,xword*,int))
/* extend from n to n+1 -- version for restricted graphs */
{
    xword x,d,dlow;
    xword xlim,*xorb;
    int xc,nx,i,j,dmax,dcrit,xlbx,xubx;
    graph gx[MAXN];
    xword *xx,ixx;
    int degx[MAXN];
    boolean rigidx;

#ifdef INSTRUMENT
    boolean haschild;

    haschild = FALSE;
    if (rigid) ++rigidnodes[n];
#endif
    ++nodes[n];

    nx = n + 1;
    dmax = deg[n-1];
    dcrit = mindeg - maxn + n;
    d = dlow = 0;
    for (i = 0; i < n; ++i)
    {
        if (deg[i] == dmax) d |= XBIT(i);
        if (deg[i] == dcrit) dlow |= XBIT(i);
    }

    if (xlb == dmax && XPOPCOUNT(d) + dmax > n) ++xlb;
    if (nx == maxn && xlb < mindeg) xlb = mindeg;
    if (xlb > xub) return;

    if (splitgraph && notsplit(g,n,maxn)) return;
    if (chordal && notchordal(g,n,maxn)) return;
    if (perfect && notperfect(g,n,maxn)) return;
#ifdef PRUNE
    if (PRUNE(g,n,maxn)) return;
#endif

    xorb = data[n].xorb;
    xx = data[n].xx;
    xlim = data[n].xlim;

    if (nx == maxn)
    {
        for (ixx = 0; ixx < xlim; ++ixx)
        {
            x = xx[ixx];
            xc = XPOPCOUNT(x);
            if (xc < xlb || xc > xub) continue;
            if ((rigid || xorb[ixx] == ixx) 
                && (xc > dmax || (xc == dmax && (x & d) == 0))
                && (dlow & ~x) == 0)
            {
                if (accept2(g,n,x,gx,deg,
                    xc > dmax+1 || (xc == dmax+1 && (x & d) == 0))
                    && (!connec ||
                          (connec==1 && isconnected(gx,nx)) ||
                          (connec>1 && isbiconnected(gx,nx))))
                {
                    if (splitgraph && notsplit(gx,nx,maxn)) continue;
                    if (chordal && notchordal(gx,nx,maxn)) continue;
                    if (perfect && notperfect(gx,nx,maxn)) continue;
#ifdef PRUNE
                    if (!PRUNE(gx,nx,maxn))
#endif
                    {
#ifdef INSTRUMENT
                        haschild = TRUE;
#endif
                        ++ecount[ne+xc];
                        (*outproc)(outfile,canonise ? gcan : gx,nx);
                    }
                }
            }
        }
    }
    else
    {
        for (ixx = 0; ixx < xlim; ++ixx)
        {
            if (nx == splitlevel)
            {
                if (odometer-- != 0) continue;
                odometer = mod - 1;
            }
            x = xx[ixx];
            xc = XPOPCOUNT(x);
            if (xc < xlb || xc > xub) continue;
            if ((rigid || xorb[ixx] == ixx)
                && (xc > dmax || (xc == dmax && (x & d) == 0))
                && (dlow & ~x) == 0)
            {
                for (j = 0; j < n; ++j) degx[j] = deg[j];
                if (data[nx].ne != ne+xc || data[nx].dmax != xc)
                    xbnds(nx,ne+xc,xc);

                xlbx = data[nx].xlb;
                xubx = data[nx].xub;
                if (xlbx <= xubx
                    && accept1b(g,n,x,gx,degx,&rigidx,makeh))
                {
#ifdef INSTRUMENT
                    haschild = TRUE;
#endif
                    spaextend(gx,nx,degx,ne+xc,rigidx,xlbx,xubx,makeh);
                }
            }
        }
        if (n == splitlevel - 1 && n >= min_splitlevel
                                && nodes[n] >= multiplicity)
            --splitlevel;
    }
#ifdef INSTRUMENT
    if (haschild) ++fertilenodes[n];
#endif
}

/**************************************************************************/

static void
genextend(graph *g, int n, int *deg, int ne, boolean rigid, int xlb, int xub)
/* extend from n to n+1 -- version for general graphs */
{
    xword x,d,dlow;
    xword *xset,*xcard,*xorb;
    xword i,imin,imax;
    int nx,xc,j,dmax,dcrit;
    int xlbx,xubx;
    graph gx[MAXN];
    int degx[MAXN];
    boolean rigidx;

#ifdef INSTRUMENT
    boolean haschild;

    haschild = FALSE;
    if (rigid) ++rigidnodes[n];
#endif
    ++nodes[n];

    nx = n + 1;
    dmax = deg[n-1];
    dcrit = mindeg - maxn + n;
    d = dlow = 0;
    for (i = 0; i < n; ++i)
    {
        if (deg[i] == dmax) d |= XBIT(i);
        if (deg[i] == dcrit) dlow |= XBIT(i);
    }

    if (xlb == dmax && XPOPCOUNT(d) + dmax > n) ++xlb;
    if (nx == maxn && xlb < mindeg) xlb = mindeg;
    if (xlb > xub) return;

    if (splitgraph && notsplit(g,n,maxn)) return;
    if (chordal && notchordal(g,n,maxn)) return;
    if (perfect && notperfect(g,n,maxn)) return;
#ifdef PRUNE 
    if (PRUNE(g,n,maxn)) return; 
#endif 

    imin = data[n].xstart[xlb];
    imax = data[n].xstart[xub+1];
    xset = data[n].xset;
    xcard = data[n].xcard;
    xorb = data[n].xorb;

    if (nx == maxn)
        for (i = imin; i < imax; ++i)
        {
            if (!rigid && xorb[i] != i) continue;
            x = xset[i];
            xc = (int)xcard[i];
            if (xc == dmax && (x & d) != 0) continue;
            if ((dlow & ~x) != 0) continue;

            if (accept2(g,n,x,gx,deg,
                        xc > dmax+1 || (xc == dmax+1 && (x & d) == 0)))
                if (!connec || (connec==1 && isconnected(gx,nx))
                            || (connec>1 && isbiconnected(gx,nx)))
                {
                    if (splitgraph && notsplit(gx,nx,maxn)) continue;
                    if (chordal && notchordal(gx,nx,maxn)) continue;
                    if (perfect && notperfect(gx,nx,maxn)) continue;
#ifdef PRUNE
                    if (!PRUNE(gx,nx,maxn))
#endif
                    {
#ifdef INSTRUMENT
                        haschild = TRUE;
#endif
                        ++ecount[ne+xc];
                        (*outproc)(outfile,canonise ? gcan : gx,nx);
                    }
                }
        }
    else
        for (i = imin; i < imax; ++i)
        {
            if (!rigid && xorb[i] != i) continue;
            x = xset[i];
            xc = (int)xcard[i];
            if (xc == dmax && (x & d) != 0) continue;
            if ((dlow & ~x) != 0) continue;
            if (nx == splitlevel)
            {
                if (odometer-- != 0) continue;
                odometer = mod - 1;
            }

            for (j = 0; j < n; ++j) degx[j] = deg[j];
            if (data[nx].ne != ne+xc || data[nx].dmax != xc)
                xbnds(nx,ne+xc,xc);
            xlbx = data[nx].xlb;
            xubx = data[nx].xub;
            if (xlbx > xubx) continue;

            data[nx].lo = data[nx].xstart[xlbx];
            data[nx].hi = data[nx].xstart[xubx+1];
            if (accept1(g,n,x,gx,degx,&rigidx))
            {
#ifdef INSTRUMENT
                haschild = TRUE;
#endif
                genextend(gx,nx,degx,ne+xc,rigidx,xlbx,xubx);
            }
        }

    if (n == splitlevel-1 && n >= min_splitlevel
            && nodes[n] >= multiplicity)
        --splitlevel;
#ifdef INSTRUMENT
    if (haschild) ++fertilenodes[n];
#endif
}

/**************************************************************************/
/**************************************************************************/

// TODO: probably rework
#ifdef GENG_MAIN
void
GENG_MAIN(int geng_argc, unsigned int argv1, unsigned int argv2)
{
    int argc = geng_argc;
    size_t *p_argv = (size_t *) (((size_t) argv1) | (((size_t) argv2) << 32));
    char **argv = (char **) *p_argv;
    generate_done = 0;
#else
int
main(int argc, char *argv[])
{
#endif
    char *arg;
    boolean badargs,gote,gotmr,gotf,gotd,gotD,gotx,gotX;
    boolean secret,connec1,connec2,safe,sparse;
    char *outfilename,sw;
    int i,j,argnum;
    graph g[1];
    int tmaxe,deg[1];
    nauty_counter nout;
    int splitlevinc;
    double t1,t2;
    char msg[201];

    /* HELP; PUTVERSION; */
    nauty_check(WORDSIZE,1,MAXN,NAUTYVERSIONID);

    badargs = FALSE;
    trianglefree = bipartite = squarefree = FALSE;
    k4free = splitgraph = chordal = perfect = clawfree = FALSE;
    verbose = quiet = FALSE;
    nautyformat = graph6 = sparse6 = nooutput = FALSE;
    savemem = canonise = header = FALSE;
    outfilename = NULL;
    secret = safe = FALSE;
    connec1 = connec2 = FALSE;

    maxdeg = MAXN;
    mindeg = 0;

    gotX = gotx = gotd = gotD = gote = gotmr = gotf = FALSE;

    argnum = 0;
    for (j = 1; !badargs && j < argc; ++j)
    {
        arg = argv[j];
        if (arg[0] == '-' && arg[1] != '\0')
        {
            ++arg;
            while (*arg != '\0')
            {
                sw = *arg++;
                     SWBOOLEAN('n',nautyformat)
                else SWBOOLEAN('u',nooutput)
                else SWBOOLEAN('g',graph6)
                else SWBOOLEAN('s',sparse6)
                else SWBOOLEAN('t',trianglefree)
                else SWBOOLEAN('f',squarefree)
                else SWBOOLEAN('k',k4free)
                else SWBOOLEAN('S',splitgraph)
                else SWBOOLEAN('T',chordal)
                else SWBOOLEAN('P',perfect)
                else SWBOOLEAN('F',clawfree)
                else SWBOOLEAN('b',bipartite)
                else SWBOOLEAN('v',verbose)
                else SWBOOLEAN('l',canonise)
                else SWBOOLEAN('h',header)
                else SWBOOLEAN('m',savemem)
                else SWBOOLEAN('c',connec1)
                else SWBOOLEAN('C',connec2)
                else SWBOOLEAN('q',quiet)
                else SWBOOLEAN('$',secret)
                else SWBOOLEAN('S',safe)
                else SWINT('d',gotd,mindeg,"geng -d")
                else SWINT('D',gotD,maxdeg,"geng -D")
                else SWINT('x',gotx,multiplicity,"geng -x")
                else SWINT('X',gotX,splitlevinc,"geng -X")
#ifdef PLUGIN_SWITCHES
PLUGIN_SWITCHES
#endif
                else badargs = TRUE;
            }
        }
        else if (arg[0] == '-' && arg[1] == '\0')
            gotf = TRUE;
        else
        {
            if (argnum == 0)
            {
                if (sscanf(arg,"%d",&maxn) != 1) badargs = TRUE;
                ++argnum;
            }
            else if (gotf)
                badargs = TRUE;
            else
            {
                if (!gotmr)
                {
                    if (sscanf(arg,"%d/%d",&res,&mod) == 2)
                    {
                        gotmr = TRUE;
                        continue;
                    }
                }
                if (!gote)
                {
                    if (sscanf(arg,"%d:%d",&mine,&maxe) == 2
                     || sscanf(arg,"%d-%d",&mine,&maxe) == 2)
                    {
                        gote = TRUE;
                        if (maxe == 0 && mine > 0) maxe = MAXN*(MAXN-1)/2;
                        continue;
                    }
                    else if (sscanf(arg,"%d",&mine) == 1)
                    {
                        gote = TRUE;
                        maxe = mine;
                        continue;
                    }
                }
                if (!gotf)
                {
                    outfilename = arg;
                    gotf = TRUE;
                    continue;
                }
            }
        }
    }

    if (argnum == 0)
        badargs = TRUE;
    else if (maxn < 1 || maxn > MAXN || maxn > 64)
    {
        fprintf(stderr,">E geng: n must be in the range 1..%d\n",MAXN);
        badargs = TRUE;
    }

    if (!gotmr)
    {
        mod = 1;
        res = 0;
    }

    if (!gote)
    {
        mine = 0;
        maxe = (maxn*maxn - maxn) / 2;
    }

    if (trianglefree || squarefree || bipartite) k4free = FALSE;
    if (bipartite) perfect = FALSE;  /* bipartite graphs are perfect */
    if (splitgraph) chordal = perfect = FALSE; /* split graphs are chordal */
    if (chordal) perfect = FALSE;  /* chordal graphs are perfect */
    if (clawfree && bipartite)
    {
        clawfree = FALSE;
        if (maxdeg > 2) maxdeg = 2;
    }
    if (chordal && bipartite && maxe >= maxn) maxe = maxn - 1;
    if (splitgraph && bipartite && maxe >= maxn) maxe = maxn - 1;

    if (connec1 && mindeg < 1 && maxn > 1) mindeg = 1;
    if (connec2 && mindeg < 2 && maxn > 2) mindeg = 2;
    if (maxdeg >= maxn) maxdeg = maxn - 1;
    if (maxe > maxn*maxdeg / 2) maxe = maxn*maxdeg / 2;
    if (maxdeg > maxe) maxdeg = maxe;
    if (mindeg < 0) mindeg = 0;
    if (mine < (maxn*mindeg+1) / 2) mine = (maxn*mindeg+1) / 2;
    if (maxdeg > 2*maxe - mindeg*(maxn-1)) maxdeg = 2*maxe - mindeg*(maxn-1);

    if      (connec2) connec = 2;
    else if (connec1) connec = 1;
    else              connec = 0;
    if (connec && mine < maxn-1) mine = maxn - 2 + connec;

#if defined(PRUNE) || defined(PREPRUNE)
    geng_mindeg = mindeg;
    geng_maxdeg = maxdeg;
    geng_mine = mine;
    geng_maxe = maxe;
    geng_connec = connec;
#endif

    if (!badargs && (mine > maxe || maxe < 0 || maxdeg < 0))
    {
        fprintf(stderr,
                ">E geng: impossible mine,maxe,mindeg,maxdeg values\n");
        badargs = TRUE;
    }

    if (!badargs && (res < 0 || res >= mod))
    {
        fprintf(stderr,">E geng: must have 0 <= res < mod\n");
        badargs = TRUE;
    }

    if (badargs)
    {
        fprintf(stderr,">E Usage: %s\n",USAGE);
        GETHELP;
        exit(1);
    }

    if ((nautyformat!=0) + (graph6!=0) + (sparse6!=0) + (nooutput!=0) > 1)
        gt_abort(">E geng: -ungs are incompatible\n");

#ifdef OUTPROC
    outproc = OUTPROC;
#else
    if (nautyformat)   outproc = writenauty;
    else if (nooutput) outproc = nullwrite;
    else if (sparse6)  outproc = writes6x;
    else               outproc = writeg6x;
#endif

#ifdef PLUGIN_INIT
PLUGIN_INIT
#endif

    for (i = 0; i <= maxe; ++i) ecount[i] = 0;
    for (i = 0; i < maxn; ++i)  nodes[i] = 0;

    if (nooutput)
        outfile = stdout;
    else if (!gotf || outfilename == NULL)
    {
        outfilename = "stdout";
        outfile = stdout;
    }
    else if ((outfile = fopen(outfilename,
                    nautyformat ? "wb" : "w")) == NULL)
    {
        fprintf(stderr,
              ">E geng: can't open %s for writing\n",outfilename);
        gt_abort(NULL);
    }

    if (bipartite)
        if (squarefree)  tmaxe = findmaxe(maxebf,maxn);
        else             tmaxe = findmaxe(maxeb,maxn);
    else if (trianglefree)
        if (squarefree)  tmaxe = findmaxe(maxeft,maxn);
        else             tmaxe = findmaxe(maxet,maxn);
    else if (squarefree) tmaxe = findmaxe(maxef,maxn);
    else                 tmaxe = (maxn*maxn - maxn) / 2;

    if (safe) ++tmaxe;

    if (maxe > tmaxe) maxe = tmaxe;

    if (gotx)
    {
        if (multiplicity < 3 * mod || multiplicity > 999999999)
            gt_abort(">E geng: -x value must be in [3*mod,10^9-1]\n");
    }
    else
    {
        multiplicity = PRUNEMULT * mod;
        if (multiplicity / PRUNEMULT != mod)
            gt_abort(">E geng: mod value is too large\n");
    }

    if (!gotX) splitlevinc = 0;

    if (!quiet)
    {
        msg[0] = '\0';
        if (strlen(argv[0]) > 75)
            fprintf(stderr,">A %s",argv[0]);
        else
            CATMSG1(">A %s",argv[0]);

        CATMSG7(" -%s%s%s%s%s%s%s",
            connec2      ? "C" : connec1 ? "c" : "",
            trianglefree ? "t" : "",
            squarefree   ? "f" : "",
            k4free       ? "k" : "",
            bipartite    ? "b" : "",
            canonise     ? "l" : "",
            savemem      ? "m" : "");
        if (splitgraph || chordal || perfect || clawfree)
            CATMSG4(" -%s%s%s%s",
                splitgraph ? "S" : "",
                chordal ? "T" : "",
                perfect ? "P" : "",
                clawfree ? "F" : "");
        if (mod > 1)
            CATMSG2("X%dx%d",splitlevinc,multiplicity);
        CATMSG4("d%dD%d n=%d e=%d",mindeg,maxdeg,maxn,mine);
        if (maxe > mine) CATMSG1("-%d",maxe);
        if (mod > 1) CATMSG2(" class=%d/%d",res,mod);
        CATMSG0("\n");
        fputs(msg,stderr);
        fflush(stderr);
    }

    g[0] = 0;
    deg[0] = 0;

    sparse = bipartite || squarefree || trianglefree || savemem;

    t1 = CPUTIME;

    if (header)
    {
        if (sparse6)
        {
            writeline(outfile,SPARSE6_HEADER);
            fflush(outfile);
        }
        else if (!nautyformat && !nooutput)
        {
            writeline(outfile,GRAPH6_HEADER);
            fflush(outfile);
        }
    }

    if (maxn == 1)
    {
        if (res == 0 && connec < 2)
        {
            ++ecount[0];
            (*outproc)(outfile,g,1);
        }
    }
    else
    {
        if (maxn > 28 || maxn+4 > 8*sizeof(xword))
            savemem = sparse = TRUE;
        if (maxn == maxe+1 && connec)
            bipartite = squarefree = sparse = TRUE;  /* trees */

        makeleveldata(sparse);

        if (maxn >= 14 && mod > 1)     splitlevel = maxn - 4;
        else if (maxn >= 6 && mod > 1) splitlevel = maxn - 3;
        else                           splitlevel = -1;

        if (splitlevel > 0) splitlevel += splitlevinc;
        if (splitlevel > maxn - 1) splitlevel = maxn - 1;
        if (splitlevel < 3) splitlevel = -1;

        min_splitlevel = 6;
        odometer = secret ? -1 : res;

        if (maxe >= mine &&
                (mod <= 1 || (mod > 1 && (splitlevel > 2 || res == 0))))
        {
            xbnds(1,0,0);
            if (sparse)
            {
                data[1].xx[0] = 0;
                if (maxdeg > 0) data[1].xx[1] = XBIT(0);
                data[1].xlim = data[1].xub + 1;
            }

            if (bipartite)
                if (squarefree)
                    spaextend(g,1,deg,0,TRUE,
                                    data[1].xlb,data[1].xub,makeb6graph);
                else
                    spaextend(g,1,deg,0,TRUE,
                                    data[1].xlb,data[1].xub,makebgraph);
            else if (trianglefree)
                if (squarefree)
                    spaextend(g,1,deg,0,TRUE,
                                    data[1].xlb,data[1].xub,makeg5graph);
                else
                    spaextend(g,1,deg,0,TRUE,
                                    data[1].xlb,data[1].xub,makexgraph);
            else if (squarefree)
                spaextend(g,1,deg,0,TRUE,
                                    data[1].xlb,data[1].xub,makesgraph);
            else if (savemem)
                spaextend(g,1,deg,0,TRUE,
                                    data[1].xlb,data[1].xub,make0graph);
            else
                genextend(g,1,deg,0,TRUE,data[1].xlb,data[1].xub);
        }
    }
    t2 = CPUTIME;

    nout = 0;
    for (i = 0; i <= maxe; ++i) nout += ecount[i];

    if (verbose)
    {
        for (i = 0; i <= maxe; ++i)
            if (ecount[i] != 0)
            {
                fprintf(stderr,">C " COUNTER_FMT " graphs with %d edges\n",
                     ecount[i],i);
            }
    }

#ifdef INSTRUMENT
    fprintf(stderr,"\n>N node counts\n");
    for (i = 1; i < maxn; ++i)
    {
        fprintf(stderr," level %2d: ",i);
        fprintf(stderr,COUNTER_FMT " (" COUNTER_FMT
                       " rigid, " COUNTER_FMT " fertile)\n",
                       nodes[i],rigidnodes[i],fertilenodes[i]);
    }
    fprintf(stderr,">A1 " COUNTER_FMT " calls to accept1, "
                   COUNTER_FMT " nauty, " COUNTER_FMT " succeeded\n",
                   a1calls,a1nauty,a1succs);
    fprintf(stderr,">A2 " COUNTER_FMT " calls to accept2, " COUNTER_FMT
                   " nuniq, "COUNTER_FMT " nauty, " COUNTER_FMT " succeeded\n",
                   a2calls,a2uniq,a2nauty,a2succs);
    fprintf(stderr,"\n");
#endif

#ifdef SUMMARY
    SUMMARY(nout,t2-t1);
#endif

    if (!quiet)
    {
        fprintf(stderr,">Z " COUNTER_FMT " graphs generated in %3.2f sec\n",
                nout,t2-t1);
    }

#ifdef GENG_MAIN
    for (i = 1; i < maxn; ++i)
        if (sparse)
        {
            free(data[i].xorb);
            free(data[i].xx);
        }
        else
        {
            free(data[i].xorb);
            free(data[i].xset);
            free(data[i].xinv);
            free(data[i].xcard);
        }
    generate_done = 1;
    // TODO: probably rework
    /* return 0; */
#else
    exit(0);
#endif
}