This iterator is my 'naive' approach to producing [google://derangements] via an iterator (at [japhy]'s prompting so he can produce his "Secret Santa" lists). A derangement is a permutation where none of the elements remained in their starting positions.
#!/usr/bin/perl -w
use strict;
sub derange
{
my @set= @_; # items (strings) to be deranged
my $last= $#set; # last index into our list
my @stack= # lists of indices to be tried at each location
[ reverse 0 .. $last ];
my @redo; # lists of indices already tried at each location
my @ret; # offsets to each selected item
my $i= 0; # which slot we are trying to fill
my $left= $stack[$i]; # indices to consider for the current slot
return sub {
while( 1 ) {
do {
if( ! @$left ) {
return if --$i < 0;
$left= $stack[$i];
push @{$redo[$i]}, $ret[$i];
}
if( @$left && $i == $left->[-1] ) {
# skip this index as it'd not make a derangement
push @{$redo[$i]}, pop @$left;
}
} while( ! @$left );
$ret[$i]= pop @$left;
if( $i == $last ) {
return @set[@ret];
}
$left= [ @$left, @{$redo[$i]} ];
$redo[++$i]= [];
$stack[$i]= $left;
}
};
}
@ARGV= 1..5 if ! @ARGV;
my $iter= derange( @ARGV );
my @list;
while( @list= $iter->() ) {
print "@list\n";
}
- [tye]
I know it's cool to do iterators, but something about derange { print "@_\n" } 1 .. 5; seems more perlish to me...
sub _derange
{
my( $cb, $todo, @v ) = @_;
@$todo or return $cb->( @v );
my %seen; @seen{@v}=();
my( $range, @todo ) = @$todo;
_derange( $cb, \@todo, @v, $_ )
for grep { ! exists $seen{$_} } @$range;
}
sub derange(&@)
{
my $cb = shift;
_derange( $cb,
[ map { my $x = $_; [ grep { $_ ne $x } @_ ] } @_ ] );
}
If one needed to allow for deranging a list which contains duplicates, one could simply derange the list of its indices. E.g.:
my @x = ( 1 .. 4, 4 );
derange { print "@x[@_]\n" } 0 .. $#x;
I don't care about "cool". I care about "useful". Callbacks are fundamentally inflexible ([id://243835]).
Note that turning my iterator into your callback interface is trivial:
sub forDerange(&@)
{
my $cv= shift @_;
my $iter= genDerange( @_ );
my @list;
while( @list= $iter->() ) {
$cv->( @list );
}
}
Try to go the other way. (:
- [tye]
I didn't have to rewrite my iterator, just use it. I believe you are pointing out ways to make it easier to rewrite a naive recursive solution so that it can be re-implemented as an iterator instead.
By "Try to go the other way" I meant (at least in part), try to use jdporter's callback interface (as-is) in a situation where an iterator is needed.
Though, I don't even understand what you linked to yet so I still consider what I did to go from iterator to callback to be "trivial" compared to what you are proposing as "not too difficult". (:
- tye
Not even understanding [jdporter]'s algorithm, I converted it to be iterative (or perhaps more precisely, lazily evaluated). I made the derange function print only the first 15 results, for convenient testing of large inputs.
sub _derange_iter {
my ($cb, $todo, @v) = @_;
@$todo or return do { $cb->( @v ); sub {} }; # this line was wrong before
my %seen;
@seen{@v} = ();
my ( $range, @todo ) = @$todo;
my @sub_iter = map {
my $my_ = $_;
sub { _derange_iter ( $cb, \@todo, @v, $my_ ) }
} grep { ! exists $seen{$_} } @$range;
return sub {} unless (@sub_iter);
# Grab and unwrap an iterator from the list
my $iter = (shift @sub_iter)->();
return sub {
my $rval;
$iter = (shift @sub_iter)->()
until ($rval = $iter->() or @sub_iter == 0);
return $rval;
}
}
sub derange(&@)
{
my $cb = shift;
my $iter = _derange_iter( $cb,
[ map { my $x = $_; [ grep { $_ ne $x } @_ ] } @_ ] );
for (1..15) {
$iter->();
}
}
derange { print "@_\n" } @ARGV;
But the iterator already existed. I find it easier to use the iterator to make a callback then to reimplement the algorithm recursively... (:
But enough good-natured intentionally talking at cross purposes... I was curious in what order your program produced the derangements and it told me:
b a d c b a d c b a d c b a d c ...
So I'll still keep my iterator-from-the-get-go. ;)
- [tye]
Most programming languages support collections, represented by an in-memory data structure, a file, a database, or a generating function. A programming language system gives us typically one of the two interfaces to systematically access elements of a collection. One traversal API is based on enumerators -- e.g., for-each, map, filter higher-order procedures -- of which the most general is fold. The second approach relies on streams, a.k.a. cursors, lazy lists. Generators such as the ones in Icon, Ruby and Python are a hybrid approach.It is well-known that given a cursor interface to a collection, we can implement an enumerator. It is less appreciated that given an enumerator interface, we can always derive a cursor -- in an automatic way. We demonstrate that generic procedure for languages with and without first-class continuations.
Now that cursors and enumerators are inter-convertible, an implementor of a collection has a choice: which of the two interfaces to implement natively? We argue that he should offer the enumerator interface as the native one. The paper elaborates that enumerators are superior: in efficiency; in ease of programming; in more predictable resource utilization and avoidance of resource leaks. We present a design of the overall optimal collection traversal interface, which is based on a left-fold-like combinator with premature termination. The design has been implemented and tested in practice.
Only having read the two abstracts, it seems to me that the authors rely on a Scheme-like language that has continuations or at least coroutines. With coroutines, it's trivial to convert between an "enumerator" (callback) and a "stream" (list/iterator). Without a coroutine mechanism, it's not as easy. As you seem to have read the paper, can you maybe post a link to the actual paper or, even better, give an application of the automatic way discussed there in Perl?
Using the Coro.pm module, it's quite easy to have generators and to convert between enumerator and iterator, but Coro has the disadvantage of relying on a GPLed library and it doesn't (immediately) work on Win32. Without Coro, one has to manage the stack oneself and/or to create a large buffer for all values passed by the enumerator from what I know. But maybe the paper shows a technique I don't know (yet).
Update: I had another look at Coro, and it isn't under the GPL. I also found that half-support for Win32/MSVC is there, now. Yay! ;)
The mechanical inversion procedure presented in * had a catch: it relies on shift/reset (or call/cc plus a mutable cell, which is the same thing). How can we do such an inversion in Haskell? We can introduce a right fold enumerator, which is more amenable to such transformations. Or we can use a continuation monad and emulate shift/reset. The present article demonstrates the third approach: a non-recursive left-fold. We argue that such a left fold is the best interface for a collection. Indeed, given the non-recursive left-fold we can:If we turn two enumerators into streams, we can *safely* interleave these streams.
- instantiate it into the ordinary left fold
- instantiate in into a stream
We should point out that the relation between the left fold, the non-recursive left fold and the stream is deep. The ordinary, recursive left fold is the fix point of the non-recursive one. On the other hand, the instantiation of the non-recursive left fold as a stream, as we shall see, effectively captures a continuation in a monadic action. We see once again that call/cc and Y are indeed two sides of the same coin **.
The rest of the article demonstrates the inversion procedure. The procedure is generic, as evidenced by its polymorphic type. We illustrate the technique on an example of a file considered a collection of characters. Haskell provides a stream interface to that collection: hGetChar. We implement a left fold enumerator. We then turn that enumerator back to a stream: we implement a function 'myhgetchar' _only_ in terms of the left fold enumerator. The approach is general and uses no monadic heavy lifting.
Re^6: Derangements iterator (callbacks)created: 2006-01-02 07:33:49Well, seeing as apparently you understand this article maybe you could give us an example how to turn foreach (@list){ ... } into a cursor based approach. Since perl doesnt support first class continuations I guess you will need to implement this "left fold" operator. Which in itself would be pretty interesting. Actually even explaining in normal english (unlike the functional jargon gobbly-gook that the article uses) what this "left fold" operator does would be nice.
---
$world=~s/war/peace/g
Re^7: Derangements iterator (callbacks)created: 2006-01-02 14:27:31#!/usr/bin/perl -w # A non-recursive left fold (foldl), taken from Language::Functional sub foldl(&$$) { my($f, $z, $xs) = @_; map { $z = $f->($z, $_) } @{$xs}; return $z; } # Recursive foldl sub foldl_rec { my($f, $z, $xs) = @_; my($head, @tail) = @$xs; $head ? foldl_rec($f, $f->($z,$head), \@tail) : $z; } # "Fold" is the universal list traversal function. Also known as # "reduce" (see List::Util) and "accumulate" (C++ STL). Any function # you write that munges lists (map, grep, etc.) can be rewritten in # terms of a fold. It essentially takes a list and replaces each "cons" # constructor with a function. Stated another way, if you have a list # @a = (1, 2, 3, 4), fold will replace the commas with another function # of your choosing. Let's say you want the sum of the elements in @a. # Replace the commas with a '+' sign, (1 + 2 + 3 + 4). Easy isn't it? # You might write it as... $s = foldl(sub{ $_[0] + $_[1] }, 0, [1..4]); print "sum = $s\n"; # 10 # ...in addition to providing the function and the list, you supply an # initial value to start out with. In the case of $sum above, we use # 0. If you want the product of the elements in the list you can change # to... $p = foldl(sub{ $_[0] * $_[1] }, 1, [1..4]); print "product = $p\n"; # 24 # The "left" portion comes into play because we start at the left end of # the list and work towards the right. The actual sum that is # calculated is (((((0+1)+2)+3)+4). It only makes a difference when the # function used isn't associative. Subtraction is an example... $l = foldl(sub{ $_[0] - $_[1] }, 0, [1..4]); print "left fold subtraction = $l\n"; # ((((0-1)-2)-3)-4) == -10 # Recursive foldr sub foldr_rec { my($f, $z, $xs) = @_; my($head, @tail) = @$xs; $head ? $f->($head,foldr_rec($f, $z, \@tail)) : $z; } $r = foldr_rec(sub{ $_[0] - $_[1] }, 0, [1..4]); print "right fold subtraction = $r\n"; # (1-(2-(3-(4-0)))) == -2 # The dual of "fold" is the universal list creation function, "unfold". # See more unfold in action... # # http://use.perl.org/~Greg%20Buchholz/journal/26747Re^7: Derangements iterator (callbacks)created: 2006-01-14 14:33:35Actually even explaining in normal english (unlike the functional jargon gobbly-gook that the article uses) what this "left fold" operator does would be nice.Another explaination of the original paper.
use List::Util 'shuffle'; my @from = shuffle(@ARGV); print "$from[$_-1] => $from[$_]\n" for 0..$#from;
That doesn't guarantee that the constraint of derangement is met. Example scenario:
1 2 3 # original 2 1 3 # shuffled 1 3 2 # rotated one place to the left.
We can just use the shuffle algorithm slightly modified to suit our needs :
use Inline C => <<'END_OF_C_CODE';
void cderange(SV* array_ref) {
AV* array;
I32 index, i;
SV** sv_1, **sv_2;
SV* sv_temp;
if (! SvROK(array_ref))
croak("array_ref is not a reference");
srand(time( NULL ));
array = (AV*)SvRV(array_ref);
index = av_len(array);
for (; index; index--) {
i = (I32) (rand() % (index));
sv_1 = av_fetch(array, index, 0);
sv_2 = av_fetch(array, i, 0);
sv_temp = *sv_1;
*sv_1 = *sv_2;
*sv_2 = sv_temp;
}
return;
}
END_OF_C_CODE
sub derange {
my $first = $_[0];
cderange \@_;
if( $first eq $_[0] ){
($_[0], $_[1]) = ($_[1], $_[0])
}
return @_;
}
This is fast, but don't work very well with very big arrays (size over RANDMAX, usually 32000).
--
use Inline C => <<'END_OF_C_CODE';
void cderange ( SV* truc, ... ){
SV* sv_temp;
I32 index, i;
Inline_Stack_Vars;
srand(time( NULL ));
index = Inline_Stack_Items;
for (; index; index--) {
i = (I32) (rand() % (index));
sv_temp = Inline_Stack_Item(index);
Inline_Stack_Item(index) = Inline_Stack_Item(i);
Inline_Stack_Item(i) = sv_temp;
}
Inline_Stack_Done;
}
END_OF_C_CODE
Very fast, but this code is subject to the same caveat as Algorithm::Numerical::Shuffle.
A derangement is a permutation where none of the elements remained in their starting positions.
I seem to get that any time I try to use any sort I've written... or many other things I wrote, come to think of it...
this version uses the services of The (Combinatorial) Object Server.
use LWP::Simple;
my $cosder = "http://www.theory.csc.uvic.ca/~cos/per/perm/perm.pl.cgi?program=Derange&output1=true&n=";
sub derangements
{
map[@_[split/, /]],get($cosder.@_)=~/(.*?)
/g
}
print "@$_\n" for derangements( qw( aleph beth gimel daleth ) );
Unfortunately, they don't appear to have an xml or text/plain option.
We're building the house of the future together.
Re: Derangements iterator
created: 2005-12-31 04:20:46
IIRC, iterators for derangements are provided by Math::Combinatorics (pure perl) and Algorithm::Combinatorics (XS).
Re^2: Derangements iterator (others)
created: 2005-12-31 14:44:41
japhy claimed to have looked at several implementations but been disappointed with each of them. I think he said that each was either too esoteric for him to understand easily enough or did too much work generating permutations that had to be skipped.
I felt that I had a rather straight-forward approach that wouldn't backtrack much at all. It is very much like Algorithm::Loops::NestedLoops(), except I attempt to build the list of values to loop over next (the offsets not currently selected) more efficiently by keeping track as I go. But I think I can do this more efficiently still.
So the code just moves along selecting the next item (actually its offset) from the list of items not selected earlier in the list and not at the same offset (and not previously selected for this slot during the current 'round').
This approach occasionally has to 'backtrack', but (I believe) this only happen when it gets to the last slot and does that at most once per derangement returned. So trying to look ahead to prevent this tiny amount of backtracking would actually be slower than the 'naive' approach.
I looked at the code for Algorithm::Combinatorics and saw that it was using the lexical-order permutation algorithm1 modified to try to skip non-derangements somewhat efficiently. I had rejected this approach as a first choice because it contains a step where you reverse a part of your list and that can place one or more items back into their original positions in such a way that it would be tricky to quickly jump to the next permutation that is a derangement. And the comments implied that it did have to skip many permutations because of this.
So, based on japhy's assessment I didn't look at other implementations. Thanks for pointing those out.
1 The classical lexical-order permutation algorithm is very similar to Algorithm::Loops::NextPermute() except for not dealing with duplicate values, something that I have yet to see done outside of my Algorithm::Loops.
- tye
Re^3: Derangements iterator (others)
created: 2006-01-02 09:56:08
(Re: Algorithm::Combinatorics) the logic in principle can be refined to skip some more permutations, but benchmarks showed no difference whatsoever, so I left the code that is easier to understand and added a comment about it:
/* I tried an alternative approach that would in theory avoid the
generation of some permutations with fixed-points: keeping track of
the leftmost fixed-point, and reversing the elements to its right.
But benchmarks up to n = 11 showed no difference whatsoever.
Thus, I left this version, which is simpler.
That n = 11 does not mean there was a difference for n = 12, it
means I stopped benchmarking at n = 11. */
The current interface guarantees lexicographic order, but I plan to provide more algorithms that relax that condition if you don't need it and faster generators are available. I will write it before I die ideally.
Re^4: Derangements iterator (order)
created: 2006-01-02 15:42:43
Thanks for the explanation.
The current interface guarantees lexicographic order
It is easy to adjust my iterator to get lexicographic order. For example, just add one [reverse]:
$left= [ @$left, reverse @{$redo[$i]} ];
I plan to add a version to [cpan://Algorithm::Loops] before I die. (:
- [tye]
Re^5: Derangements iterator (NestedLoops implementation)
created: 2006-01-04 11:57:17
Here's a version you can add to Algorithm::Loops. It uses NestedLoops and is comparatively efficient about skipping invalid combinations. It also puts its results in numeric/lexicographic order and handles duplicates. The commented-out code is for watching/debugging the management of the "pool" of available numbers.
use strict;
use warnings;
use Algorithm::Loops 'NestedLoops';
sub derange {
# Generate the list of possible values at each position
# Skip a value if it's already used up (no more in the pool) or is in its original position
my %pool;
++$pool{$_} for (@_);
my @orig = @_;
no warnings 'numeric';
my @values = sort {$a <=> $b or $a cmp $b} keys %pool;
my @prev_values;
NestedLoops(
[(sub {
# Generate all candidate values for this position
# print "Generating with \@_ = @_\n";
my $pos = @_;
# Update the pool: the last value on @_ has just changed, so
# return the previous value(s) to the pool and remove the new
# one.
if (@_) {
for (grep {defined $prev_values[$_]} $#_..$#orig-1) {
#print "Returning $prev_values[$_] to the pool\n";
++$pool{$prev_values[$_]};
undef $prev_values[$_];
}
#print "Removing $_[-1] from the pool\n";
--$pool{$_[-1]};
#print "Valid values in the pool:\n";
#while (my ($k,$v) = each %pool) {
# print "$k: $v\n" if $v;
#}
$prev_values[$#_] = $_[-1];
}
[ grep {$orig[$pos] ne $_ and $pool{$_} > 0} @values ]
}) x @orig]
);
}
my @results;
my $iter = derange(@ARGV);
print "@results\n" while @results = $iter->();
As a possibly interesting note: if you simply remove $orig[$pos] ne $_ and, it becomes a permutations generator. But derange is not simply a permutations generator that filters out individual invalid permutations; it prunes entire sub-trees. (Similarly, the permutation generator doesn't loop through the whole cartesian space and filter it.)
Caution: Contents may have been coded under pressure.
Re: Derangements iterator (handles duplicate values)
created: 2006-01-03 14:34:16
I've come up with a solution that handles duplicate values properly: that is,
a b b a
generates exactly
b a a b
I wrote it as a recursive sub, and then converted it to be an iterator.
use strict;
use warnings;
# The recursive one
sub derange {
# First argument is optional arrayref of original values; if not provided,
# build it from arg list
my @orig = ref $_[0] ? @{shift(@_)} : @_;
if (@_ == 0) { return [] }
if (@_ == 1) {
return $_[0] eq $orig[0] ? () : [$_[0]];
}
# Generate a derangement by extracting each element then mapping it as the head
# to the derangement of the everything else
# Swaps that would cause a value to match its corresponding original value are skipped
my %seen;
map {
if ($orig[$_] ne $_[0] and $orig[0] ne $_[$_] and not $seen{$_[$_]}++) {
my $swap_i = $_;
map [$_[$swap_i], @$_]
, derange([@orig[1..$#orig]], @_[0..$swap_i-1,$swap_i+1..$#_]);
}
else {
();
}
} (0..$#_);
}
# The iterator version
sub derange_iter {
# First argument is optional arrayref of original values; if not provided,
# build it from arg list
my @orig = ref $_[0] ? @{shift(@_)} : @_;
# Base cases get assigned to an array, which the iterator shifts through
if (@_ == 0) {
my @base_case = ([]);
return sub { shift @base_case };
}
elsif (@_ == 1) {
my @base_case = $_[0] eq $orig[0] ? () : [$_[0]];
return sub { shift @base_case };
}
# otherwise..
my %seen;
my @list = @_;
my @sub_iter = map {
if ($orig[$_] ne $list[0] and $orig[0] ne $list[$_] and not $seen{$list[$_]}++) {
my $swap_i = $_;
sub {
my $cdr_iter = derange_iter([@orig[1..$#orig]], @list[0..$swap_i-1,$swap_i+1..$#list]);
sub {
my $cdr = $cdr_iter->();
if ($cdr) { return [$list[$swap_i], @$cdr] }
else { return () }
};
}
}
else {
();
}
} (0..$#_);
# Grab and unwrap an iterator from the list
my $iter = (shift @sub_iter)->();
return sub {
my $rval;
$iter = (shift @sub_iter)->()
until ($rval = $iter->() or @sub_iter == 0);
return $rval;
}
}
@ARGV or @ARGV = 1..5;
my $i = derange_iter(@ARGV);
for (1..50) {
my $val = $i->() or last;
print "@$val\n";
}
#print "\nShould be:\n";
#print "@$_\n" for sort {"@$a" cmp "@$b"} derange(@ARGV);
The commented-out code at the end is for verifying that the iterator generates the same output as the recursive version. But don't uncomment it if you want to run on large inputs. The iterator will spit out the first 50 values almost immediately; the recursive version will hang/crash.
Caution: Contents may have been coded under pressure.
perlmonks.org content © perlmonks.org and Anonymous Monk, Corion, demerphq, fxn, jdporter, Jedaï, pKai, Roy Johnson, spiritway, tye
prlmnks.org © 2006
edmund von der burg
(eccles & toad)
v 0.03