Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" Set systems
Many matroid methods return a collection of subsets. In this module a class :class:`SetSystem <sage.matroids.set_system.SetSystem>` is defined to do just this. The class is intended for internal use, so all you can do as a user is iterate over its members.
The class is equipped with partition refinement methods to help with matroid isomorphism testing.
AUTHORS:
- Rudi Pendavingh, Stefan van Zwam (2013-04-01): initial version
Methods ======= """ #***************************************************************************** # Copyright (C) 2013 Rudi Pendavingh <rudi.pendavingh@gmail.com> # Copyright (C) 2013 Stefan van Zwam <stefanvanzwam@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 __future__ import print_function
from cysignals.memory cimport check_allocarray, check_reallocarray, sig_free include 'sage/data_structures/bitset.pxi'
# SetSystem
cdef class SetSystem: """ A ``SetSystem`` is an enumerator of a collection of subsets of a given fixed and finite ground set. It offers the possibility to enumerate its contents. One is most likely to encounter these as output from some Matroid methods::
sage: M = matroids.named_matroids.Fano() sage: M.circuits() Iterator over a system of subsets
To access the sets in this structure, simply iterate over them. The simplest way must be::
sage: from sage.matroids.set_system import SetSystem sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) sage: T = list(S)
Or immediately use it to iterate::
sage: from sage.matroids.set_system import SetSystem sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) sage: [min(X) for X in S] [1, 3, 1]
Note that this class is intended for runtime, so no loads/dumps mechanism was implemented.
.. WARNING::
The only guaranteed behavior of this class is that it is iterable. It is expected that M.circuits(), M.bases(), and so on will in the near future return actual iterators. All other methods (which are already hidden by default) are only for internal use by the Sage matroid code. """ def __cinit__(self, groundset, subsets=None, capacity=1): """ Init internal data structures.
EXAMPLES::
sage: from sage.matroids.set_system import SetSystem sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) sage: S Iterator over a system of subsets """ cdef long i else:
def __init__(self, groundset, subsets=None, capacity=1): """ Create a SetSystem.
INPUT:
- ``groundset`` -- a list or tuple of finitely many elements. - ``subsets`` -- (default: ``None``) an enumerator for a set of subsets of ``groundset``. - ``capacity`` -- (default: ``1``) Initial maximal capacity of the set system.
EXAMPLES::
sage: from sage.matroids.set_system import SetSystem sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) sage: S Iterator over a system of subsets sage: sorted(S[1]) [3, 4] sage: for s in S: print(sorted(s)) [1, 2] [3, 4] [1, 2, 4]
"""
def __dealloc__(self): cdef long i
def __len__(self): """ Return the number of subsets in this SetSystem.
EXAMPLES::
sage: from sage.matroids.set_system import SetSystem sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) sage: S Iterator over a system of subsets sage: len(S) 3 """
def __iter__(self): """ Return an iterator for the subsets in this SetSystem.
EXAMPLES::
sage: from sage.matroids.set_system import SetSystem sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) sage: for s in S: print(sorted(s)) [1, 2] [3, 4] [1, 2, 4] """
def __getitem__(self, k): """ Return the `k`-th subset in this SetSystem.
INPUT:
- ``k`` -- an integer. The index of the subset in the system.
OUTPUT:
The subset at index `k`.
EXAMPLES::
sage: from sage.matroids.set_system import SetSystem sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) sage: sorted(S[0]) [1, 2] sage: sorted(S[1]) [3, 4] sage: sorted(S[2]) [1, 2, 4] """ else: raise ValueError("out of range")
def __repr__(self): """ Return a string representation of ``self``.
EXAMPLES::
sage: from sage.matroids.set_system import SetSystem sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) sage: repr(S) # indirect doctest 'Iterator over a system of subsets'
"""
cdef copy(self): cdef SetSystem S
cdef _relabel(self, l): """ Relabel each element `e` of the ground set as `l(e)`, where `l` is a given injective map.
INPUT:
- ``l`` -- a python object such that `l[e]` is the new label of e.
OUTPUT:
``None``.
""" cdef long i E = [] for i in range(self._groundset_size): if self._groundset[i] in l: E.append(l[self._E[i]]) else: E.append(self._E[i]) self._groundset = E self._idx = {} for i in xrange(self._groundset_size): self._idx[self._groundset[i]] = i
cpdef _complements(self): """ Return a SetSystem containing the complements of each element in the groundset.
EXAMPLES::
sage: from sage.matroids.set_system import SetSystem sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) sage: T = S._complements() sage: for t in T: print(sorted(t)) [3, 4] [1, 2] [3]
""" cdef SetSystem S return self
cdef inline resize(self, k=None): """ Change the capacity of the SetSystem. """ bitset_free(self._subsets[i])
cdef inline _append(self, bitset_t X): """ Append subset in internal, bitset format """
cdef inline append(self, X): """ Append subset. """
cdef inline _subset(self, long k): """ Return the k-th subset, in index format. """ return bitset_list(self._subsets[k])
cdef subset(self, k): """ Return the k-th subset. """ cdef long i
cpdef _get_groundset(self): """ Return the ground set of this SetSystem.
EXAMPLES::
sage: from sage.matroids.set_system import SetSystem sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) sage: sorted(S._get_groundset()) [1, 2, 3, 4] """
cpdef is_connected(self): """ Test if the :class:`SetSystem` is connected.
A :class:`SetSystem` is connected if there is no nonempty proper subset ``X`` of the ground set so the each subset is either contained in ``X`` or disjoint from ``X``.
EXAMPLES::
sage: from sage.matroids.set_system import SetSystem sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) sage: S.is_connected() True sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4]]) sage: S.is_connected() False sage: S = SetSystem([1], []) sage: S.is_connected() True
""" cdef long i cdef bitset_t active
# We compute the union of all sets containing 0, and deactivate them.
# We update _temp with all active sets that intersects it. If there # is no such set, then _temp is closed (i.e. a connected component).
# isomorphism
cdef list _incidence_count(self, E): """ For the sub-collection indexed by ``E``, count how often each element occurs. """ cdef long i, e cdef list cnt
cdef SetSystem _groundset_partition(self, SetSystem P, list cnt): """ Helper method for partition methods below. """ cdef dict C cdef long i, j, v, t0, t cdef bint split
continue else:
cdef long subset_characteristic(self, SetSystem P, long e): """ Helper method for partition methods below. """ cdef long c
cdef subsets_partition(self, SetSystem P=None, E=None): """ Helper method for partition methods below. """ P = self.groundset_partition()
else:
cdef _distinguish(self, v): """ Helper method for partition methods below. """ cdef SetSystem S
# partition functions cdef initial_partition(self, SetSystem P=None, E=None): """ Helper method for partition methods below. """ else:
cpdef _equitable_partition(self, SetSystem P=None, EP=None): """ Return an equitable ordered partition of the ground set of the hypergraph whose edges are the subsets in this SetSystem.
Given any ordered partition `P = (p_1, ..., p_k)` of the ground set of a hypergraph, any edge `e` of the hypergraph has a characteristic intersection number sequence `i(e)=(|p_1\cap e|, ... , |p_k\cap e|))`. There is an ordered partition `EP` of the edges that groups the edges according to this intersection number sequence. Given this an ordered partition of the edges, we may similarly refine `P` to a new ordered partition `P'`, by considering the incidence numbers of ground set elements with each partition element of `EP`.
The ordered partition `P` is equitable when `P' = P`.
INPUT:
- ``P``, an equitable ordered partition of the ground set, stored as a SetSystem. - ``EP``, the corresponding equitable partition of the edges, stored as a list of lists of indices of subsets of this SetSystem.
OUTPUT:
- ``P``, an equitable ordered partition of the ground set, stored as a SetSystem. - ``EP``, the corresponding equitable partition of the edges, stored as a list of lists of indices of subsets of this SetSystem. - ``h``, an integer invariant of the SetSystem.
EXAMPLES::
sage: from sage.matroids.set_system import SetSystem sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) sage: for p in S._equitable_partition()[0]: print(sorted(p)) [3] [4] [1, 2] sage: T = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 3, 4]]) sage: for p in T._equitable_partition()[0]: print(sorted(p)) [2] [1] [3, 4]
.. NOTE::
We do not maintain any well-defined order when refining a partition. We do maintain that the resulting order of the partition elements is an invariant of the isomorphism class of the hypergraph. """ cdef long h, l cdef list EP2, H
else:
cpdef _heuristic_partition(self, SetSystem P=None, EP=None): """ Return an heuristic ordered partition into singletons of the ground set of the hypergraph whose edges are the subsets in this SetSystem.
This partition obtained as follows: make an equitable partition ``P``, and while ``P`` has a partition element ``p`` with more than one element, select an arbitrary ``e`` from the first such ``p`` and split ``p`` into ``p-e``. Then replace ``P`` with the equitable refinement of this partition.
INPUT:
- ``P`` -- (default: ``None``) an ordered partition of the ground set. - ``EP`` -- (default: ``None``) the corresponding partition of the edges, stored as a list of lists of indices of subsets of this SetSystem.
OUTPUT:
- ``P`` -- an ordered partition of the ground set into singletons, stored as a SetSystem. - ``EP`` -- the corresponding partition of the edges, stored as a list of lists of indices of subsets of this SetSystem. - ``h`` -- an integer invariant of the SetSystem.
EXAMPLES::
sage: from sage.matroids.set_system import SetSystem sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) sage: for p in S._heuristic_partition()[0]: print(sorted(p)) [3] [4] [2] [1] sage: T = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 3, 4]]) sage: for p in T._heuristic_partition()[0]: print(sorted(p)) [2] [1] [4] [3] """
cpdef _isomorphism(self, SetSystem other, SetSystem SP=None, SetSystem OP=None): """ Return a groundset isomorphism between this SetSystem and an other.
INPUT:
- ``other`` -- a SetSystem - ``SP`` (optional) -- a SetSystem storing an ordered partition of the ground set of ``self`` - ``OP`` (optional) -- a SetSystem storing an ordered partition of the ground set of ``other``
OUTPUT:
``morphism`` -- a dictionary containing an isomorphism respecting the given ordered partitions, or ``None`` if no such isomorphism exists.
EXAMPLES::
sage: from sage.matroids.set_system import SetSystem sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) sage: T = SetSystem(['a', 'b', 'c', 'd'], [['a', 'b'], ['c', 'd'], ....: ['a', 'c', 'd']]) sage: S._isomorphism(T) {1: 'c', 2: 'd', 3: 'b', 4: 'a'} sage: S = SetSystem([], []) sage: S._isomorphism(S) {} """ cdef long l, p return None return None v = bitset_next(OP._subsets[i], v + 1) return None return None
cpdef _equivalence(self, is_equiv, SetSystem other, SetSystem SP=None, SetSystem OP=None): """ Return a groundset isomorphism that is an equivalence between this SetSystem and an other.
INPUT:
- ``is_equiv`` -- a function that determines if a given groundset isomorphism is a valid equivalence - ``other`` -- a SetSystem - ``SP`` (optional) -- a SetSystem storing an ordered partition of the groundset of ``self`` - ``OP`` (optional) -- a SetSystem storing an ordered partition of the groundset of ``other``
OUTPUT:
``morphism``, a dictionary containing an isomorphism respecting the given ordered partitions, so that ``is_equiv(self, other, morphism)`` is ``True``; or ``None`` if no such equivalence exists.
EXAMPLES::
sage: from sage.matroids.set_system import SetSystem sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) sage: T = SetSystem(['a', 'b', 'c', 'd'], [['a', 'b'], ['c', 'd'], ....: ['a', 'c', 'd']]) sage: S._equivalence(lambda self, other, morph:True, T) {1: 'c', 2: 'd', 3: 'b', 4: 'a'}
Check that :trac:`15189` is fixed::
sage: M = Matroid(ring=GF(5), reduced_matrix=[[1,0,3],[0,1,1],[1,1,0]]) sage: N = Matroid(ring=GF(5), reduced_matrix=[[1,0,1],[0,1,1],[1,1,0]]) sage: M.is_field_isomorphic(N) False sage: any(M.is_field_isomorphism(N, p) for p in Permutations(range(6))) False """ return None return None return None
cdef class SetSystemIterator: def __init__(self, H): """ Create an iterator for a SetSystem.
Called internally when iterating over the contents of a SetSystem.
EXAMPLES::
sage: from sage.matroids.set_system import SetSystem sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) sage: type(S.__iter__()) <... 'sage.matroids.set_system.SetSystemIterator'> """
def __next__(self): """ Return the next subset of a SetSystem.
EXAMPLES::
sage: from sage.matroids.set_system import SetSystem sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) sage: I = S.__iter__() sage: sorted(I.__next__()) [1, 2] sage: sorted(I.__next__()) [3, 4] sage: sorted(I.__next__()) [1, 2, 4] """ else: |