(Ordered) Set Partitions in fixed-size Blocks
Posted
by
Eugen
on Stack Overflow
See other posts from Stack Overflow
or by Eugen
Published on 2011-01-01T11:12:03Z
Indexed on
2011/01/02
12:53 UTC
Read the original article
Hit count: 256
Here is a function I would like to write but am unable to do so. Even if you don't / can't give a solution I would be grateful for tips. For example, I know that there is a correlation between the ordered represantions of the sum of an integer and ordered set partitions but that alone does not help me in finding the solution. So here is the description of the function I need:
The Task
Create an efficient* function
List<int[]> createOrderedPartitions(int n_1, int n_2,..., int n_k)
that returns a list of arrays of all set partions of the set
{0,...,n_1+n_2+...+n_k-1}
in number of arguments
blocks of size (in this
order) n_1,n_2,...,n_k
(e.g. n_1=2, n_2=1, n_3=1 -> ({0,1},{3},{2}),...
).
Here is a usage example:
int[] partition = createOrderedPartitions(2,1,1).get(0);
partition[0]; // -> 0
partition[1]; // -> 1
partition[2]; // -> 3
partition[3]; // -> 2
Note that the number of elements in the list is
(n_1+n_2+...+n_n choose n_1) * (n_2+n_3+...+n_n choose n_2) * ... *
(n_k choose n_k)
. Also, createOrderedPartitions(1,1,1)
would create the
permutations of {0,1,2}
and thus there would be 3! = 6
elements in the
list.
* by efficient I mean that you should not initially create a bigger list like all partitions and then filter out results. You should do it directly.
Extra Requirements
If an argument is 0 treat it as if it was not there, e.g.
createOrderedPartitions(2,0,1,1)
should yield the same result as
createOrderedPartitions(2,1,1)
. But at least one argument must not be 0.
Of course all arguments must be >= 0.
Remarks
The provided pseudo code is quasi Java but the language of the solution doesn't matter. In fact, as long as the solution is fairly general and can be reproduced in other languages it is ideal.
Actually, even better would be a return type of List<Tuple<Set>>
(e.g. when
creating such a function in Python). However, then the arguments wich have
a value of 0 must not be ignored. createOrderedPartitions(2,0,2)
would then
create
[({0,1},{},{2,3}),({0,2},{},{1,3}),({0,3},{},{1,2}),({1,2},{},{0,3}),...]
Background
I need this function to make my mastermind-variation bot more efficient and
most of all the code more "beautiful". Take a look at the filterCandidates
function in my source code. There are unnecessary
/ duplicate queries because I'm simply using permutations instead of
specifically ordered partitions. Also, I'm just interested in how to write
this function.
My ideas for (ugly) "solutions"
Create the powerset of {0,...,n_1+...+n_k}
, filter out the subsets of size
n_1, n_2
etc. and create the cartesian product of the n subsets. However
this won't actually work because there would be duplicates, e.g.
({1,2},{1})...
First choose n_1
of x = {0,...,n_1+n_2+...+n_n-1}
and put them in the
first set. Then choose n_2
of x without the n_1 chosen elements
beforehand
and so on. You then get for example ({0,2},{},{1,3},{4})
. Of
course, every possible combination must be created so ({0,4},{},{1,3},{2})
,
too, and so on. Seems rather hard to implement but might be possible.
Research
I guess this goes in the direction I want however I don't see how I can utilize it for my specific scenario.
© Stack Overflow or respective owner