This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
/
bibd.py
1119 lines (882 loc) · 38.7 KB
/
bibd.py
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
r"""
Balanced Incomplete Block Designs (BIBD)
This module implements two constructions of Balanced Incomplete Block Designs:
* Steiner Triple Systems, i.e. `(v,3,1)`-BIBD.
* `K_4`-decompositions of `K_v`, i.e. `(v,4,1)`-BIBD.
These BIBD can be obtained through the :meth:`BalancedIncompleteBlockDesign`
method, available in Sage as ``designs.BalancedIncompleteBlockDesign``.
EXAMPLES::
sage: designs.BalancedIncompleteBlockDesign(7,3)
Incidence structure with 7 points and 7 blocks
sage: designs.BalancedIncompleteBlockDesign(7,3).blocks()
[[0, 1, 3], [0, 2, 4], [0, 5, 6], [1, 2, 6], [1, 4, 5], [2, 3, 5], [3, 4, 6]]
sage: designs.BalancedIncompleteBlockDesign(13,4).blocks()
[[0, 1, 2, 12], [0, 3, 6, 9], [0, 4, 8, 10], [0, 5, 7, 11], [1, 3, 8, 11],
[1, 4, 7, 9], [1, 5, 6, 10], [2, 3, 7, 10], [2, 4, 6, 11], [2, 5, 8, 9],
[3, 4, 5, 12], [6, 7, 8, 12], [9, 10, 11, 12]]
`K_4`-decompositions of `K_v`
-----------------------------
Decompositions of `K_v` into `K_4` (i.e. `(v,4,1)`-BIBD) are built following
Douglas Stinson's construction as presented in [Stinson2004]_ page 167. It is
based upon the construction of `(v\{4,5,8,9,12\})`-PBD (see the doc of
:meth:`PBD_4_5_8_9_12`), knowing that a `(v\{4,5,8,9,12\})`-PBD on `v` points
can always be transformed into a `((k-1)v+1,4,1)`-BIBD, which covers all
possible cases of `(v,4,1)`-BIBD.
`K_5`-decompositions of `K_v`
-----------------------------
Decompositions of `K_v` into `K_4` (i.e. `(v,4,1)`-BIBD) are built following
Clayton Smith's construction [ClaytonSmith]_.
.. [ClaytonSmith] On the existence of `(v,5,1)`-BIBD.
http://www.argilo.net/files/bibd.pdf
Clayton Smith
Functions
---------
"""
from sage.categories.sets_cat import EmptySetError
from sage.misc.unknown import Unknown
from design_catalog import transversal_design
from block_design import BlockDesign
from sage.rings.arith import binomial
from sage.rings.arith import is_prime_power
def BalancedIncompleteBlockDesign(v,k,existence=False,use_LJCR=False):
r"""
Returns a BIBD of parameters `v,k`.
A Balanced Incomplete Block Design of parameters `v,k` is a collection
`\mathcal C` of `k`-subsets of `V=\{0,\dots,v-1\}` such that for any two
distinct elements `x,y\in V` there is a unique element `S\in \mathcal C`
such that `x,y\in S`.
More general definitions sometimes involve a `\lambda` parameter, and we
assume here that `\lambda=1`.
For more information on BIBD, see the
:wikipedia:`corresponding Wikipedia entry <Block_design#Definition_of_a_BIBD_.28or_2-design.29>`.
INPUT:
- ``v,k`` (integers)
- ``existence`` (boolean) -- instead of building the design, returns:
- ``True`` -- meaning that Sage knows how to build the design
- ``Unknown`` -- meaning that Sage does not know how to build the
design, but that the design may exist (see :mod:`sage.misc.unknown`).
- ``False`` -- meaning that the design does not exist.
- ``use_LJCR`` (boolean) -- whether to query the La Jolla Covering
Repository for the design when Sage does not know how to build it (see
:meth:`~sage.combinat.designs.covering_design.best_known_covering_design_www`). This
requires internet.
.. SEEALSO::
* :func:`steiner_triple_system`
* :func:`v_4_1_BIBD`
* :func:`v_5_1_BIBD`
TODO:
* Implement other constructions from the Handbook of Combinatorial
Designs.
EXAMPLES::
sage: designs.BalancedIncompleteBlockDesign(7,3).blocks()
[[0, 1, 3], [0, 2, 4], [0, 5, 6], [1, 2, 6], [1, 4, 5], [2, 3, 5], [3, 4, 6]]
sage: B = designs.BalancedIncompleteBlockDesign(21,5, use_LJCR=True) # optional - internet
sage: B # optional - internet
Incidence structure with 21 points and 21 blocks
sage: B.blocks() # optional - internet
[[0, 1, 2, 3, 20], [0, 4, 8, 12, 16], [0, 5, 10, 15, 19],
[0, 6, 11, 13, 17], [0, 7, 9, 14, 18], [1, 4, 11, 14, 19],
[1, 5, 9, 13, 16], [1, 6, 8, 15, 18], [1, 7, 10, 12, 17],
[2, 4, 9, 15, 17], [2, 5, 11, 12, 18], [2, 6, 10, 14, 16],
[2, 7, 8, 13, 19], [3, 4, 10, 13, 18], [3, 5, 8, 14, 17],
[3, 6, 9, 12, 19], [3, 7, 11, 15, 16], [4, 5, 6, 7, 20],
[8, 9, 10, 11, 20], [12, 13, 14, 15, 20], [16, 17, 18, 19, 20]]
sage: designs.BalancedIncompleteBlockDesign(20,5, use_LJCR=True) # optional - internet
Traceback (most recent call last):
...
ValueError: No such design exists !
sage: designs.BalancedIncompleteBlockDesign(16,6)
Traceback (most recent call last):
...
NotImplementedError: I don't know how to build a BIBD(16,6,1)!
TESTS::
sage: designs.BalancedIncompleteBlockDesign(85,5,existence=True)
True
sage: _ = designs.BalancedIncompleteBlockDesign(85,5)
A BIBD from a Finite Projective Plane::
sage: _ = designs.BalancedIncompleteBlockDesign(21,5)
Some trivial BIBD::
sage: designs.BalancedIncompleteBlockDesign(10,10)
Incidence structure with 10 points and 1 blocks
sage: designs.BalancedIncompleteBlockDesign(1,10)
Incidence structure with 1 points and 0 blocks
Existence of BIBD with `k=3,4,5`::
sage: [v for v in xrange(50) if designs.BalancedIncompleteBlockDesign(v,3,existence=True)]
[1, 3, 7, 9, 13, 15, 19, 21, 25, 27, 31, 33, 37, 39, 43, 45, 49]
sage: [v for v in xrange(100) if designs.BalancedIncompleteBlockDesign(v,4,existence=True)]
[1, 4, 13, 16, 25, 28, 37, 40, 49, 52, 61, 64, 73, 76, 85, 88, 97]
sage: [v for v in xrange(150) if designs.BalancedIncompleteBlockDesign(v,5,existence=True)]
[1, 5, 21, 25, 41, 45, 61, 65, 81, 85, 101, 105, 121, 125, 141, 145]
For `k > 5` there are currently very few constructions::
sage: [v for v in xrange(150) if designs.BalancedIncompleteBlockDesign(v,6,existence=True) is True]
[1, 6, 31]
sage: [v for v in xrange(150) if designs.BalancedIncompleteBlockDesign(v,6,existence=True) is Unknown]
[16, 21, 36, 46, 51, 61, 66, 76, 81, 91, 96, 106, 111, 121, 126, 136, 141]
"""
if v == 1:
if existence:
return True
return BlockDesign(v, [], test=False)
if k == v:
if existence:
return True
return BlockDesign(v, [range(v)], test=False)
if v < k or k < 2 or (v-1) % (k-1) != 0 or (v*(v-1)) % (k*(k-1)) != 0:
if existence:
return False
raise EmptySetError("No such design exists !")
if k == 2:
if existence:
return True
from itertools import combinations
return BlockDesign(v, combinations(range(v),2), test = False)
if k == 3:
if existence:
return v%6 == 1 or v%6 == 3
return steiner_triple_system(v)
if k == 4:
if existence:
return v%12 == 1 or v%12 == 4
return BlockDesign(v, v_4_1_BIBD(v), test = False)
if k == 5:
if existence:
return v%20 == 1 or v%20 == 5
return BlockDesign(v, v_5_1_BIBD(v), test = False)
if BIBD_from_TD(v,k,existence=True):
if existence:
return True
return BlockDesign(v, BIBD_from_TD(v,k))
if v == (k-1)**2+k and is_prime_power(k-1):
if existence:
return True
from block_design import projective_plane
return projective_plane(k-1)
if use_LJCR:
from covering_design import best_known_covering_design_www
B = best_known_covering_design_www(v,k,2)
# Is it a BIBD or just a good covering ?
expected_n_of_blocks = binomial(v,2)/binomial(k,2)
if B.low_bd() > expected_n_of_blocks:
if existence:
return False
raise EmptySetError("No such design exists !")
B = B.incidence_structure()
if len(B.blcks) == expected_n_of_blocks:
if existence:
return True
else:
return B
if existence:
return Unknown
else:
raise NotImplementedError("I don't know how to build a BIBD({},{},1)!".format(v,k))
def steiner_triple_system(n):
r"""
Returns a Steiner Triple System
A Steiner Triple System (STS) of a set `\{0,...,n-1\}`
is a family `S` of 3-sets such that for any `i \not = j`
there exists exactly one set of `S` in which they are
both contained.
It can alternatively be thought of as a factorization of
the complete graph `K_n` with triangles.
A Steiner Triple System of a `n`-set exists if and only if
`n \equiv 1 \pmod 6` or `n \equiv 3 \pmod 6`, in which case
one can be found through Bose's and Skolem's constructions,
respectively [AndHonk97]_.
INPUT:
- ``n`` returns a Steiner Triple System of `\{0,...,n-1\}`
EXAMPLE:
A Steiner Triple System on `9` elements ::
sage: sts = designs.steiner_triple_system(9)
sage: sts
Incidence structure with 9 points and 12 blocks
sage: list(sts)
[[0, 1, 5], [0, 2, 4], [0, 3, 6], [0, 7, 8], [1, 2, 3], [1, 4, 7], [1, 6, 8], [2, 5, 8], [2, 6, 7], [3, 4, 8], [3, 5, 7], [4, 5, 6]]
As any pair of vertices is covered once, its parameters are ::
sage: sts.parameters(t=2)
(2, 9, 3, 1)
An exception is raised for invalid values of ``n`` ::
sage: designs.steiner_triple_system(10)
Traceback (most recent call last):
...
EmptySetError: Steiner triple systems only exist for n = 1 mod 6 or n = 3 mod 6
REFERENCE:
.. [AndHonk97] A short course in Combinatorial Designs,
Ian Anderson, Iiro Honkala,
Internet Editions, Spring 1997,
http://www.utu.fi/~honkala/designs.ps
"""
name = "Steiner Triple System on "+str(n)+" elements"
if n%6 == 3:
t = (n-3) // 6
Z = range(2*t+1)
T = lambda (x,y) : x + (2*t+1)*y
sts = [[(i,0),(i,1),(i,2)] for i in Z] + \
[[(i,k),(j,k),(((t+1)*(i+j)) % (2*t+1),(k+1)%3)] for k in range(3) for i in Z for j in Z if i != j]
elif n%6 == 1:
t = (n-1) // 6
N = range(2*t)
T = lambda (x,y) : x+y*t*2 if (x,y) != (-1,-1) else n-1
L1 = lambda i,j : (i+j) % ((n-1)//3)
L = lambda i,j : L1(i,j)//2 if L1(i,j)%2 == 0 else t+(L1(i,j)-1)//2
sts = [[(i,0),(i,1),(i,2)] for i in range(t)] + \
[[(-1,-1),(i,k),(i-t,(k+1) % 3)] for i in range(t,2*t) for k in [0,1,2]] + \
[[(i,k),(j,k),(L(i,j),(k+1) % 3)] for k in [0,1,2] for i in N for j in N if i < j]
else:
raise EmptySetError("Steiner triple systems only exist for n = 1 mod 6 or n = 3 mod 6")
from sage.sets.set import Set
sts = Set(map(lambda x: Set(map(T,x)),sts))
return BlockDesign(n, sts, name=name)
def BIBD_from_TD(v,k,existence=False):
r"""
Returns a BIBD through TD-based constructions.
INPUT:
- ``v,k`` (integers) -- computes a `(v,k,1)`-BIBD.
- ``existence`` (boolean) -- instead of building the design, returns:
- ``True`` -- meaning that Sage knows how to build the design
- ``Unknown`` -- meaning that Sage does not know how to build the
design, but that the design may exist (see :mod:`sage.misc.unknown`).
- ``False`` -- meaning that the design does not exist.
This method implements three constructions:
- If there exists a `TD(k,v)` and a `(v,k,1)`-BIBD then there exists a
`(kv,k,1)`-BIBD.
The BIBD is obtained from all blocks of the `TD`, and from the blocks of
the `(v,k,1)`-BIBDs defined over the `k` groups of the `TD`.
- If there exists a `TD(k,v)` and a `(v+1,k,1)`-BIBD then there exists a
`(kv+1,k,1)`-BIBD.
The BIBD is obtained from all blocks of the `TD`, and from the blocks of
the `(v+1,k,1)`-BIBDs defined over the sets `V_1\cup \infty,\dots,V_k\cup
\infty` where the `V_1,\dots,V_k` are the groups of the TD.
- If there exists a `TD(k,v)` and a `(v+k,k,1)`-BIBD then there exists a
`(kv+k,k,1)`-BIBD.
The BIBD is obtained from all blocks of the `TD`, and from the blocks of
the `(v+k,k,1)`-BIBDs defined over the sets `V_1\cup
\{\infty_1,\dots,\infty_k\},\dots,V_k\cup \{\infty_1,\dots,\infty_k\}`
where the `V_1,\dots,V_k` are the groups of the TD. By making sure that
all copies of the `(v+k,k,1)`-BIBD contain the block
`\{\infty_1,\dots,\infty_k\}`, the result is also a BIBD.
These constructions can be found in
`<http://www.argilo.net/files/bibd.pdf>`_.
EXAMPLES:
First construction::
sage: from sage.combinat.designs.bibd import BIBD_from_TD
sage: BIBD_from_TD(25,5,existence=True)
True
sage: _ = BlockDesign(25,BIBD_from_TD(25,5))
Second construction::
sage: from sage.combinat.designs.bibd import BIBD_from_TD
sage: BIBD_from_TD(21,5,existence=True)
True
sage: _ = BlockDesign(21,BIBD_from_TD(21,5))
Third construction::
sage: from sage.combinat.designs.bibd import BIBD_from_TD
sage: BIBD_from_TD(85,5,existence=True)
True
sage: _ = BlockDesign(85,BIBD_from_TD(85,5))
No idea::
sage: from sage.combinat.designs.bibd import BIBD_from_TD
sage: BIBD_from_TD(20,5,existence=True)
Unknown
sage: BIBD_from_TD(20,5)
Traceback (most recent call last):
...
NotImplementedError: I do not know how to build a BIBD(20,5,1)!
"""
from orthogonal_arrays import transversal_design
# First construction
if (v%k == 0 and
BalancedIncompleteBlockDesign(v//k,k,existence=True) and
transversal_design(k,v//k,existence=True)):
if existence:
return True
v = v//k
BIBDvk = BalancedIncompleteBlockDesign(v,k)
TDkv = transversal_design(k,v,check=False)
BIBD = TDkv
for i in range(k):
BIBD.extend([[x+i*v for x in B] for B in BIBDvk])
# Second construction
elif ((v-1)%k == 0 and
BalancedIncompleteBlockDesign((v-1)//k+1,k,existence=True) and
transversal_design(k,(v-1)//k,existence=True)):
if existence:
return True
v = (v-1)//k
BIBDv1k = BalancedIncompleteBlockDesign(v+1,k)
TDkv = transversal_design(k,v,check=False)
inf = v*k
BIBD = TDkv
for i in range(k):
BIBD.extend([[inf if x == v else x+i*v for x in B] for B in BIBDv1k])
# Third construction
elif ((v-k)%k == 0 and
BalancedIncompleteBlockDesign((v-k)//k+k,k,existence=True) and
transversal_design(k,(v-k)//k,existence=True)):
if existence:
return True
v = (v-k)//k
BIBDvpkk = BalancedIncompleteBlockDesign(v+k,k)
TDkv = transversal_design(k,v,check=False)
inf = v*k
BIBD = TDkv
# makes sure that [v,...,v+k-1] is a block of BIBDvpkk. Then, we remove it.
BIBDvpkk = _relabel_bibd(BIBDvpkk,v+k)
BIBDvpkk = [B for B in BIBDvpkk if min(B) < v]
for i in range(k):
BIBD.extend([[(x-v)+inf if x >= v else x+i*v for x in B] for B in BIBDvpkk])
BIBD.append(range(k*v,v*k+k))
# No idea ...
else:
if existence:
return Unknown
else:
raise NotImplementedError("I do not know how to build a BIBD({},{},1)!".format(v,k))
return BIBD
def BIBD_from_difference_family(G, D, check=True):
r"""
Return the BIBD associated to the difference family ``D`` on the group ``G``.
Let `G` be a finite Abelian group. A *simple `(G,k)`-difference family* (or
a *`(G,k,1)`-difference family*) is a family `B = \{B_1,B_2,\ldots,B_b\}` of
`k`-subsets of `G` such that for each element of `G \backslash \{0\}` there
exists a unique `s \in \{1,\ldots,b\}` and a unique pair of distinct
elements `x,y \in B_s` such that `x - y = g`.
If `\{B_1, B_2, \ldots, B_b\}` is a simple `(G,k)`-difference family then
its set of translates `\{B_i + g; i \in \{1,\ldots,b\}, g \in G\}` is a
`(v,k,1)`-BIBD where `v` is the cardinality of `G`.
INPUT::
- ``G`` - a finite Abelian group
- ``D`` - a difference family on ``G``.
- ``check`` - whether or not we check the output (default: ``True``)
EXAMPLES::
sage: G = Zmod(21)
sage: D = [[0,1,4,14,16]]
sage: print sorted(G(x-y) for x in D[0] for y in D[0] if x != y)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
sage: from sage.combinat.designs.bibd import BIBD_from_difference_family
sage: BIBD_from_difference_family(G, D)
[[0, 1, 4, 14, 16],
[1, 2, 5, 15, 17],
[2, 3, 6, 16, 18],
[3, 4, 7, 17, 19],
[4, 5, 8, 18, 20],
[5, 6, 9, 19, 0],
[6, 7, 10, 20, 1],
[7, 8, 11, 0, 2],
[8, 9, 12, 1, 3],
[9, 10, 13, 2, 4],
[10, 11, 14, 3, 5],
[11, 12, 15, 4, 6],
[12, 13, 16, 5, 7],
[13, 14, 17, 6, 8],
[14, 15, 18, 7, 9],
[15, 16, 19, 8, 10],
[16, 17, 20, 9, 11],
[17, 18, 0, 10, 12],
[18, 19, 1, 11, 13],
[19, 20, 2, 12, 14],
[20, 0, 3, 13, 15]]
"""
r = {e:i for i,e in enumerate(G)}
bibd = [[r[G(x)+g] for x in d] for d in D for g in r]
if check:
assert _check_pbd(bibd, G.cardinality(), [len(D[0])])
return bibd
################
# (v,4,1)-BIBD #
################
def v_4_1_BIBD(v, check=True):
r"""
Returns a `(v,4,1)`-BIBD.
A `(v,4,1)`-BIBD is an edge-decomposition of the complete graph `K_v` into
copies of `K_4`. For more information, see
:meth:`BalancedIncompleteBlockDesign`. It exists if and only if `v\equiv 1,4
\pmod {12}`.
See page 167 of [Stinson2004]_ for the construction details.
.. SEEALSO::
* :meth:`BalancedIncompleteBlockDesign`
INPUT:
- ``v`` (integer) -- number of points.
- ``check`` (boolean) -- whether to check that output is correct before
returning it. As this is expected to be useless (but we are cautious
guys), you may want to disable it whenever you want speed. Set to ``True``
by default.
EXAMPLES::
sage: from sage.combinat.designs.bibd import v_4_1_BIBD # long time
sage: for n in range(13,100): # long time
....: if n%12 in [1,4]: # long time
....: _ = v_4_1_BIBD(n, check = True) # long time
"""
from sage.rings.finite_rings.constructor import FiniteField
k = 4
if v == 0:
return []
if v <= 12 or v%12 not in [1,4]:
raise EmptySetError("A K_4-decomposition of K_v exists iif v=2,4 mod 12, v>12 or v==0")
# Step 1. Base cases.
if v == 13:
from block_design import projective_plane
return projective_plane(3).blocks()
if v == 16:
from block_design import AffineGeometryDesign
return AffineGeometryDesign(2,1,FiniteField(4,'x')).blocks()
if v == 25:
return [[0, 1, 17, 22], [0, 2, 11, 21], [0, 3, 15, 18], [0, 4, 7, 13],
[0, 5, 12, 14], [0, 6, 19, 23], [0, 8, 16, 24], [0, 9, 10, 20],
[1, 2, 3, 4], [1, 5, 6, 7], [1, 8, 12, 15], [1, 9, 13, 16],
[1, 10, 11, 14], [1, 18, 20, 23], [1, 19, 21, 24], [2, 5, 15, 24],
[2, 6, 9, 17], [2, 7, 14, 18], [2, 8, 22, 23], [2, 10, 12, 13],
[2, 16, 19, 20], [3, 5, 16, 22], [3, 6, 11, 20], [3, 7, 12, 19],
[3, 8, 9, 14], [3, 10, 17, 24], [3, 13, 21, 23], [4, 5, 10, 23],
[4, 6, 8, 21], [4, 9, 18, 24], [4, 11, 15, 16], [4, 12, 17, 20],
[4, 14, 19, 22], [5, 8, 13, 20], [5, 9, 11, 19], [5, 17, 18, 21],
[6, 10, 15, 22], [6, 12, 16, 18], [6, 13, 14, 24], [7, 8, 11, 17],
[7, 9, 15, 23], [7, 10, 16, 21], [7, 20, 22, 24], [8, 10, 18, 19],
[9, 12, 21, 22], [11, 12, 23, 24], [11, 13, 18, 22], [13, 15, 17, 19],
[14, 15, 20, 21], [14, 16, 17, 23]]
if v == 28:
return [[0, 1, 23, 26], [0, 2, 10, 11], [0, 3, 16, 18], [0, 4, 15, 20],
[0, 5, 8, 9], [0, 6, 22, 25], [0, 7, 14, 21], [0, 12, 17, 27],
[0, 13, 19, 24], [1, 2, 24, 27], [1, 3, 11, 12], [1, 4, 17, 19],
[1, 5, 14, 16], [1, 6, 9, 10], [1, 7, 20, 25], [1, 8, 15, 22],
[1, 13, 18, 21], [2, 3, 21, 25], [2, 4, 12, 13], [2, 5, 18, 20],
[2, 6, 15, 17], [2, 7, 19, 22], [2, 8, 14, 26], [2, 9, 16, 23],
[3, 4, 22, 26], [3, 5, 7, 13], [3, 6, 14, 19], [3, 8, 20, 23],
[3, 9, 15, 27], [3, 10, 17, 24], [4, 5, 23, 27], [4, 6, 7, 8],
[4, 9, 14, 24], [4, 10, 16, 21], [4, 11, 18, 25], [5, 6, 21, 24],
[5, 10, 15, 25], [5, 11, 17, 22], [5, 12, 19, 26], [6, 11, 16, 26],
[6, 12, 18, 23], [6, 13, 20, 27], [7, 9, 17, 18], [7, 10, 26, 27],
[7, 11, 23, 24], [7, 12, 15, 16], [8, 10, 18, 19], [8, 11, 21, 27],
[8, 12, 24, 25], [8, 13, 16, 17], [9, 11, 19, 20], [9, 12, 21, 22],
[9, 13, 25, 26], [10, 12, 14, 20], [10, 13, 22, 23], [11, 13, 14, 15],
[14, 17, 23, 25], [14, 18, 22, 27], [15, 18, 24, 26], [15, 19, 21, 23],
[16, 19, 25, 27], [16, 20, 22, 24], [17, 20, 21, 26]]
if v == 37:
return [[0, 1, 3, 24], [0, 2, 23, 36], [0, 4, 26, 32], [0, 5, 9, 31],
[0, 6, 11, 15], [0, 7, 17, 25], [0, 8, 20, 27], [0, 10, 18, 30],
[0, 12, 19, 29], [0, 13, 14, 16], [0, 21, 34, 35], [0, 22, 28, 33],
[1, 2, 4, 25], [1, 5, 27, 33], [1, 6, 10, 32], [1, 7, 12, 16],
[1, 8, 18, 26], [1, 9, 21, 28], [1, 11, 19, 31], [1, 13, 20, 30],
[1, 14, 15, 17], [1, 22, 35, 36], [1, 23, 29, 34], [2, 3, 5, 26],
[2, 6, 28, 34], [2, 7, 11, 33], [2, 8, 13, 17], [2, 9, 19, 27],
[2, 10, 22, 29], [2, 12, 20, 32], [2, 14, 21, 31], [2, 15, 16, 18],
[2, 24, 30, 35], [3, 4, 6, 27], [3, 7, 29, 35], [3, 8, 12, 34],
[3, 9, 14, 18], [3, 10, 20, 28], [3, 11, 23, 30], [3, 13, 21, 33],
[3, 15, 22, 32], [3, 16, 17, 19], [3, 25, 31, 36], [4, 5, 7, 28],
[4, 8, 30, 36], [4, 9, 13, 35], [4, 10, 15, 19], [4, 11, 21, 29],
[4, 12, 24, 31], [4, 14, 22, 34], [4, 16, 23, 33], [4, 17, 18, 20],
[5, 6, 8, 29], [5, 10, 14, 36], [5, 11, 16, 20], [5, 12, 22, 30],
[5, 13, 25, 32], [5, 15, 23, 35], [5, 17, 24, 34], [5, 18, 19, 21],
[6, 7, 9, 30], [6, 12, 17, 21], [6, 13, 23, 31], [6, 14, 26, 33],
[6, 16, 24, 36], [6, 18, 25, 35], [6, 19, 20, 22], [7, 8, 10, 31],
[7, 13, 18, 22], [7, 14, 24, 32], [7, 15, 27, 34], [7, 19, 26, 36],
[7, 20, 21, 23], [8, 9, 11, 32], [8, 14, 19, 23], [8, 15, 25, 33],
[8, 16, 28, 35], [8, 21, 22, 24], [9, 10, 12, 33], [9, 15, 20, 24],
[9, 16, 26, 34], [9, 17, 29, 36], [9, 22, 23, 25], [10, 11, 13, 34],
[10, 16, 21, 25], [10, 17, 27, 35], [10, 23, 24, 26], [11, 12, 14, 35],
[11, 17, 22, 26], [11, 18, 28, 36], [11, 24, 25, 27], [12, 13, 15, 36],
[12, 18, 23, 27], [12, 25, 26, 28], [13, 19, 24, 28], [13, 26, 27, 29],
[14, 20, 25, 29], [14, 27, 28, 30], [15, 21, 26, 30], [15, 28, 29, 31],
[16, 22, 27, 31], [16, 29, 30, 32], [17, 23, 28, 32], [17, 30, 31, 33],
[18, 24, 29, 33], [18, 31, 32, 34], [19, 25, 30, 34], [19, 32, 33, 35],
[20, 26, 31, 35], [20, 33, 34, 36], [21, 27, 32, 36]]
# Step 2 : this is function PBD_4_5_8_9_12
PBD = PBD_4_5_8_9_12((v-1)/(k-1),check=False)
# Step 3 : Theorem 7.20
bibd = BIBD_from_PBD(PBD,v,k,check=False)
if check:
_check_pbd(bibd,v,[k])
return bibd
def BIBD_from_PBD(PBD,v,k,check=True,base_cases={}):
r"""
Returns a `(v,k,1)`-BIBD from a `(r,K)`-PBD where `r=(v-1)/(k-1)`.
This is Theorem 7.20 from [Stinson2004]_.
INPUT:
- ``v,k`` -- integers.
- ``PBD`` -- A PBD on `r=(v-1)/(k-1)` points, such that for any block of
``PBD`` of size `s` there must exist a `((k-1)s+1,k,1)`-BIBD.
- ``check`` (boolean) -- whether to check that output is correct before
returning it. As this is expected to be useless (but we are cautious
guys), you may want to disable it whenever you want speed. Set to ``True``
by default.
- ``base_cases`` -- caching system, for internal use.
EXAMPLES::
sage: from sage.combinat.designs.bibd import PBD_4_5_8_9_12
sage: from sage.combinat.designs.bibd import BIBD_from_PBD
sage: from sage.combinat.designs.bibd import _check_pbd
sage: PBD = PBD_4_5_8_9_12(17)
sage: bibd = _check_pbd(BIBD_from_PBD(PBD,52,4),52,[4])
"""
r = (v-1) // (k-1)
bibd = []
for X in PBD:
n = len(X)
N = (k-1)*n+1
if not (n,k) in base_cases:
base_cases[n,k] = _relabel_bibd(BalancedIncompleteBlockDesign(N,k).blcks,N)
for XX in base_cases[n,k]:
if N-1 in XX:
continue
bibd.append([X[x//(k-1)] + (x%(k-1))*r for x in XX])
for x in range(r):
bibd.append([x+i*r for i in range(k-1)]+[v-1])
if check:
_check_pbd(bibd,v,[k])
return bibd
def _check_pbd(B,v,S):
r"""
Checks that ``B`` is a PBD on `v` points with given block sizes.
INPUT:
- ``bibd`` -- a list of blocks
- ``v`` (integer) -- number of points
- ``S`` -- list of integers
EXAMPLE::
sage: designs.BalancedIncompleteBlockDesign(40,4).blocks() # indirect doctest
[[0, 1, 2, 12], [0, 3, 6, 9], [0, 4, 8, 10],
[0, 5, 7, 11], [0, 13, 26, 39], [0, 14, 25, 28],
[0, 15, 27, 38], [0, 16, 22, 32], [0, 17, 23, 34],
...
"""
from itertools import combinations
from sage.graphs.graph import Graph
if not all(len(X) in S for X in B):
raise RuntimeError("This is not a nice honest PBD from the good old days !")
g = Graph()
m = 0
for X in B:
g.add_edges(list(combinations(X,2)))
if g.size() != m+binomial(len(X),2):
raise RuntimeError("This is not a nice honest PBD from the good old days !")
m = g.size()
if not (g.is_clique() and g.vertices() == range(v)):
raise RuntimeError("This is not a nice honest PBD from the good old days !")
return B
def _relabel_bibd(B,n,p=None):
r"""
Relabels the BIBD on `n` points and blocks of size k such that
`\{0,...,k-2,n-1\},\{k-1,...,2k-3,n-1\},...,\{n-k,...,n-2,n-1\}` are blocks
of the BIBD.
INPUT:
- ``B`` -- a list of blocks.
- ``n`` (integer) -- number of points.
- ``p`` (optional) -- the point that will be labeled with n-1.
EXAMPLE::
sage: designs.BalancedIncompleteBlockDesign(40,4).blocks() # indirect doctest
[[0, 1, 2, 12], [0, 3, 6, 9], [0, 4, 8, 10],
[0, 5, 7, 11], [0, 13, 26, 39], [0, 14, 25, 28],
[0, 15, 27, 38], [0, 16, 22, 32], [0, 17, 23, 34],
...
"""
if p is None:
p = n-1
found = 0
last = n-1
d = {}
for X in B:
if last in X:
for x in X:
if x == last:
continue
d[x] = found
found += 1
if found == n-1:
break
d[p] = n-1
return [[d[x] for x in X] for X in B]
def PBD_4_5_8_9_12(v, check=True):
"""
Returns a `(v,\{4,5,8,9,12\})-`PBD on `v` elements.
A `(v,\{4,5,8,9,12\})`-PBD exists if and only if `v\equiv 0,1 \pmod 4`. The
construction implemented here appears page 168 in [Stinson2004]_.
INPUT:
- ``v`` (integer)
- ``check`` (boolean) -- whether to check that output is correct before
returning it. As this is expected to be useless (but we are cautious
guys), you may want to disable it whenever you want speed. Set to ``True``
by default.
EXAMPLES::
sage: designs.BalancedIncompleteBlockDesign(40,4).blocks() # indirect doctest
[[0, 1, 2, 12], [0, 3, 6, 9], [0, 4, 8, 10],
[0, 5, 7, 11], [0, 13, 26, 39], [0, 14, 25, 28],
[0, 15, 27, 38], [0, 16, 22, 32], [0, 17, 23, 34],
...
"""
if not v%4 in [0,1]:
raise ValueError
if v == 0:
return []
if v == 13:
PBD = v_4_1_BIBD(v, check=False)
elif v == 28:
PBD = v_4_1_BIBD(v, check=False)
elif v == 29:
TD47 = transversal_design(4,7)
four_more_sets = [[28]+[i*7+j for j in range(7)] for i in range(4)]
PBD = TD47 + four_more_sets
elif v == 41:
TD59 = transversal_design(5,9)
PBD = ([[x for x in X if x<41] for X in TD59]
+[[i*9+j for j in range(9)] for i in range(4)]
+[[36,37,38,39,40]])
elif v == 44:
TD59 = transversal_design(5,9)
PBD = ([[x for x in X if x<44] for X in TD59]
+[[i*9+j for j in range(9)] for i in range(4)]
+[[36,37,38,39,40,41,42,43]])
elif v == 45:
TD59 = transversal_design(5,9)
PBD = (TD59+[[i*9+j for j in range(9)] for i in range(5)])
elif v == 48:
TD4_12 = transversal_design(4,12)
PBD = (TD4_12+[[i*12+j for j in range(12)] for i in range(4)])
elif v == 49:
# Lemma 7.16 : A (49,{4,13})-PBD
TD4_12 = transversal_design(4,12)
# Replacing the block of size 13 with a BIBD
BIBD_13_4 = v_4_1_BIBD(13)
for i in range(4):
for B in BIBD_13_4:
TD4_12.append([i*12+x if x != 12 else 48
for x in B])
PBD = TD4_12
else:
t,u = _get_t_u(v)
TD = transversal_design(5,t)
TD = [[x for x in X if x<4*t+u] for X in TD]
for B in [range(t*i,t*(i+1)) for i in range(4)]:
TD.extend(_PBD_4_5_8_9_12_closure([B]))
if u > 1:
TD.extend(_PBD_4_5_8_9_12_closure([range(4*t,4*t+u)]))
PBD = TD
if check:
_check_pbd(PBD,v,[4,5,8,9,12])
return PBD
def _PBD_4_5_8_9_12_closure(B):
r"""
Makes sure all blocks of `B` have size in `\{4,5,8,9,12\}`.
This is a helper function for :meth:`PBD_4_5_8_9_12`. Given that
`\{4,5,8,9,12\}` is PBD-closed, any block of size not in `\{4,5,8,9,12\}`
can be decomposed further.
EXAMPLES::
sage: designs.BalancedIncompleteBlockDesign(40,4).blocks() # indirect doctest
[[0, 1, 2, 12], [0, 3, 6, 9], [0, 4, 8, 10],
[0, 5, 7, 11], [0, 13, 26, 39], [0, 14, 25, 28],
[0, 15, 27, 38], [0, 16, 22, 32], [0, 17, 23, 34],
...
"""
BB = []
for X in B:
if len(X) not in [4,5,8,9,12]:
PBD = PBD_4_5_8_9_12(len(X), check = False)
X = [[X[i] for i in XX] for XX in PBD]
BB.extend(X)
else:
BB.append(X)
return BB
table_7_1 = {
0:{'t':-4,'u':16,'s':2},
1:{'t':-4,'u':17,'s':2},
4:{'t':1,'u':0,'s':1},
5:{'t':1,'u':1,'s':1},
8:{'t':1,'u':4,'s':1},
9:{'t':1,'u':5,'s':1},
12:{'t':1,'u':8,'s':1},
13:{'t':1,'u':9,'s':1},
16:{'t':4,'u':0,'s':0},
17:{'t':4,'u':1,'s':0},
20:{'t':5,'u':0,'s':0},
21:{'t':5,'u':1,'s':0},
24:{'t':5,'u':4,'s':0},
25:{'t':5,'u':5,'s':0},
28:{'t':5,'u':8,'s':1},
29:{'t':5,'u':9,'s':1},
32:{'t':8,'u':0,'s':0},
33:{'t':8,'u':1,'s':0},
36:{'t':8,'u':4,'s':0},
37:{'t':8,'u':5,'s':0},
40:{'t':8,'u':8,'s':0},
41:{'t':8,'u':9,'s':1},
44:{'t':8,'u':12,'s':1},
45:{'t':8,'u':13,'s':1},
}
def _get_t_u(v):
r"""
Returns the parameters of table 7.1 from [Stinson2004]_.
INPUT:
- ``v`` (integer)
EXAMPLE::
sage: from sage.combinat.designs.bibd import _get_t_u
sage: _get_t_u(20)
(5, 0)
"""
# Table 7.1
v = int(v)
global table_7_1
d = table_7_1[v%48]
s = v//48
if s < d['s']:
raise RuntimeError("This should not have happened.")
t = 12*s+d['t']
u = d['u']
return t,u
################
# (v,5,1)-BIBD #
################
def v_5_1_BIBD(v, check=True):
r"""
Returns a `(v,5,1)`-BIBD.
This method follows the constuction from [ClaytonSmith]_.
INPUT:
- ``v`` (integer)
.. SEEALSO::
* :meth:`BalancedIncompleteBlockDesign`
EXAMPLES::
sage: from sage.combinat.designs.bibd import v_5_1_BIBD
sage: i = 0
sage: while i<200:
....: i += 20
....: _ = v_5_1_BIBD(i+1)
....: _ = v_5_1_BIBD(i+5)
"""
v = int(v)
assert (v > 1)
assert (v%20 == 5 or v%20 == 1) # note: equivalent to (v-1)%4 == 0 and (v*(v-1))%20 == 0
# Lemma 27
if v%5 == 0 and (v//5)%4 == 1 and is_prime_power(v//5):
bibd = BIBD_5q_5_for_q_prime_power(v//5)
# Lemma 28
elif v == 21:
from sage.rings.finite_rings.integer_mod_ring import Zmod
bibd = BIBD_from_difference_family(Zmod(21), [[0,1,4,14,16]], check=False)
elif v == 41:
from sage.rings.finite_rings.integer_mod_ring import Zmod
bibd = BIBD_from_difference_family(Zmod(41), [[0,1,4,11,29],[0,2,8,17,22]], check=False)
elif v == 61:
from sage.rings.finite_rings.integer_mod_ring import Zmod
bibd = BIBD_from_difference_family(Zmod(61), [[0,1,3,13,34],[0,4,9,23,45],[0,6,17,24,32]], check=False)
elif v == 81:
from sage.groups.additive_abelian.additive_abelian_group import AdditiveAbelianGroup
D = [[(0, 0, 0, 1), (2, 0, 0, 1), (0, 0, 2, 1), (1, 2, 0, 2), (0, 1, 1, 1)],
[(0, 0, 1, 0), (1, 1, 0, 2), (0, 2, 1, 0), (1, 2, 0, 1), (1, 1, 1, 0)],
[(2, 2, 1, 1), (1, 2, 2, 2), (2, 0, 1, 2), (0, 1, 2, 1), (1, 1, 0, 0)],
[(0, 2, 0, 2), (1, 1, 0, 1), (1, 2, 1, 2), (1, 2, 1, 0), (0, 2, 1, 1)]]
bibd = BIBD_from_difference_family(AdditiveAbelianGroup([3]*4), D, check=False)
elif v == 161:
# VI.16.16 of the Handbook of Combinatorial Designs, Second Edition
D = [(0, 19, 34, 73, 80), (0, 16, 44, 71, 79), (0, 12, 33, 74, 78), (0, 13, 30, 72, 77), (0, 11, 36, 67, 76), (0, 18, 32, 69, 75), (0, 10, 48, 68, 70), (0, 3, 29, 52, 53)]
from sage.rings.finite_rings.integer_mod_ring import Zmod
bibd = BIBD_from_difference_family(Zmod(161), D, check=False)
elif v == 281:
from sage.rings.finite_rings.integer_mod_ring import Zmod
D = [[3**(2*a+56*b) for b in range(5)] for a in range(14)]
bibd = BIBD_from_difference_family(Zmod(281), D, check=False)
# Lemma 29
elif v == 165:
bibd = BIBD_from_PBD(v_5_1_BIBD(41,check=False),165,5,check=False)
elif v == 181:
bibd = BIBD_from_PBD(v_5_1_BIBD(45,check=False),181,5,check=False)
elif v in (201,285,301,401,421,425):
# Call directly the BIBD_from_TD function
bibd = BIBD_from_TD(v,5)
# Lemma 30
elif v == 141:
# VI.16.16 of the Handbook of Combinatorial Designs, Second Edition
from sage.rings.finite_rings.integer_mod_ring import Zmod
D = [(0, 33, 60, 92, 97), (0, 3, 45, 88, 110), (0, 18, 39, 68, 139), (0, 12, 67, 75, 113), (0, 1, 15, 84, 94), (0, 7, 11, 24, 30), (0, 36, 90, 116, 125)]
bibd = BIBD_from_difference_family(Zmod(141), D, check=False)
# Theorem 31.2
elif (v-1)//4 in [80, 81, 85, 86, 90, 91, 95, 96, 110, 111, 115, 116, 120, 121, 250, 251, 255, 256, 260, 261, 265, 266, 270, 271]:
r = (v-1)//4
if r <= 96:
k,t,u = 5, 16, r-80