NAME
Math::Permute::Array - Perl extension for computing any permutation of an array. The permutation could be access by an index in [0,cardinal] or by iterating with prev, cur and next.
SYNOPSIS
use Math::Permute::Array;
print "permutation with direct call to Permutate\n";
my $i;
my @array = (1,2,3);
foreach $i (0..5){
my @tmp = @{Math::Permute::Array::Permute($i,\@array)};
print "@tmp\n";
}
print "permutation with counter\n";
my $p = new Math::Permute::Array(\@array);
foreach $i (0..$p->cardinal()-1){
my @tmp = @{$p->permutation($i)};
print "@tmp\n";
}
print "permutation with next\n";
$p = new Math::Permute::Array(\@array);
my @tmp = @{$p->cur()};
print "@tmp\n";
foreach $i (1..$p->cardinal()-1){
@tmp = @{$p->next()};
print "@tmp\n";
}
print "permutation with prev\n";
my $tmp=\@tmp;
while(defined $tmp){
@tmp = @{$tmp};
print "@tmp\n";
$tmp = $p->prev();
}
print "Apply a function on all permutations\n";
Math::Permute::Array::Apply_on_perms { print "@_\n"} \@array;
the output should be:
permutation with direct call to Permute
1 2 3
2 1 3
3 1 2
1 3 2
2 3 1
3 2 1
1 2 3
permutation with counter
1 2 3
2 1 3
3 1 2
1 3 2
2 3 1
3 2 1
1 2 3
permutation with next
1 2 3
2 1 3
3 1 2
1 3 2
2 3 1
3 2 1
1 2 3
Apply a function on all permutations
1 2 3
2 1 3
3 1 2
1 3 2
2 3 1
3 2 1
1 2 3
DESCRIPTION
This module compute the i^{th} permutation of an array recursively. The main advantage of this module is the fact that you could access to any permutation in the order that you want. Moreover this module doesn't use a lot of memory because the permutation is compute. the cost for computing one permutation is O(n).
it could be optimize by doing this iteratively but it seems efficient. Thus this module doesn't need a lot of memory because the permutation isn't stored.
EXPORT
- Permute [index, $ref_array]
-
Returns a reference on the index^{th} permutation for the array. This function should be called directly as in the example.
- Apply_on_perms [func, $ref_array]
-
Applies the function on each permutation (this interface is efficient but limited).
- new [ref_array]
-
Returns a permutor object for the given items.
- next
-
Called on a permutor, it returns a reference on the array contening the next permutation.
- prev
-
Called on a permutor, it returns a reference on the array contening the previous permutation.
- cur
-
Called on a permutor, it returns a reference on the array contening the current permutation.
- permutation [index, @array]
-
Called on a permutor, it returns a reference on a array contening index^{th} permutation for the array.
- cardinal
-
Called on a permutor, it returns the number of permutations
Internal functions
- factorial [n]
-
returns the factorial of n. This is a internal function to calculate the number of permutations.
SEE ALSO
AUTHOR
jean-noel quintin, <quintin_at_imag_dot_fr>
COPYRIGHT AND LICENSE
Copyright (C) 2012 by jean-noel quintin
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.12.4 or, at your option, any later version of Perl 5 you may have available.