Algorithm-Combinatorics
view release on metacpan or search on metacpan
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
The number of permutations of `n' elements is:
n! = 1, if n = 0
n! = n*(n-1)*...*1, if n > 0
See some values at http://www.research.att.com/~njas/sequences/A000142.
circular_permutations(\@data)
The circular permutations of `@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 they have to their right and left, no
matter the actual chair they sit on.
For example the circular permutations of `@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 `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
http://www.research.att.com/~njas/sequences/A000142.
derangements(\@data)
The derangements of `@data' are those reorderings that have no element
in its original place. In jargon those are the permutations of `@data'
with no fixed points. For example, the derangements of `@data = (1, 2,
3)' are:
(2, 3, 1)
(3, 1, 2)
The number of derangements of `n' elements is:
d(n) = 1, if n = 0
d(n) = n*d(n-1) + (-1)**n, if n > 0
See some values at http://www.research.att.com/~njas/sequences/A000166.
complete_permutations(\@data)
This is an alias for `derangements', documented above.
variations(\@data, $k)
The variations of length `$k' of `@data' are all the tuples of length
`$k' consisting of elements of `@data'. For example, for `@data = (1, 2,
3)' and `$k = 2':
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
For this to make sense, `$k' has to be less than or equal to the length
of `@data'.
Note that
permutations(\@data);
is equivalent to
variations(\@data, scalar @data);
The number of variations of `n' elements taken in groups of `k' is:
v(n, k) = 1, if k = 0
v(n, k) = n*(n-1)*...*(n-k+1), if 0 < k <= n
variations_with_repetition(\@data, $k)
The variations with repetition of length `$k' of `@data' are all the
tuples of length `$k' consisting of elements of `@data', including
repetitions. For example, for `@data = (1, 2, 3)' and `$k = 2':
(1, 1)
(1, 2)
(1, 3)
(2, 1)
(2, 2)
(2, 3)
(3, 1)
(3, 2)
(3, 3)
Note that `$k' can be greater than the length of `@data'. For example,
for `@data = (1, 2)' and `$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 `n' elements taken in groups
of `k >= 0' is:
vr(n, k) = n**k
tuples(\@data, $k)
This is an alias for `variations', documented above.
tuples_with_repetition(\@data, $k)
This is an alias for `variations_with_repetition', documented above.
combinations(\@data, $k)
The combinations of length `$k' of `@data' are all the sets of size `$k'
consisting of elements of `@data'. For example, for `@data = (1, 2, 3,
4)' and `$k = 3':
(1, 2, 3)
(1, 2, 4)
(1, 3, 4)
(2, 3, 4)
For this to make sense, `$k' has to be less than or equal to the length
of `@data'.
The number of combinations of `n' elements taken in groups of `0 <= k <=
n' is:
n choose k = n!/(k!*(n-k)!)
combinations_with_repetition(\@data, $k);
The combinations of length `$k' of an array `@data' are all the bags of
size `$k' consisting of elements of `@data', with repetitions. For
example, for `@data = (1, 2, 3)' and `$k = 2':
(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3)
Note that `$k' can be greater than the length of `@data'. For example,
for `@data = (1, 2, 3)' and `$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 `n' elements taken in
groups of `k >= 0' is:
n+k-1 over k = (n+k-1)!/(k!*(n-1)!)
partitions(\@data[, $k])
A partition of `@data' is a division of `@data' in separate pieces.
Technically that's a set of subsets of `@data' which are non-empty,
disjoint, and whose union is `@data'. For example, the partitions of
`@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 `@data'.
The number of partitions of a set of `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 http://www.research.att.com/~njas/sequences/A000110.
If you pass the optional parameter `$k', the subroutine generates only
partitions of size `$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 *of the partition* which is `$k'. For instance
if `@data' has 5 elements there are partitions of size 2 that consist of
a subset of size 2 and its complement of size 3; and partitions of size
2 that consist of a subset of size 1 and its complement of size 4. In
both cases the partitions have the same size, they have two elements.
( run in 0.807 second using v1.01-cache-2.11-cpan-140bd7fdf52 )