Algorithm-Combinatorics
view release on metacpan or search on metacpan
Combinatorics.pm view on Meta::CPAN
return if $finished;
my $subset = __next_subset($data, \@odometer);
$finished = 1 if @$subset == 0;
$subset;
});
return __contextualize($iter);
}
sub variations {
my ($data, $k) = @_;
__check_params($data, $k);
return __contextualize(__null_iter()) if $k < 0;
return __contextualize(__once_iter()) if $k == 0;
if ($k > @$data) {
carp("Parameter k is greater than the size of data");
return __contextualize(__null_iter());
}
# permutations() is more efficient because it knows
# all indices are always used
return permutations($data) if @$data == $k;
my @indices = 0..($k-1);
my @used = ((1) x $k, (0) x (@$data-$k));
my $iter = Algorithm::Combinatorics::Iterator->new(sub {
__next_variation(\@indices, \@used, @$data-1) == -1 ? undef : [ @{$data}[@indices] ];
}, [ @{$data}[@indices] ]);
return __contextualize($iter);
}
*tuples = \&variations;
sub variations_with_repetition {
my ($data, $k) = @_;
__check_params($data, $k);
return __contextualize(__null_iter()) if $k < 0;
return __contextualize(__once_iter()) if $k == 0;
my @indices = (0) x $k;
my $iter = Algorithm::Combinatorics::Iterator->new(sub {
__next_variation_with_repetition(\@indices, @$data-1) == -1 ? undef : [ @{$data}[@indices] ];
}, [ @{$data}[@indices] ]);
return __contextualize($iter);
}
*tuples_with_repetition = \&variations_with_repetition;
sub __variations_with_repetition_gray_code {
my ($data, $k) = @_;
__check_params($data, $k);
return __contextualize(__null_iter()) if $k < 0;
return __contextualize(__once_iter()) if $k == 0;
my @indices = (0) x $k;
my @focus_pointers = 0..$k; # yeah, length $k+1
my @directions = (1) x $k;
my $iter = Algorithm::Combinatorics::Iterator->new(sub {
__next_variation_with_repetition_gray_code(
\@indices,
\@focus_pointers,
\@directions,
@$data-1,
) == -1 ? undef : [ @{$data}[@indices] ];
}, [ @{$data}[@indices] ]);
return __contextualize($iter);
}
sub permutations {
my ($data) = @_;
__check_params($data, 0);
return __contextualize(__once_iter()) if @$data == 0;
my @indices = 0..(@$data-1);
my $iter = Algorithm::Combinatorics::Iterator->new(sub {
__next_permutation(\@indices) == -1 ? undef : [ @{$data}[@indices] ];
}, [ @{$data}[@indices] ]);
return __contextualize($iter);
}
sub circular_permutations {
my ($data) = @_;
__check_params($data, 0);
return __contextualize(__once_iter()) if @$data == 0;
return __contextualize(__once_iter([@$data])) if @$data == 1 || @$data == 2;
my @indices = 1..(@$data-1);
my $iter = Algorithm::Combinatorics::Iterator->new(sub {
__next_permutation(\@indices) == -1 ? undef : [ @{$data}[0, @indices] ];
}, [ @{$data}[0, @indices] ]);
return __contextualize($iter);
}
sub __permutations_heap {
my ($data) = @_;
__check_params($data, 0);
return __contextualize(__once_iter()) if @$data == 0;
my @a = 0..(@$data-1);
my @c = (0) x (@$data+1); # yeah, there's an spurious $c[0] to make the notation coincide
my $iter = Algorithm::Combinatorics::Iterator->new(sub {
__next_permutation_heap(\@a, \@c) == -1 ? undef : [ @{$data}[@a] ];
}, [ @{$data}[@a] ]);
return __contextualize($iter);
}
Combinatorics.pm view on Meta::CPAN
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
The number of permutations of C<n> elements is:
n! = 1, if n = 0
n! = n*(n-1)*...*1, if n > 0
See some values at L<http://www.research.att.com/~njas/sequences/A000142>.
=head2 circular_permutations(\@data)
The circular permutations of C<@data> are its arrangements around a circle, where only relative order of elements matter, rather than their actual position. Think possible arrangements of people around a circular table for dinner according to whom th...
For example the circular permutations of C<@data = (1, 2, 3, 4)> are:
(1, 2, 3, 4)
(1, 2, 4, 3)
(1, 3, 2, 4)
(1, 3, 4, 2)
(1, 4, 2, 3)
(1, 4, 3, 2)
The number of circular permutations of C<n> elements is:
n! = 1, if 0 <= n <= 1
(n-1)! = (n-1)*(n-2)*...*1, if n > 1
See a few numbers in a comment of L<http://www.research.att.com/~njas/sequences/A000142>.
=head2 derangements(\@data)
The derangements of C<@data> are those reorderings that have no element
in its original place. In jargon those are the permutations of C<@data>
with no fixed points. For example, the derangements of C<@data = (1, 2,
3)> are:
(2, 3, 1)
(3, 1, 2)
The number of derangements of C<n> elements is:
d(n) = 1, if n = 0
d(n) = n*d(n-1) + (-1)**n, if n > 0
See some values at L<http://www.research.att.com/~njas/sequences/A000166>.
=head2 complete_permutations(\@data)
This is an alias for C<derangements>, documented above.
=head2 variations(\@data, $k)
The variations of length C<$k> of C<@data> are all the tuples of length C<$k> consisting of elements of C<@data>. For example, for C<@data = (1, 2, 3)> and C<$k = 2>:
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
For this to make sense, C<$k> has to be less than or equal to the length of C<@data>.
Note that
permutations(\@data);
is equivalent to
variations(\@data, scalar @data);
The number of variations of C<n> elements taken in groups of C<k> is:
v(n, k) = 1, if k = 0
v(n, k) = n*(n-1)*...*(n-k+1), if 0 < k <= n
=head2 variations_with_repetition(\@data, $k)
The variations with repetition of length C<$k> of C<@data> are all the tuples of length C<$k> consisting of elements of C<@data>, including repetitions. For example, for C<@data = (1, 2, 3)> and C<$k = 2>:
(1, 1)
(1, 2)
(1, 3)
(2, 1)
(2, 2)
(2, 3)
(3, 1)
(3, 2)
(3, 3)
Note that C<$k> can be greater than the length of C<@data>. For example, for C<@data = (1, 2)> and C<$k = 3>:
(1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 2, 2)
(2, 1, 1)
(2, 1, 2)
(2, 2, 1)
(2, 2, 2)
The number of variations with repetition of C<n> elements taken in groups of C<< k >= 0 >> is:
vr(n, k) = n**k
=head2 tuples(\@data, $k)
This is an alias for C<variations>, documented above.
=head2 tuples_with_repetition(\@data, $k)
This is an alias for C<variations_with_repetition>, documented above.
=head2 combinations(\@data, $k)
The combinations of length C<$k> of C<@data> are all the sets of size C<$k> consisting of elements of C<@data>. For example, for C<@data = (1, 2, 3, 4)> and C<$k = 3>:
(1, 2, 3)
(1, 2, 4)
(1, 3, 4)
(2, 3, 4)
For this to make sense, C<$k> has to be less than or equal to the length of C<@data>.
The number of combinations of C<n> elements taken in groups of C<< 0 <= k <= n >> is:
n choose k = n!/(k!*(n-k)!)
=head2 combinations_with_repetition(\@data, $k);
The combinations of length C<$k> of an array C<@data> are all the bags of size C<$k> consisting of elements of C<@data>, with repetitions. For example, for C<@data = (1, 2, 3)> and C<$k = 2>:
(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3)
Note that C<$k> can be greater than the length of C<@data>. For example, for C<@data = (1, 2, 3)> and C<$k = 4>:
(1, 1, 1, 1)
(1, 1, 1, 2)
(1, 1, 1, 3)
(1, 1, 2, 2)
(1, 1, 2, 3)
(1, 1, 3, 3)
(1, 2, 2, 2)
(1, 2, 2, 3)
(1, 2, 3, 3)
(1, 3, 3, 3)
(2, 2, 2, 2)
(2, 2, 2, 3)
(2, 2, 3, 3)
(2, 3, 3, 3)
(3, 3, 3, 3)
The number of combinations with repetition of C<n> elements taken in groups of C<< k >= 0 >> is:
n+k-1 over k = (n+k-1)!/(k!*(n-1)!)
=head2 partitions(\@data[, $k])
A partition of C<@data> is a division of C<@data> in separate pieces. Technically that's a set of subsets of C<@data> which are non-empty, disjoint, and whose union is C<@data>. For example, the partitions of C<@data = (1, 2, 3)> are:
((1, 2, 3))
((1, 2), (3))
((1, 3), (2))
((1), (2, 3))
((1), (2), (3))
This subroutine returns in consequence tuples of tuples. The top-level tuple (an arrayref) represents the partition itself, whose elements are tuples (arrayrefs) in turn, each one representing a subset of C<@data>.
The number of partitions of a set of C<n> elements are known as Bell numbers, and satisfy the recursion:
B(0) = 1
B(n+1) = (n over 0)B(0) + (n over 1)B(1) + ... + (n over n)B(n)
See some values at L<http://www.research.att.com/~njas/sequences/A000110>.
If you pass the optional parameter C<$k>, the subroutine generates only partitions of size C<$k>. This uses an specific algorithm for partitions of known size, which is more efficient than generating all partitions and filtering them by size.
Note that in that case the subsets themselves may have several sizes, it is the number of elements I<of the partition> which is C<$k>. For instance if C<@data> has 5 elements there are partitions of size 2 that consist of a subset of size 2 and its c...
The number of partitions of size C<k> of a set of C<n> elements are known as Stirling numbers of the second kind, and satisfy the recursion:
S(0, 0) = 1
S(n, 0) = 0 if n > 0
S(n, 1) = S(n, n) = 1
S(n, k) = S(n-1, k-1) + kS(n-1, k)
=head2 subsets(\@data[, $k])
This subroutine iterates over the subsets of data, which is assumed to represent a set. If you pass the optional parameter C<$k> the iteration runs over subsets of data of size C<$k>.
The number of subsets of a set of C<n> elements is
2**n
( run in 0.512 second using v1.01-cache-2.11-cpan-140bd7fdf52 )