Algorithm-Combinatorics
view release on metacpan or search on metacpan
Combinatorics.pm view on Meta::CPAN
}
The C<next()> method returns an arrayref to the next tuple, if any, or C<undef> if the
sequence is exhausted.
Memory usage is minimal, no recursion and no stacks are involved.
=item *
In list context subroutines slurp the entire set of tuples. This behaviour is offered
for convenience, but take into account that the resulting array may be really huge:
my @all_combinations = combinations(\@data, $k);
=back
=head2 permutations(\@data)
The permutations of C<@data> are all its reorderings. For example, the permutations of C<@data = (1, 2, 3)> are:
(1, 2, 3)
(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)
( run in 3.692 seconds using v1.01-cache-2.11-cpan-cdf2f3d4e48 )