Represents a basic subset object.
We generate subsets using essentially two techniques, binary enumeration and lexicographic enumeration. The Subset class takes two arguments, the first one describes the initial subset to consider and the second describes the superset.
Examples
>>> from sympy.combinatorics.subsets import Subset
>>> a = Subset(['c','d'], ['a','b','c','d'])
>>> a.next_binary().subset
['b']
>>> a.prev_binary().subset
['c']
Gets the bitlist corresponding to a subset.
See also
Examples
>>> from sympy.combinatorics.subsets import Subset
>>> Subset.bitlist_from_subset(['c','d'], ['a','b','c','d'])
'0011'
Returns the number of all possible subsets.
See also
Examples
>>> from sympy.combinatorics.subsets import Subset
>>> a = Subset(['c','d'], ['a','b','c','d'])
>>> a.cardinality
16
This is a helper function. It iterates over the binary subsets by k steps. This variable can be both positive or negative.
See also
Examples
>>> from sympy.combinatorics.subsets import Subset
>>> a = Subset(['c','d'], ['a','b','c','d'])
>>> a.iterate_binary(-2).subset
['d']
>>> a = Subset(['a','b','c'], ['a','b','c','d'])
>>> a.iterate_binary(2).subset
[]
Helper function used for prev_gray and next_gray. It performs k step overs to get the respective Gray codes.
Examples
>>> from sympy.combinatorics.subsets import Subset
>>> a = Subset([1,2,3], [1,2,3,4])
>>> a.iterate_graycode(3).subset
[1, 4]
>>> a.iterate_graycode(-2).subset
[1, 2, 4]
Generates the next binary ordered subset.
See also
Examples
>>> from sympy.combinatorics.subsets import Subset
>>> a = Subset(['c','d'], ['a','b','c','d'])
>>> a.next_binary().subset
['b']
>>> a = Subset(['a','b','c','d'], ['a','b','c','d'])
>>> a.next_binary().subset
[]
Generates the next Gray code ordered subset.
See also
Examples
>>> from sympy.combinatorics.subsets import Subset
>>> a = Subset([1,2,3], [1,2,3,4])
>>> a.next_gray().subset
[1, 3]
Generates the next lexicographically ordered subset.
See also
Examples
>>> from sympy.combinatorics.subsets import Subset
>>> a = Subset(['c','d'], ['a','b','c','d'])
>>> a.next_lexicographic().subset
['d']
>>> a = Subset(['d'], ['a','b','c','d'])
>>> a.next_lexicographic().subset
[]
Generates the previous binary ordered subset.
See also
Examples
>>> from sympy.combinatorics.subsets import Subset
>>> a = Subset([], ['a','b','c','d'])
>>> a.prev_binary().subset
['a', 'b', 'c', 'd']
>>> a = Subset(['c','d'], ['a','b','c','d'])
>>> a.prev_binary().subset
['c']
Generates the previous Gray code ordered subset.
See also
Examples
>>> from sympy.combinatorics.subsets import Subset
>>> a = Subset([2,3,4], [1,2,3,4,5])
>>> a.prev_gray().subset
[2, 3, 4, 5]
Generates the previous lexicographically ordered subset.
See also
Examples
>>> from sympy.combinatorics.subsets import Subset
>>> a = Subset([], ['a','b','c','d'])
>>> a.prev_lexicographic().subset
['d']
>>> a = Subset(['c','d'], ['a','b','c','d'])
>>> a.prev_lexicographic().subset
['c']
Computes the binary ordered rank.
See also
Examples
>>> from sympy.combinatorics.subsets import Subset
>>> a = Subset([], ['a','b','c','d'])
>>> a.rank_binary
0
>>> a = Subset(['c','d'], ['a','b','c','d'])
>>> a.rank_binary
3
Computes the Gray code ranking of the subset.
See also
Examples
>>> from sympy.combinatorics.subsets import Subset
>>> a = Subset(['c','d'], ['a','b','c','d'])
>>> a.rank_gray
2
>>> a = Subset([2,4,5], [1,2,3,4,5,6])
>>> a.rank_gray
27
Computes the lexicographic ranking of the subset.
Examples
>>> from sympy.combinatorics.subsets import Subset
>>> a = Subset(['c','d'], ['a','b','c','d'])
>>> a.rank_lexicographic
14
>>> a = Subset([2,4,5], [1,2,3,4,5,6])
>>> a.rank_lexicographic
43
Gets the size of the subset.
See also
Examples
>>> from sympy.combinatorics.subsets import Subset
>>> a = Subset(['c','d'], ['a','b','c','d'])
>>> a.size
2
Gets the subset represented by the current instance.
See also
Examples
>>> from sympy.combinatorics.subsets import Subset
>>> a = Subset(['c','d'], ['a','b','c','d'])
>>> a.subset
['c', 'd']
Gets the subset defined by the bitlist.
See also
Examples
>>> from sympy.combinatorics.subsets import Subset
>>> Subset.subset_from_bitlist(['a','b','c','d'], '0011').subset
['c', 'd']
Return indices of subset in superset in a list; the list is empty if all elements of subset are not in superset.
Examples:
>>> from sympy.combinatorics import Subset
>>> superset = [1, 3, 2, 5, 4]
>>> Subset.subset_indices([3, 2, 1], superset)
[1, 2, 0]
>>> Subset.subset_indices([1, 6], superset)
[]
>>> Subset.subset_indices([], superset)
[]
Gets the superset of the subset.
See also
Examples
>>> from sympy.combinatorics.subsets import Subset
>>> a = Subset(['c','d'], ['a','b','c','d'])
>>> a.superset
['a', 'b', 'c', 'd']
Returns the size of the superset.
See also
Examples
>>> from sympy.combinatorics.subsets import Subset
>>> a = Subset(['c','d'], ['a','b','c','d'])
>>> a.superset_size
4
Gets the binary ordered subset of the specified rank.
See also
Examples
>>> from sympy.combinatorics.subsets import Subset
>>> Subset.unrank_binary(4, ['a','b','c','d']).subset
['b']
Finds the subsets of size k in lexicographic order.
This uses the itertools generator.
See also
Examples
>>> from sympy.combinatorics.subsets import ksubsets
>>> list(ksubsets([1,2,3], 2))
[(1, 2), (1, 3), (2, 3)]
>>> list(ksubsets([1,2,3,4,5], 2))
[(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)]