Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re^3: Generating powerset with progressive ordering

by Roy Johnson (Monsignor)
on Apr 26, 2005 at 20:20 UTC ( #451756=note: print w/replies, xml ) Need Help??

in reply to Re^2: Generating powerset with progressive ordering
in thread Generating powerset with progressive ordering

In hopes of better understanding what you were doing, I implemented powersets first as a recursive function and then as an iterator that tries to parallel the recursive version. I thought you might be interested to see what I came up with.
# Powerset: the set of all subsets -- (for illustration only, not used + below) sub powerset { my ($car, @cdr) = @_; my @cdr_powerset = @cdr ? powerset(@cdr) : (); return ([$car], map([$car, @$_], @cdr_powerset), @cdr_powerset); } sub iter_powerset { my ($car, @cdr) = @_; my $cdr_power_iter = @cdr ? iter_powerset(@cdr) : (); my $mode = 0; return sub { if ($mode == 0) { ++$mode; return [$car]; } elsif ($mode == 1) { my $tail = $cdr_power_iter ? $cdr_power_iter->() : []; if (@$tail) { return [$car, @$tail]; } else { ++$mode; } } my $tail = $cdr_power_iter ? $cdr_power_iter->() : []; if (@$tail) { return [@$tail]; } else { $mode = 0; return []; } } } my $iter = iter_powerset(2,3,5,7,11); print "@$_\n" while (@{$_ = $iter->()});
The recursive version returns a list of three sub-lists: the first element, the first element mapped onto all the powerset of the tail, and the powerset of the tail. These are the three modes that an iterator can be in.

My iterator factory begins by recursively calling itself, creating an iterator for the tail of the list, then it returns a closure. The code in the closure checks the mode and uses the tail-iterator (if necessary) to generate the appropriate set: In Mode 0, just the first element is returned; in Mode 1, the first element and the next powerset of the tail; Mode 1 may "fall through" into Mode 2, where just the next powerset of the tail is returned. Upon reaching the end of its powerset, the iterator returns an empty arrayref, and resets the mode to 0 so it can be re-run.

The recursive use of the factory simplifies bookkeeping quite a bit, I think, and it only generates N iterators for a list of N elements, so it's in keeping with the no-explosive-growth objective of having iterators. It also begins cranking out results immediately, even when called with 1..45 as arguments, unlike the recursive version.

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^4: Generating powerset with progressive ordering
by tlm (Prior) on Apr 26, 2005 at 23:46 UTC

    A powerset should always include the empty set. (The cardinality of the powerset of a set of cardinality n should be 2n.) Therefore, you can simplify powerset:

    sub powerset { my ( $car, @cdr ) = @_; my @cdr_powerset = @cdr ? powerset( @cdr ) : []; return ( @cdr_powerset, map [ $car, @$_ ], @cdr_powerset ); }
    I imagine that this means that only two states are needed in iter_powerset.

    the lowliest monk

      That does simplify it, but it throws off the progressive ordering, which worked out almost magically the way I had it written. For the problem L~R was looking to solve, it was not useful to consider the empty set, either. Probably I should rename the function "non-empty powerset". :-)

      ++ for technical merit.

      Caution: Contents may have been coded under pressure.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://451756]
[Your Mother]: (Work in progress.)
[marto]: unaware that the model in question started the cycle automatically, provided the money/token had already been provided
[marto]: CCTV footage of the joke gone wrong is used in educational materials now IIRC
[ambrus]: oiskuu: definitely not silly. have you ever tried to remove cat hair or dog hair from clothes? for many clothes, it's pretty hard, and running the washing machine with a cat inside would get the hair on everything.
[oiskuu]: YM, I'm interested in your questions... maybe start a a thread at polls.
[oiskuu]: marto, it ended badly? I doubt the power is easy to plug at those installations?
[marto]: oiskuu sadly yes
[Your Mother]: My cat used to climb into the freezer when I opened it. Once I closed it and left him for 5 minutes hoping to disuade the habit. When I opened it expecting a cat eager to never return he just stared at me. Had to pull him out.

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (12)
As of 2017-12-18 15:02 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (488 votes). Check out past polls.