PowerSet(3p)

NAME
Data::PowerSet - Generate all subsets of a list of elements

VERSION
This document describes version 0.05 of Data::PowerSet, released 2008-05-13.

SYNOPSIS
  use Data::PowerSet 'powerset';

  my $powerset = powerset( 3, 1, 4 );
  for my $p (@$powerset) {
    print "@$p\n";
  }

  # prints
  3 1 4
  1 4
  3 4
  4
  3 1
  1
  3

An object-oriented interface is also available;

  my $d = Data::PowerSet->new( 3, 1, 4 );
  while (my $r = $d->next) {
    print "@$r\n";
  }
  # produces the same output as above

DESCRIPTION
"Data::PowerSet" takes a list and returns all possible combinations of the elements appearing in the list without replacement.

EXPORTABLE FUNCTIONS

powerset
    The "powerset" function takes an array (or a reference to an array) on input and returns a reference to an array of arrays containing all the possible unique combinations of elements.

    It is also possible to supply a reference to hash as the first parameter to tweak the behaviour. See the "new" method for a description of what keys can be specified.

      powerset( 2, 5, 10, 17 );

      powerset( {min => 1}, qw(a b c d) );

      powerset( [qw[ bodine mondaugen gadrulfi fleische eigenvalue ]] );

METHODS
The object-oriented interface provided by the module is implemented with the following methods.

new Creates a new "Data::PowerSet" object.

      my $ps = Data::PowerSet->new( qw( foo bar grault waldo ));

    A reference to a hash may be supplied, to change the way the object behaves.

    min Minimum number of elements present in the selection.

        Note that the empty set (no elements) is quite valid, according to the mathematical definition of a power set. If this is not what you expect, setting "min" to 1 will effectively cause the empty set to be excluded from the result.

          my $ps = Data::PowerSet->new( {min=>2}, 2, 3, 5, 8, 11 );

        In the above object, no returned list will contain fewer than 2 elements.

    max Maximum number of elements present in the selection.

          my $ps = Data::PowerSet->new( {max=>3}, 2, 3, 5, 8, 11 );

        In the above object, no returned list will contain more than 3 elements.

    join
        Perform a join() on each returned list using the specified value.

          my $ps = Data::Powerset->new( {join=>'-'}, 'a', 'b' );

        When this attribute is used, the next() method will return a scalar rather than a reference to an array.

next
    Returns a reference to an array containing the next combination of elements from the original list;

        my $ps = Data::PowerSet->new(qw(e t a i s o n));
        my $first = $ps->next;
        my $next  = $ps->next;

reset
    Restart from the first combination of the list.

data
    Accept a new list of elements from which to draw combinations.

      $ps->data( qw(all new elements to use) );

count
    Returns the number of elements in the set. This can be used to set "max" to the number of elements minus one, in order to exclude the set of all elements, when the number of elements is difficult to determine beforehand.

DIAGNOSTICS
None.

NOTES
Power sets grow exponentially. A power set of 10 elements returns a more than one thousand results. A power set of 20 elements contains more than one million results. The module is not expected to be put to use in larger sets.

A power set, by definition, includes the set of no elements and the set of all elements. If these results are not desired, the "min" and "max" methods or properties can be used to exclude them from the results.

This module works with perl version 5.005_04 and above.

SEE ALSO

List::PowerSet
    Another module that generates power sets. If I had managed to find it in a search beforehand, I probably would have used it instead. Nonetheless, "Data::PowerSet" has a couple of features not present in "List::PowerSet", but otherwise both can be used pretty much interchangeably.

Algorithm::Combinatorics
    A fast (no stacks, no recursion) method for generating permutations and combinations of a set. A power set is merely the union of all combinations (of differing lengths).

<http://en.wikipedia.org/wiki/Power_set>
    The wikipedia definition of a power set.

BUGS
None known. Please report all bugs at <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Data-PowerSet|rt.cpan.org>

Make sure you include the output from the following two commands:

  perl -MData::PowerSet -le 'print Data::PowerSet::VERSION'
  perl -V

ACKNOWLEDGEMENTS
This module is dedicated to Estelle Souche, who pointed out the very elegant and obvious algorithm. Smylers suggested the name.

AUTHOR
David Landgren, copyright (C) 2005-2008. All rights reserved.

http://www.landgren.net/perl/

If you (find a) use this module, I'd love to hear about it. If you want to be informed of updates, send me a note. You know my first name, you know my domain. Can you guess my e-mail address?

LICENSE
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.