watashi has asked for the wisdom of the Perl Monks concerning the following question:

The Math::Combinatorics module facilitates combination generation but is it capable of generating more complex pattern? For example, given a set (a b c d e f g), is there any convenient way to generate lists with a parameter of "k: maximum number of elements to use", say, 3,

to give lists like:
ab c d e f g
a b c d e f g
abc d ef g
aef cdg b

etc.

Replies are listed 'Best First'.
Re: Combinatorics of Math::Combinatorics
by frozenwithjoy (Priest) on Jul 21, 2012 at 03:54 UTC
    As that module currently stands, it looks like you would need to do some (recursive?) combining of combine(1,@n), combine(2,@n), and permute(@n).
Re: Combinatorics of Math::Combinatorics
by BrowserUk (Patriarch) on Jul 21, 2012 at 21:46 UTC

    If your input sets get much bigger than about 18 elements, you will probably want to consider converting this to an iterator form:

    #! perl -slw use strict; sub parts { my $n = shift; return [] unless @_; map { local @_ = @_; my $first = join '', splice @_, 0, $_; map { [ $first, @$_ ] } parts( $n, @_ ); } 1 .. ( @_ < $n ? @_ : $n ); } print join'|', @$_ for parts( 3, 'a'..'g' ); __DATA__ C:\test>982945.pl a|b|c|d|e|f|g a|b|c|d|e|fg a|b|c|d|ef|g a|b|c|d|efg a|b|c|de|f|g a|b|c|de|fg a|b|c|def|g a|b|cd|e|f|g a|b|cd|e|fg a|b|cd|ef|g a|b|cd|efg a|b|cde|f|g a|b|cde|fg a|bc|d|e|f|g a|bc|d|e|fg a|bc|d|ef|g a|bc|d|efg a|bc|de|f|g a|bc|de|fg a|bc|def|g a|bcd|e|f|g a|bcd|e|fg a|bcd|ef|g a|bcd|efg ab|c|d|e|f|g ab|c|d|e|fg ab|c|d|ef|g ab|c|d|efg ab|c|de|f|g ab|c|de|fg ab|c|def|g ab|cd|e|f|g ab|cd|e|fg ab|cd|ef|g ab|cd|efg ab|cde|f|g ab|cde|fg abc|d|e|f|g abc|d|e|fg abc|d|ef|g abc|d|efg abc|de|f|g abc|de|fg abc|def|g

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re: Combinatorics of Math::Combinatorics
by Khen1950fx (Canon) on Jul 21, 2012 at 10:07 UTC
    I would use Algorithm::Combinatorics.
    #!/usr/bin/perl -l BEGIN { $| = 1; $^W = 1; } use strict; use warnings; use Data::Dump qw(pp); use Algorithm::Combinatorics qw(partitions); my(@data) = qw(a b c d e f g); my $iter = partitions(\@data, 3, 4); while (my $p = $iter->next) { print pp($p); }

      From the documentation that you linked to, calling partitions( \@data, 3, 4 ) is not a supported usage. Reading the code linked from there, it appears to be the same as calling partitions( \@data, 3 ) which would produce only partitions composed of exactly 3 subsets, not partitions of any size but with no more than 3 members in each subset of the partition (the latter being what, to me, the OP seems to have asked for).

      - tye        

Re: Combinatorics of Math::Combinatorics (Loops)
by tye (Sage) on Jul 22, 2012 at 02:18 UTC

    Using some slightly simplified code ripped from a future version of Algorithm::Loops I solved it like this (using an iterator so no worries about memory exhaustion for moderately large inputs):

    my @input = ( 'a' .. 'e' ); my $max_subset = 3; my $iter = NestedLoops( [ sub { [0] }, ( sub { [ 0 .. 1+$_ ] } ) x $#input ], sub { return -1 if $max_subset < $_; return 1 if @_ == @input; }, ); my @slots; while( @slots = $iter->() ) { my @subsets = ('') x @input; $subsets[$slots[$_]] .= $input[$_] for 0 .. $#input; print "@subsets\n"; }

    - tye