This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
/
incidence_structures.py
1221 lines (967 loc) · 44.5 KB
/
incidence_structures.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
"""
Incidence structures (i.e. hypergraphs, i.e. set systems)
An incidence structure is specified by a list of points, blocks, and
an incidence matrix ([1]_, [2]_).
REFERENCES:
.. [1] Block designs and incidence structures from wikipedia,
:wikipedia:`Block_design`
:wikipedia:`Incidence_structure`
.. [2] E. Assmus, J. Key, Designs and their codes, CUP, 1992.
AUTHORS:
- Peter Dobcsanyi and David Joyner (2007-2008)
This is a significantly modified form of part of the module block_design.py
(version 0.6) written by Peter Dobcsanyi peter@designtheory.org.
- Vincent Delecroix (2014): major rewrite
Methods
-------
"""
#***************************************************************************
# Copyright (C) 2007 #
# #
# Peter Dobcsanyi and David Joyner #
# <peter@designtheory.org> <wdjoyner@gmail.com> #
# #
# #
# Distributed under the terms of the GNU General Public License (GPL) #
# as published by the Free Software Foundation; either version 2 of #
# the License, or (at your option) any later version. #
# http://www.gnu.org/licenses/ #
#***************************************************************************
from sage.misc.superseded import deprecated_function_alias
from sage.misc.cachefunc import cached_method
from sage.rings.all import ZZ
from sage.rings.integer import Integer
from sage.misc.latex import latex
from sage.sets.set import Set
def IncidenceStructureFromMatrix(M, name=None):
"""
Deprecated function that builds an incidence structure from a matrix.
You should now use ``designs.IncidenceStructure(incidence_matrix=M)``.
INPUT:
- ``M`` -- a binary matrix. Creates a set of "points" from the rows and a
set of "blocks" from the columns.
EXAMPLES::
sage: BD1 = designs.IncidenceStructure(7,[[0,1,2],[0,3,4],[0,5,6],[1,3,5],[1,4,6],[2,3,6],[2,4,5]])
sage: M = BD1.incidence_matrix()
sage: BD2 = IncidenceStructureFromMatrix(M)
doctest:...: DeprecationWarning: IncidenceStructureFromMatrix is deprecated.
Please use designs.IncidenceStructure(incidence_matrix=M) instead.
See http://trac.sagemath.org/16553 for details.
sage: BD1 == BD2
True
"""
from sage.misc.superseded import deprecation
deprecation(16553, 'IncidenceStructureFromMatrix is deprecated. Please use designs.IncidenceStructure(incidence_matrix=M) instead.')
return IncidenceStructure(incidence_matrix=M, name=name)
class IncidenceStructure(object):
r"""
A base class for incidence structures (i.e. hypergraphs, i.e. set systems)
An incidence structure (i.e. hypergraph, i.e. set system) can be defined
from a collection of blocks (i.e. sets, i.e. edges), optionally with an
explicit ground set (i.e. point set, i.e. vertex set). Alternatively they
can be defined from a binary incidence matrix.
INPUT:
- ``points`` -- (i.e. ground set, i.e. vertex set) the underlying set. If
``points`` is an integer `v`, then the set is considered to be `\{0, ...,
v-1\}`.
.. NOTE::
The following syntax, where ``points`` is ommitted, automatically
defines the ground set as the union of the blocks::
sage: H = IncidenceStructure([['a','b','c'],['c','d','e']])
sage: H.ground_set()
['a', 'b', 'c', 'd', 'e']
- ``blocks`` -- (i.e. edges, i.e. sets) the blocks defining the incidence
structure. Can be any iterable.
- ``incidence_matrix`` -- a binary incidence matrix. Each column represents
a set.
- ``name`` (a string, such as "Fano plane").
- ``check`` -- whether to check the input
- ``copy`` -- (use with caution) if set to ``False`` then ``blocks`` must be
a list of lists of integers. The list will not be copied but will be
modified in place (each block is sorted, and the whole list is
sorted). Your ``blocks`` object will become the
:class:`IncidenceStructure` instance's internal data.
EXAMPLES:
An incidence structure can be constructed by giving the number of points and
the list of blocks::
sage: designs.IncidenceStructure(7, [[0,1,2],[0,3,4],[0,5,6],[1,3,5],[1,4,6],[2,3,6],[2,4,5]])
Incidence structure with 7 points and 7 blocks
Only providing the set of blocks is sufficient. In this case, the ground set
is defined as the union of the blocks::
sage: IncidenceStructure([[1,2,3],[2,3,4]])
Incidence structure with 4 points and 2 blocks
Or by its adjacency matrix (a `\{0,1\}`-matrix in which rows are indexed by
points and columns by blocks)::
sage: m = matrix([[0,1,0],[0,0,1],[1,0,1],[1,1,1]])
sage: designs.IncidenceStructure(m)
Incidence structure with 4 points and 3 blocks
The points can be any (hashable) object::
sage: V = [(0,'a'),(0,'b'),(1,'a'),(1,'b')]
sage: B = [(V[0],V[1],V[2]), (V[1],V[2]), (V[0],V[2])]
sage: I = designs.IncidenceStructure(V, B)
sage: I.ground_set()
[(0, 'a'), (0, 'b'), (1, 'a'), (1, 'b')]
sage: I.blocks()
[[(0, 'a'), (0, 'b'), (1, 'a')], [(0, 'a'), (1, 'a')], [(0, 'b'), (1, 'a')]]
The order of the points and blocks does not matter as they are sorted on
input (see :trac:`11333`)::
sage: A = designs.IncidenceStructure([0,1,2], [[0],[0,2]])
sage: B = designs.IncidenceStructure([1,0,2], [[0],[2,0]])
sage: B == A
True
sage: C = designs.BlockDesign(2, [[0], [1,0]])
sage: D = designs.BlockDesign(2, [[0,1], [0]])
sage: C == D
True
If you care for speed, you can set ``copy`` to ``False``, but in that
case, your input must be a list of lists and the ground set must be `{0,
..., v-1}`::
sage: blocks = [[0,1],[2,0],[1,2]] # a list of lists of integers
sage: I = designs.IncidenceStructure(3, blocks, copy=False)
sage: I.blocks(copy=False) is blocks
True
"""
def __init__(self, points=None, blocks=None, incidence_matrix=None,
name=None, check=True, test=None, copy=True):
r"""
TESTS::
sage: designs.IncidenceStructure(3, [[4]])
Traceback (most recent call last):
...
ValueError: Block [4] is not contained in the point set
sage: designs.IncidenceStructure(3, [[0,1],[0,2]], test=True)
doctest:...: DeprecationWarning: the keyword test is deprecated,
use check instead
See http://trac.sagemath.org/16553 for details.
Incidence structure with 3 points and 2 blocks
sage: designs.IncidenceStructure(2, [[0,1,2,3,4,5]], test=False)
Incidence structure with 2 points and 1 blocks
We avoid to convert to integers when the points are not (but compare
equal to integers because of coercion)::
sage: V = GF(5)
sage: e0,e1,e2,e3,e4 = V
sage: [e0,e1,e2,e3,e4] == range(5) # coercion makes them equal
True
sage: blocks = [[e0,e1,e2],[e0,e1],[e2,e4]]
sage: I = designs.IncidenceStructure(V, blocks)
sage: type(I.ground_set()[0])
<type 'sage.rings.finite_rings.integer_mod.IntegerMod_int'>
sage: type(I.blocks()[0][0])
<type 'sage.rings.finite_rings.integer_mod.IntegerMod_int'>
"""
if test is not None:
from sage.misc.superseded import deprecation
deprecation(16553, "the keyword test is deprecated, use check instead")
check = test
from sage.matrix.constructor import matrix
from sage.structure.element import Matrix
# Reformatting input
if isinstance(points, Matrix):
assert incidence_matrix is None, "'incidence_matrix' cannot be defined when 'points' is a matrix"
assert blocks is None, "'blocks' cannot be defined when 'points' is a matrix"
incidence_matrix = points
points = blocks = None
elif points and blocks is None:
blocks = points
points = set().union(*blocks)
if points:
assert incidence_matrix is None, "'incidence_matrix' cannot be defined when 'points' is defined"
if incidence_matrix:
M = matrix(incidence_matrix)
v = M.nrows()
self._points = range(v)
self._point_to_index = None
self._blocks = sorted(M.nonzero_positions_in_column(i) for i in range(M.ncols()))
else:
if isinstance(points, (int,Integer)):
self._points = range(points)
self._point_to_index = None
else:
self._points = sorted(points)
if self._points == range(len(points)) and all(isinstance(x,(int,Integer)) for x in self._points):
self._point_to_index = None
else:
self._point_to_index = {e:i for i,e in enumerate(self._points)}
if check:
for block in blocks:
if any(x not in self._points for x in block):
raise ValueError("Block {} is not contained in the point set".format(block))
if len(block) != len(set(block)):
raise ValueError("Repeated element in block {}".format(block))
if self._point_to_index:
# translate everything to integers between 0 and v-1
blocks = [sorted(self._point_to_index[e] for e in block) for block in blocks]
elif copy:
# create a new list made of sorted blocks
blocks = [sorted(block) for block in blocks]
else:
# sort the data but avoid copying it
for b in blocks:
b.sort()
blocks.sort()
self._blocks = blocks
self._name = str(name) if name is not None else 'IncidenceStructure'
def __iter__(self):
"""
Iterator over the blocks.
Note that it is faster to call for the method ``.blocks(copy=True)``
(but in that case the output should not be modified).
EXAMPLES::
sage: sts = designs.steiner_triple_system(9)
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]]
sage: b = designs.IncidenceStructure('ab', ['a','ab'])
sage: it = iter(b)
sage: it.next()
['a']
sage: it.next()
['a', 'b']
"""
if self._point_to_index is None:
for b in self._blocks: yield b[:]
else:
for b in self._blocks:
yield [self._points[i] for i in b]
def __repr__(self):
"""
A print method.
EXAMPLES::
sage: BD = designs.IncidenceStructure(7,[[0,1,2],[0,3,4],[0,5,6],[1,3,5],[1,4,6],[2,3,6],[2,4,5]])
sage: BD
Incidence structure with 7 points and 7 blocks
"""
return 'Incidence structure with {} points and {} blocks'.format(
self.num_points(), self.num_blocks())
def __str__(self):
"""
A print method.
EXAMPLES::
sage: BD = designs.IncidenceStructure(7,[[0,1,2],[0,3,4],[0,5,6],[1,3,5],[1,4,6],[2,3,6],[2,4,5]])
sage: print BD
IncidenceStructure<points=[0, 1, 2, 3, 4, 5, 6], blocks=[[0, 1, 2], [0, 3, 4], [0, 5, 6], [1, 3, 5], [1, 4, 6], [2, 3, 6], [2, 4, 5]]>
sage: BD = designs.IncidenceStructure(range(7),[[0,1,2],[0,3,4],[0,5,6],[1,3,5],[1,4,6],[2,3,6],[2,4,5]])
sage: print BD
IncidenceStructure<points=[0, 1, 2, 3, 4, 5, 6], blocks=[[0, 1, 2], [0, 3, 4], [0, 5, 6], [1, 3, 5], [1, 4, 6], [2, 3, 6], [2, 4, 5]]>
"""
return '{}<points={}, blocks={}>'.format(
self._name, self.ground_set(), self.blocks())
def __eq__(self, other):
"""
Tests is the two incidence structures are equal
TESTS::
sage: blocks = [[0,1,2],[0,3,4],[0,5,6],[1,3,5],[1,4,6],[2,3,6],[2,4,5]]
sage: BD1 = designs.IncidenceStructure(7, blocks)
sage: M = BD1.incidence_matrix()
sage: BD2 = designs.IncidenceStructure(incidence_matrix=M)
sage: BD1 == BD2
True
sage: e1 = frozenset([0,1])
sage: e2 = frozenset([2])
sage: sorted([e1,e2]) == [e1,e2]
True
sage: sorted([e2,e1]) == [e2,e1]
True
sage: I1 = designs.IncidenceStructure([e1,e2], [[e1],[e1,e2]])
sage: I2 = designs.IncidenceStructure([e1,e2], [[e2,e1],[e1]])
sage: I3 = designs.IncidenceStructure([e2,e1], [[e1,e2],[e1]])
sage: I1 == I2 and I2 == I1 and I1 == I3 and I3 == I1 and I2 == I3 and I3 == I2
True
"""
# We are extra careful in this method since we cannot assume that a
# total order is defined on the point set.
if not isinstance(other, IncidenceStructure):
return False
if self._points == other._points:
return self._blocks == other._blocks
if (self.num_points() != other.num_points() or
self.num_blocks() != other.num_blocks()):
return False
p_to_i = self._point_to_index if self._point_to_index else range(self.num_points())
if any(p not in p_to_i for p in other.ground_set()):
return False
other_blocks = sorted(sorted(p_to_i[p] for p in b) for b in other.blocks())
return self._blocks == other_blocks
def __ne__(self, other):
r"""
Difference test.
EXAMPLES::
sage: BD1 = designs.IncidenceStructure(7, [[0,1,2],[0,3,4],[0,5,6],[1,3,5],[1,4,6],[2,3,6],[2,4,5]])
sage: M = BD1.incidence_matrix()
sage: BD2 = designs.IncidenceStructure(incidence_matrix=M)
sage: BD1 != BD2
False
"""
return not self.__eq__(other)
def ground_set(self, copy=True):
r"""
Return the ground set (i.e the list of points).
INPUT:
- ``copy`` (boolean) -- ``True`` by default. When set to ``False``, a
pointer toward the object's internal data is given. Set it to
``False`` only if you know what you are doing.
EXAMPLES::
sage: designs.IncidenceStructure(3, [[0,1],[0,2]]).ground_set()
[0, 1, 2]
"""
if copy:
return self._points[:]
return self._points
def num_points(self):
r"""
The number of points in that design.
EXAMPLES::
sage: designs.DesarguesianProjectivePlaneDesign(2).num_points()
7
sage: B = designs.IncidenceStructure(4, [[0,1],[0,2],[0,3],[1,2], [1,2,3]])
sage: B.num_points()
4
"""
return len(self._points)
def num_blocks(self):
r"""
The number of blocks.
EXAMPLES::
sage: designs.DesarguesianProjectivePlaneDesign(2).num_blocks()
7
sage: B = designs.IncidenceStructure(4, [[0,1],[0,2],[0,3],[1,2], [1,2,3]])
sage: B.num_blocks()
5
"""
return len(self._blocks)
def blocks(self, copy=True):
"""Return the list of blocks.
INPUT:
- ``copy`` (boolean) -- ``True`` by default. When set to ``False``, a
pointer toward the object's internal data is given. Set it to
``False`` only if you know what you are doing.
EXAMPLES::
sage: BD = designs.IncidenceStructure(7,[[0,1,2],[0,3,4],[0,5,6],[1,3,5],[1,4,6],[2,3,6],[2,4,5]])
sage: BD.blocks()
[[0, 1, 2], [0, 3, 4], [0, 5, 6], [1, 3, 5], [1, 4, 6], [2, 3, 6], [2, 4, 5]]
What you should pay attention to::
sage: blocks = BD.blocks(copy=False)
sage: del blocks[0:6]
sage: BD
Incidence structure with 7 points and 1 blocks
"""
if copy:
if self._point_to_index is None:
from copy import deepcopy
return deepcopy(self._blocks)
else:
return [[self._points[i] for i in b] for b in self._blocks]
else:
return self._blocks
def block_sizes(self):
r"""
Return the set of block sizes.
EXAMPLES::
sage: BD = designs.IncidenceStructure(8, [[0,1,3],[1,4,5,6],[1,2],[5,6,7]])
sage: BD.block_sizes()
[3, 2, 4, 3]
sage: BD = designs.IncidenceStructure(7,[[0,1,2],[0,3,4],[0,5,6],[1,3,5],[1,4,6],[2,3,6],[2,4,5]])
sage: BD.block_sizes()
[3, 3, 3, 3, 3, 3, 3]
"""
return map(len, self._blocks)
def degree(self, p=None):
r"""
Returns the degree of a point ``p``
The degree of a point `p` is the number of blocks that contain it.
INPUT:
- ``p`` -- a point. If set to ``None`` (default), a dictionary
associating the points with their degrees is returned.
EXAMPLES::
sage: designs.steiner_triple_system(9).degree(3)
4
sage: designs.steiner_triple_system(9).degree()
{0: 4, 1: 4, 2: 4, 3: 4, 4: 4, 5: 4, 6: 4, 7: 4, 8: 4}
"""
if p is None:
d = [0]*self.num_points()
for b in self._blocks:
for x in b:
d[x] += 1
return {p: d[i] for i, p in enumerate(self._points)}
else:
p = self._point_to_index[p] if self._point_to_index else p
return sum(1 for b in self._blocks if p in b)
def is_connected(self):
r"""
Test whether the design is connected.
EXAMPLES::
sage: designs.IncidenceStructure(3, [[0,1],[0,2]]).is_connected()
True
sage: designs.IncidenceStructure(4, [[0,1],[2,3]]).is_connected()
False
"""
return self.incidence_graph().is_connected()
def is_simple(self):
r"""
Test whether this design is simple (i.e. no repeated block).
EXAMPLES::
sage: designs.IncidenceStructure(3, [[0,1],[1,2],[0,2]]).is_simple()
True
sage: designs.IncidenceStructure(3, [[0],[0]]).is_simple()
False
sage: V = [(0,'a'),(0,'b'),(1,'a'),(1,'b')]
sage: B = [[V[0],V[1]], [V[1],V[2]]]
sage: I = designs.IncidenceStructure(V, B)
sage: I.is_simple()
True
sage: I2 = designs.IncidenceStructure(V, B*2)
sage: I2.is_simple()
False
"""
B = self._blocks
return all(B[i] != B[i+1] for i in xrange(len(B)-1))
def _gap_(self):
"""
Return the GAP string describing the design.
EXAMPLES::
sage: BD = designs.IncidenceStructure(7,[[0,1,2],[0,3,4],[0,5,6],[1,3,5],[1,4,6],[2,3,6],[2,4,5]])
sage: BD._gap_()
'BlockDesign(7,[[1, 2, 3], [1, 4, 5], [1, 6, 7], [2, 4, 6], [2, 5, 7], [3, 4, 7], [3, 5, 6]])'
"""
B = self.blocks()
v = self.num_points()
gB = [[x+1 for x in b] for b in self._blocks]
return "BlockDesign("+str(v)+","+str(gB)+")"
def incidence_matrix(self):
r"""
Return the incidence matrix `A` of the design. A is a `(v \times b)`
matrix defined by: ``A[i,j] = 1`` if ``i`` is in block ``B_j`` and 0
otherwise.
EXAMPLES::
sage: BD = designs.IncidenceStructure(7, [[0,1,2],[0,3,4],[0,5,6],[1,3,5],[1,4,6],[2,3,6],[2,4,5]])
sage: BD.block_sizes()
[3, 3, 3, 3, 3, 3, 3]
sage: BD.incidence_matrix()
[1 1 1 0 0 0 0]
[1 0 0 1 1 0 0]
[1 0 0 0 0 1 1]
[0 1 0 1 0 1 0]
[0 1 0 0 1 0 1]
[0 0 1 1 0 0 1]
[0 0 1 0 1 1 0]
sage: I = designs.IncidenceStructure('abc', ('ab','abc','ac','c'))
sage: I.incidence_matrix()
[1 1 1 0]
[1 1 0 0]
[0 1 1 1]
"""
from sage.matrix.constructor import Matrix
from sage.rings.all import ZZ
A = Matrix(ZZ, self.num_points(), self.num_blocks(), sparse=True)
for j, b in enumerate(self._blocks):
for i in b:
A[i, j] = 1
return A
def incidence_graph(self):
"""
Returns the incidence graph of the design, where the incidence
matrix of the design is the adjacency matrix of the graph.
EXAMPLE::
sage: BD = designs.IncidenceStructure(7, [[0,1,2],[0,3,4],[0,5,6],[1,3,5],[1,4,6],[2,3,6],[2,4,5]])
sage: BD.incidence_graph()
Bipartite graph on 14 vertices
sage: A = BD.incidence_matrix()
sage: Graph(block_matrix([[A*0,A],[A.transpose(),A*0]])) == BD.incidence_graph()
True
REFERENCE:
- Sage Reference Manual on Graphs
"""
from sage.graphs.bipartite_graph import BipartiteGraph
A = self.incidence_matrix()
return BipartiteGraph(A)
#####################
# real computations #
#####################
def packing(self, solver=None, verbose=0):
r"""
Returns a maximum packing
A maximum packing in a hypergraph is collection of disjoint sets/blocks
of maximal cardinality. This problem is NP-complete in general, and in
particular on 3-uniform hypergraphs. It is solved here with an Integer
Linear Program.
For more information, see the :wikipedia:`Packing_in_a_hypergraph`.
INPUT:
- ``solver`` -- (default: ``None``) Specify a Linear Program (LP)
solver to be used. If set to ``None``, the default one is used. For
more information on LP solvers and which default solver is used, see
the method
:meth:`solve <sage.numerical.mip.MixedIntegerLinearProgram.solve>`
of the class
:class:`MixedIntegerLinearProgram <sage.numerical.mip.MixedIntegerLinearProgram>`.
- ``verbose`` -- integer (default: ``0``). Sets the level of
verbosity. Set to 0 by default, which means quiet.
Only useful when ``algorithm == "LP"``.
EXAMPLE::
sage; IncidenceStructure([[1,2],[3,"A"],[2,3]]).packing()
[[1, 2], [3, 'A']]
sage: len(designs.steiner_triple_system(9).packing())
3
"""
from sage.numerical.mip import MixedIntegerLinearProgram
# List of blocks containing a given point x
d = [[] for x in self._points]
for i,B in enumerate(self._blocks):
for x in B:
d[x].append(i)
p = MixedIntegerLinearProgram(solver=solver)
b = p.new_variable(binary=True)
for x,L in enumerate(d): # Set of disjoint blocks
p.add_constraint(p.sum([b[i] for i in L]) <= 1)
# Maximum number of blocks
p.set_objective(p.sum([b[i] for i in range(self.num_blocks())]))
p.solve(log=verbose)
return [[self._points[x] for x in self._blocks[i]]
for i,v in p.get_values(b).iteritems() if v]
def is_t_design(self, t=None, v=None, k=None, l=None, return_parameters=False):
r"""
Test whether ``self`` is a `t-(v,k,l)` design.
A `t-(v,k,\lambda)` (sometimes called `t`-design for short) is a block
design in which:
- the underlying set has cardinality `v`
- the blocks have size `k`
- each `t`-subset of points is covered by `\lambda` blocks
INPUT:
- ``t,v,k,l`` (integers) -- their value is set to ``None`` by
default. The function tests whether the design is a ``t-(v,k,l)``
design using the provided values and guesses the others. Note that
`l`` cannot be specified if ``t`` is not.
- ``return_parameters`` (boolean)-- whether to return the parameters of
the `t`-design. If set to ``True``, the function returns a pair
``(boolean_answer,(t,v,k,l))``.
EXAMPLES::
sage: fano_blocks = [[0,1,2],[0,3,4],[0,5,6],[1,3,5],[1,4,6],[2,3,6],[2,4,5]]
sage: BD = designs.IncidenceStructure(7, fano_blocks)
sage: BD.is_t_design()
True
sage: BD.is_t_design(return_parameters=True)
(True, (2, 7, 3, 1))
sage: BD.is_t_design(2, 7, 3, 1)
True
sage: BD.is_t_design(1, 7, 3, 3)
True
sage: BD.is_t_design(0, 7, 3, 7)
True
sage: BD.is_t_design(0,6,3,7) or BD.is_t_design(0,7,4,7) or BD.is_t_design(0,7,3,8)
False
sage: BD = designs.AffineGeometryDesign(3, 1, GF(2))
sage: BD.is_t_design(1)
True
sage: BD.is_t_design(2)
True
Steiner triple and quadruple systems are other names for `2-(v,3,1)` and
`3-(v,4,1)` designs::
sage: S3_9 = designs.steiner_triple_system(9)
sage: S3_9.is_t_design(2,9,3,1)
True
sage: blocks = designs.steiner_quadruple_system(8)
sage: S4_8 = designs.IncidenceStructure(8, blocks)
sage: S4_8.is_t_design(3,8,4,1)
True
sage: blocks = designs.steiner_quadruple_system(14)
sage: S4_14 = designs.IncidenceStructure(14, blocks)
sage: S4_14.is_t_design(3,14,4,1)
True
Some examples of Witt designs that need the gap database::
sage: BD = designs.WittDesign(9) # optional - gap_packages
sage: BD.is_t_design(2,9,3,1) # optional - gap_packages
True
sage: W12 = designs.WittDesign(12) # optional - gap_packages
sage: W12.is_t_design(5,12,6,1) # optional - gap_packages
True
sage: W12.is_t_design(4) # optional - gap_packages
True
Further examples::
sage: D = designs.IncidenceStructure(4,[[],[]])
sage: D.is_t_design(return_parameters=True)
(True, (0, 4, 0, 2))
sage: D = designs.IncidenceStructure(4, [[0,1],[0,2],[0,3]])
sage: D.is_t_design(return_parameters=True)
(True, (0, 4, 2, 3))
sage: D = designs.IncidenceStructure(4, [[0],[1],[2],[3]])
sage: D.is_t_design(return_parameters=True)
(True, (1, 4, 1, 1))
sage: D = designs.IncidenceStructure(4,[[0,1],[2,3]])
sage: D.is_t_design(return_parameters=True)
(True, (1, 4, 2, 1))
sage: D = designs.IncidenceStructure(4, [range(4)])
sage: D.is_t_design(return_parameters=True)
(True, (4, 4, 4, 1))
TESTS::
sage: blocks = designs.steiner_quadruple_system(8)
sage: S4_8 = designs.IncidenceStructure(8, blocks)
sage: R = range(15)
sage: [(v,k,l) for v in R for k in R for l in R if S4_8.is_t_design(3,v,k,l)]
[(8, 4, 1)]
sage: [(v,k,l) for v in R for k in R for l in R if S4_8.is_t_design(2,v,k,l)]
[(8, 4, 3)]
sage: [(v,k,l) for v in R for k in R for l in R if S4_8.is_t_design(1,v,k,l)]
[(8, 4, 7)]
sage: [(v,k,l) for v in R for k in R for l in R if S4_8.is_t_design(0,v,k,l)]
[(8, 4, 14)]
sage: A = designs.AffineGeometryDesign(3, 1, GF(2))
sage: A.is_t_design(return_parameters=True)
(True, (2, 8, 2, 1))
sage: A = designs.AffineGeometryDesign(4, 2, GF(2))
sage: A.is_t_design(return_parameters=True)
(True, (3, 16, 4, 1))
sage: I = designs.IncidenceStructure(2, [])
sage: I.is_t_design(return_parameters=True)
(True, (0, 2, 0, 0))
sage: I = designs.IncidenceStructure(2, [[0],[0,1]])
sage: I.is_t_design(return_parameters=True)
(False, (0, 0, 0, 0))
"""
from sage.rings.arith import binomial
# Missing parameters ?
if v is None:
v = self.num_points()
if k is None:
k = len(self._blocks[0]) if self._blocks else 0
if l is not None and t is None:
raise ValueError("t must be set when l=None")
b = self.num_blocks()
# Trivial wrong answers
if (any(len(block) != k for block in self._blocks) or # non k-uniform
v != self.num_points()):
return (False, (0,0,0,0)) if return_parameters else False
# Trivial case t>k
if (t is not None and t>k):
if (l is None or l == 0):
return (True, (t,v,k,0)) if return_parameters else True
else:
return (False, (0,0,0,0)) if return_parameters else False
# Trivial case k=0
if k==0:
if (l is None or l == 0):
return (True, (0,v,k,b)) if return_parameters else True
else:
return (False, (0,0,0,0)) if return_parameters else False
# Trivial case k=v (includes v=0)
if k == v:
if t is None:
t = v
if l is None or b == l:
return (True, (t,v,k,b)) if return_parameters else True
else:
return (True, (0,0,0,0)) if return_parameters else False
# Handbook of combinatorial design theorem II.4.8:
#
# a t-(v,k,l) is also a t'-(v,k,l')
# for t' < t and l' = l* binomial(v-t',t-t') / binomial(k-t',t-t')
#
# We look for the largest t such that self is a t-design
from itertools import combinations
for tt in (range(1,k+1) if t is None else [t]):
# is lambda an integer?
if (b*binomial(k,tt)) % binomial(v,tt) != 0:
tt -= 1
break
s = {}
for block in self._blocks:
for i in combinations(block,tt):
s[i] = s.get(i,0) + 1
if len(set(s.values())) != 1:
tt -= 1
break
ll = b*binomial(k,tt) // binomial(v,tt)
if ((t is not None and t!=tt) or
(l is not None and l!=ll)):
return (False, (0,0,0,0)) if return_parameters else False
else:
if tt == 0:
ll = b
return (True, (tt,v,k,ll)) if return_parameters else True
def dual(self, algorithm=None):
"""
Returns the dual of the incidence structure.
INPUT:
- ``algorithm`` -- whether to use Sage's implementation
(``algorithm=None``, default) or use GAP's (``algorithm="gap"``).
.. NOTE::
The ``algorithm="gap"`` option requires GAP's Design package
(included in the gap_packages Sage spkg).
EXAMPLES:
The dual of a projective plane is a projective plane::
sage: PP = designs.DesarguesianProjectivePlaneDesign(4)
sage: PP.dual().is_t_design(return_parameters=True)
(True, (2, 21, 5, 1))
TESTS::
sage: D = designs.IncidenceStructure(4, [[0,2],[1,2,3],[2,3]])
sage: D
Incidence structure with 4 points and 3 blocks
sage: D.dual()
Incidence structure with 3 points and 4 blocks
sage: print D.dual(algorithm="gap") # optional - gap_packages
IncidenceStructure<points=[0, 1, 2], blocks=[[0], [0, 1, 2], [1], [1, 2]]>
sage: blocks = [[0,1,2],[0,3,4],[0,5,6],[1,3,5],[1,4,6],[2,3,6],[2,4,5]]
sage: BD = designs.IncidenceStructure(7, blocks, name="FanoPlane");
sage: BD
Incidence structure with 7 points and 7 blocks
sage: print BD.dual(algorithm="gap") # optional - gap_packages
IncidenceStructure<points=[0, 1, 2, 3, 4, 5, 6], blocks=[[0, 1, 2], [0, 3, 4], [0, 5, 6], [1, 3, 5], [1, 4, 6], [2, 3, 6], [2, 4, 5]]>
sage: BD.dual()
Incidence structure with 7 points and 7 blocks
REFERENCE:
- Soicher, Leonard, Design package manual, available at
http://www.gap-system.org/Manuals/pkg/design/htm/CHAP003.htm
"""
if algorithm == "gap":
from sage.interfaces.gap import gap
gap.load_package("design")
gD = self._gap_()
gap.eval("DD:=DualBlockDesign("+gD+")")
v = eval(gap.eval("DD.v"))
gblcks = eval(gap.eval("DD.blocks"))
gB = []
for b in gblcks:
gB.append([x-1 for x in b])
return IncidenceStructure(range(v), gB, name=None, check=False)
else:
return IncidenceStructure(
incidence_matrix=self.incidence_matrix().transpose(),
check=False)
def automorphism_group(self):
r"""
Returns the subgroup of the automorphism group of the incidence graph
which respects the P B partition. It is (isomorphic to) the automorphism
group of the block design, although the degrees differ.
EXAMPLES::
sage: P = designs.DesarguesianProjectivePlaneDesign(2); P
Incidence structure with 7 points and 7 blocks
sage: G = P.automorphism_group()
sage: G.is_isomorphic(PGL(3,2))
True
sage: G
Permutation Group with generators [(2,3)(4,5), (2,4)(3,5), (1,2)(4,6), (0,1)(4,5)]
A non self-dual example::
sage: IS = designs.IncidenceStructure(range(4), [[0,1,2,3],[1,2,3]])
sage: IS.automorphism_group().cardinality()
6
sage: IS.dual().automorphism_group().cardinality()
1
An example with points other than integers::
sage: I = designs.IncidenceStructure('abc', ('ab','ac','bc'))
sage: I.automorphism_group()
Permutation Group with generators [('b','c'), ('a','b')]
"""
from sage.groups.perm_gps.partn_ref.refinement_matrices import MatrixStruct
from sage.groups.perm_gps.permgroup import PermutationGroup
from sage.groups.perm_gps.permgroup_named import SymmetricGroup
M1 = self.incidence_matrix().transpose()
M2 = MatrixStruct(M1)
M2.run()
gens = M2.automorphism_group()[0]
if self._point_to_index:
gens = [[self._points[i] for i in p] for p in gens]
return PermutationGroup(gens, domain=self._points)
###############
# Deprecation #
###############
def parameters(self):
r"""
Deprecated function. You should use :meth:`is_t_design` instead.
EXAMPLES::
sage: I = designs.IncidenceStructure('abc', ['ab','ac','bc'])
sage: I.parameters()
doctest:...: DeprecationWarning: .parameters() is deprecated. Use
`is_t_design` instead
See http://trac.sagemath.org/16553 for details.
(2, 3, 2, 1)
"""
from sage.misc.superseded import deprecation
deprecation(16553, ".parameters() is deprecated. Use `is_t_design` instead")
return self.is_t_design(return_parameters=True)[1]
dual_design = deprecated_function_alias(16553, dual)
dual_incidence_structure = deprecated_function_alias(16553, dual)
is_block_design = deprecated_function_alias(16553, is_t_design)
points = deprecated_function_alias(16553, ground_set)
def block_design_checker(self, t, v, k, lmbda, type=None):
"""
This method is deprecated and will soon be removed (see :trac:`16553`).
You could use :meth:`is_t_design` instead.
This is *not* a wrapper for GAP Design's IsBlockDesign. The GAP
Design function IsBlockDesign
http://www.gap-system.org/Manuals/pkg/design/htm/CHAP004.htm
apparently simply checks the record structure and no mathematical
properties. Instead, the function below checks some necessary (but
not sufficient) "easy" identities arising from the identity.
INPUT: