Module for set partitions. We define a
Partition
to be a wrapper around the DisjointUnion
type defined
in the DataStructures
module, but with a bit more functionality.
We also include IntegerPartition
.
A new Partition
is created by specifying the ground set. That is, if A
is a Set{T}
(for some type T
) or an IntSet
, then Partition(A)
creates
a new Partition
whose ground set is A
and the parts are singletons.
julia> using ShowSet
julia> using SimplePartitions
julia> A = Set(1:10)
{1,2,3,4,5,6,7,8,9,10}
julia> P = Partition(A)
{{9},{6},{5},{8},{1},{3},{2},{10},{7},{4}}
The parameter to Partition
may also be a list (one-dimensional array) or
a positive integer n
, in which case a partition of the set {1,2,...,n} is
created.
If S
is a set of sets then PartitionBuilder(S)
creates
a new partition whose parts are the sets in S
. The
sets in S
must be nonempty and pairwise disjoint.
If p
is a Permutation
, then Partition(p)
creates a new
partition whose parts are the cycles of p
.
If d
is a dictionary, the Partition(d)
creates a new
partition whose elements are the keys of d
in which
two elements a
and b
are in the same part if and only
if d[a] == d[b]
.
num_elements(P)
: returns the number of elements in the ground set ofP
.num_parts(P)
: returns the number of parts inP
.parts(P)
: returns the set of the parts in this partition.collect(P)
returns a one-dimensional array containing the parts.ground_set(P)
: returns (a copy of) the ground set ofP
.in(a,P)
: test ifa
(element) is in the ground set ofP
.in(A,P)
: test ifA
(set) is a part ofP
.merge_parts!(P,a,b)
: ModifyP
by merging the parts ofP
that contain the elementsa
andb
. This may also be called with a list for the second argument:merge_parts!(P,[a,b,...])
.in_same_part(P,a,b)
: returnstrue
ifa
andb
are in the same part ofP
.find_part(P,a)
: returns the set of elements inP
that are in the same part asa
.
join(P,Q)
returns the join of partitionsP
andQ
. This can also be invoked asP+Q
or asP∨Q
.meet(P,Q)
returns the meet of the partitions. This can also be invoked asP*Q
or asP∧Q
.P+x
whereP
is a partition andx
is a new element creates a new partition in whichx
is added as a singleton.P+A
whereP
is a partition andA
is a set of elements (that are disjoint from the elements already inP
) creates a new partition by addingA
as a part.
P==Q
determines ifP
andQ
are equal partitions.P<=Q
determines ifP
is a refinement ofQ
. That is, we returntrue
if each part ofP
is a subset of a part ofQ
. Note thatP
andQ
must have the same ground set or else an error is thrown. The variantsP<Q
,P>=Q
, andP>Q
are available with the expected meanings. Callingrefines(P,Q)
is the same asP<=Q
.
It is possible to iterate over the parts of a Partition
:
julia> using SimplePartitions, ShowSet
julia> P = Partition(7); merge_parts!(P,1,2); merge_parts!(P,3,4); merge_parts!(P,3,5);
julia> @show P
P = {{3,4,5},{6},{1,2},{7}}
julia> for p in P; println(p); end
{7}
{6}
{3,4,5}
{1,2}
all_partitions(A::Set)
creates aSet
containing all possible partitions ofA
.all_partitions(n::Int)
creates aSet
containing all possible partitions of the set{1,2,...,n}
.
Both of these take an optional second argument k
to specify that
only partitions with exactly k
parts should be returned.
Note: Sets are nicely displayed here because we invoked
using ShowSet
.
julia> A = Set(["alpha", "bravo", "charlie", "delta", "echo"])
{alpha,bravo,charlie,delta,echo}
julia> P = Partition(A)
{{delta},{echo},{charlie},{bravo},{alpha}}
julia> merge_parts!(P,"alpha", "bravo")
julia> merge_parts!(P,"echo", "bravo")
julia> merge_parts!(P,"charlie", "delta")
julia> P
{{charlie,delta},{alpha,bravo,echo}}
julia> Q = Partition(A);
julia> merge_parts!(Q,"alpha", "echo")
julia> merge_parts!(Q,"delta","alpha")
julia> Q
{{charlie},{bravo},{alpha,delta,echo}}
julia> P+Q
{{alpha,bravo,charlie,delta,echo}}
julia> P*Q
{{delta},{charlie},{bravo},{alpha,echo}}
The type IntegerPartition
represents a partition of an integer.
These can be constructed either from a one-dimensional array of
integers or as individual arguments:
IntegerPartition([a,b,c,...])
orIntegerPartition(a,b,c,...)
parts(P)
returns a list containing the parts.sum(P)
returns the sum of the parts.num_parts(P)
returns the number of parts.Ferrers(P)
prints a Ferrer's diagram ofP
.conj(P)
orP'
returns the Ferrer's conjugate ofP
P+Q
returns the concatenation ofP
andQ
:
julia> P = IntegerPartition(2,2,4)
(4+2+2)
julia> Q = IntegerPartition(5,2,1)
(5+2+1)
julia> P+Q
(5+4+2+2+2+1)
- Create
RandomPartition(n)
[andRandomPartition(Set)
].