http://www.perlmonks.org?node_id=29374

This permutation generator generates only unique permutations, even when the list to be permuted contains duplicates. It can be used for huge lists since the list of permutations is not built in memory (and we don't have to keep any "context" info around). To use it, first sort the array, then each call to nextPermute() will permute the array in place, returning false when all permutations have been done (which also means that the array was just in reverse sorted order and has been left that way).

Note that the permutations get generated in "sorted order". For example, if all of the items are gt " ", then the output of the sample usage would already be sorted.

The call to reverse was originally a call to sort. When I noticed this inefficiency, I compared the improved version against Algorith::Permute and all of the permutation finders that it included in its benchmark. My (improved) code was faster than all of them except Algorith::Permute's [ which are written in XS, so being only about 1/2 as fast was quite acceptable to me (: ]. Note also that the version below handles duplicates while none of the other fine algorithms did.

Update: Did s/nextpermute/nextPermute/.

Update: Now available as part of Algorithm::Loops.

Update: Replaced 'while' with 'until' as the comparisons where "backward" (implied negations).

sub nextPermute(\@) { my( $vals )= @_; my $last= $#{$vals}; return "" if $last < 1; # Find last item not in reverse-sorted order: my $i= $last-1; $i-- until $i < 0 || $vals->[$i] lt $vals->[$i+1]; # If complete reverse sort, we are done! return "" if -1 == $i; # Re-sort the reversely-sorted tail of the list: @{$vals}[$i+1..$last]= reverse @{$vals}[$i+1..$last] if $vals->[$i+1] gt $vals->[$last]; # Find next item that will make us "greater": my $j= $i+1; $j++ until $vals->[$i] lt $vals->[$j]; # Swap: @{$vals}[$i,$j]= @{$vals}[$j,$i]; return 1; } #Code to make the sample use below friendly: if( 0 == @ARGV ) { die "Usage: $0 word\n", " or: $0 t o k e n s\n", "Prints all unique permutations of the letters or words given. +\n"; } elsif( 1 == @ARGV ) { @ARGV= $ARGV[0] =~ /(.)/gs; $"= ""; } #Sample use: @ARGV= sort @ARGV; do { print "@ARGV\n"; } while( nextPermute(@ARGV) );

Replies are listed 'Best First'.
Re: Permuting with duplicates and no memory
by dragonchild (Archbishop) on Mar 12, 2002 at 22:05 UTC
    If you want a nice numeric permutation iterator-making closure (as I did), here's how I'd write it (based completely on the above node).
    sub make_orderings { my $num = shift; my @arr = (1 .. $num); return sub { my $last = $#arr; my $i = $last - 1; $i-- while 0 <= $i && $arr[$i] >= $arr[$i+1]; return if $i == -1; @arr[$i+1..$last] = reverse @arr[$i+1..$last] if $arr[$i+1] > $arr[$last]; my $j=$i+1; $j++ while $arr[$i] >= $arr[$j]; @arr[$i,$j] = @arr[$j,$i]; return @arr; } }

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      I borrowed this for something and noticed that if $num=8 this produces only 40319 permutations... I made the following el cheapo minor modification on mine.

      sub make_orderings { my $num = shift; my @arr = (1 .. $num); my $first = 1; return sub { if( $first ) { $first = 0; return @arr; } my $last = $#arr; my $i = $last - 1; $i-- while 0 <= $i && $arr[$i] >= $arr[$i+1]; return if $i == -1; @arr[$i+1..$last] = reverse @arr[$i+1..$last] if $arr[$i+1] > $arr[$last]; my $j=$i+1; $j++ while $arr[$i] >= $arr[$j]; @arr[$i,$j] = @arr[$j,$i]; return @arr; } }

      -Paul

        Just FYI, perhaps you missed this bit from the root node:

        do { print "@ARGV\n"; } while( nextPermut­e(@ARGV) );

        which is also why I called my routine "nextPermute".

        Also note that dragonchild's and my iterators can be reused. For yours to be reusable you probably want to change:

        return if $i == -1;

        to

        if( $i == -1 ) { $first= 1; return; }

        - tye        

Re: Permuting with duplicates and no memory
by spx2 (Deacon) on Mar 02, 2010 at 03:26 UTC
    I feel a bit indebted to write the results of a benchmark I made to find out which of the permutation generators are fast, Algorithm::Permute , tye's implementation , dragonchild's implementation and of course my Steinhaus Johnson Trotter XS implementation:
    Rate tye SJT dchld A::P tye 1.27/s -- -22% -46% -81% SJT 1.63/s 28% -- -30% -76% dchld 2.34/s 84% 44% -- -65% A::P 6.67/s 423% 310% 185% --
    Here's the code I used to make the benchmark:
    use strict; use warnings; use Benchmark qw/cmpthese timethese/; use Algorithm::Permute; use SJT; my $n = 8; # objects to permute my $iter=4; sub nextPermute(\@) { my( $vals )= @_; my $last= $#{$vals}; return "" if $last < 1; # Find last item not in reverse-sorted order: my $i= $last-1; $i-- until $i < 0 || $vals->[$i] lt $vals->[$i+1]; # If complete reverse sort, we are done! return "" if -1 == $i; # Re-sort the reversely-sorted tail of the list: @{$vals}[$i+1..$last]= reverse @{$vals}[$i+1..$last] if $vals->[$i+1] gt $vals->[$last]; # Find next item that will make us "greater": my $j= $i+1; $j++ until $vals->[$i] lt $vals->[$j]; # Swap: @{$vals}[$i,$j]= @{$vals}[$j,$i]; return 1; } sub make_orderings { my $num = shift; my @arr = (1 .. $num); return sub { my $last = $#arr; my $i = $last - 1; $i-- while 0 <= $i && $arr[$i] >= $arr[$i+1]; return if $i == -1; @arr[$i+1..$last] = reverse @arr[$i+1..$last] if $arr[$i+1] > $arr[$last]; my $j=$i+1; $j++ while $arr[$i] >= $arr[$j]; @arr[$i,$j] = @arr[$j,$i]; return @arr; } } cmpthese( $iter, { 'A::P' => sub { use Algorithm::Permute; my $p = new Algorithm::Permute([1..$n], $n); while (my @res = $p->next) { #print join(", ", @res), "\n"; } }, 'SJT' => sub { my $s = SJT->new($n); while($s->next_perm()){ my @p = @{$s->{permutation}}; #$s->print_perm; }; }, 'tye' => sub { my @w= (1..$n); do { } while( nextPermute(@w) ); }, 'dchld' => sub { my $i = make_orderings($n); while(my @a = $i->()){ }; }, } );