artist has asked for the wisdom of the Perl Monks concerning the following question:

I have few sets. Each sets has few elements, (in varying numbers). Whenever I introduce a new set, I like to establish a one to one relationship with all closely matching existing sets. "Close matches' could be defined in various ways.
Relationship should be relationship between sets. Some examples: A is subset of B, B is subset of A, A and B has difference of 3 elements, etc..

How do I go about this? Existing algorithm/modules would be useful. I am open to new ideas.

Thanks,
artist.

Replies are listed 'Best First'.
Re: Relationships of Matching Sets
by kvale (Monsignor) on Dec 17, 2004 at 00:03 UTC
    Assuming that these are true sets, i.e., no element is repeated within the same set, Set::Scalar has what you need (from the synopsis):
    use Set::Scalar; $s = Set::Scalar->new; $s->insert('a', 'b'); $s->delete('b'); $t = Set::Scalar->new('x', 'y', $z);
    Given the set objects, you can perfom various operation on the sets to derive relations:
    $u = $s->union($t); $i = $s->intersection($t); $d = $s->difference($t); $e = $s->symmetric_difference($t); $v = $s->unique($t); $c = $s->complement;
    difference can be used to test for subsets and symmetric difference can be used as a kind of distance metric.

    Update: fixed a typo.

    -Mark

      Great idea, this was the first module that came to my mind. Also, if you'd like to explore your ideas on "close matching," try subclassing Set::Scalar. It seems like it might be difficult to define a general solution for "almost matching" in terms of sets but you could certainly implement specific solutions easily by adding the methods you want to a subclass of Set::Scalar (and leave the union, symmetric_difference, etc. in the original class definition).

      Best of luck. :)

Re: Relationships of Matching Sets
by zejames (Hermit) on Dec 16, 2004 at 23:04 UTC

    As far as I understand what you need, your sets will be stored in arrays or hashes, and you'll have to manipulate those data structures to find relationship.

    You'll find a bunch of example in the famous Perl cookbook, in which much data manipulation (including arrays and hashes) is described in a very useful way

    Update : to complete this, have a look at the Set::Array and Set:Hash modules in CPAN.

    HTH


    --
    zejames
Re: Relationships of Matching Sets
by davido (Cardinal) on Dec 17, 2004 at 02:08 UTC

    No discussion of sets would be complete without someone mentioning Quantum::Superpositions, from Damian Conway (TheDamian...yes, really that damian).

    It's a sick and humbling module, yet one of the coolest things I've played with, and I couldn't begin to do justice to describing its features. Every time I wade through its intreguing documentation I find something new that I hadn't noticed before. You may find it useful to read the section on Boolean evaluation of superpositions, for example.

    Enjoy!


    Dave