ambrus has asked for the wisdom of the Perl Monks concerning the following question:
Here's a question about a non-perl homework.
I have a set of objects, say in the array @sg. These objects have a certain property which is typically different for most objects, but sometimes it's equal for two or more of them. I'd like to find and output these sets of objects (in any reasonable format).
We compute the property of each object like this: @sgprop = map { property($_) } @sg;. (This is the most time-consuming part of the task btw, but it's not our task here to improve it.) However, two equal properties can have different representations.
You can do two things with these properties. Firstly, you can create a fingerprint of the properties @sgpropfp = map { fingerprint($_) } @sg;. These fingerprints are plain array that you can easily compare lexicographically. Assume that the function for three-way comparing these arrays is available: fp_compare($sgpropfp[$i], $sgpropfp[$j]) returns -1, 0, +1 for less, equal, and greater resp. For equal properties, this fingerprint will always be the same, but it may accidentally match for different properties.
Secondly, there's an equivalence relation that compares two properties for equality value: prop_matching($sgprop[$i], $sgprop[$j]) returns true for equal properties and false for unequal, but can't tell the order of unequal properties.
So now I have to write code that outputs those sets of objects from @sg that have matching property value.
The code I wrote works like this: it sorts the stuff by fingerprints, then compares each two adjacent properties in this order, and if they are equal, the two objects are output. For example (this code is untested because the real code is not in perl):
@order = sort { fp_compare($sgpropfp[$a], $sgpropfp[$b]) } 0 .. @sg - +1; for (0 .. @sg - 2) { if (prop_matching($sgprop[$order[$_]], $sgprop[$order[$_ + 1]] +)) { print "property of $sg[$order[$_]] and $sg[$order[$_ + + 1]] is equal"; } }
This solution has two problems. Firstly, if the properties of three objects have equal fingerprints but the property of the second one really is different, then this program will not detect that the properties of the first and the third are equal. Secondly, it doesn't handle the case when the property of more than two objects are the same.
I'd like to fix these two problems. I think I started the right way, that is, I have to categorize the objects by their property fingerprints. However, I don't know what exactly to do after that.
I'll be glad for any help. Thanks.
Oh, and I need this by yesterday.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Equivalence classes from equivalence relation and fingerprints
by ambrus (Abbot) on Jan 17, 2007 at 21:19 UTC | |
|
Re: Equivalence classes from equivalence relation and fingerprints
by merlyn (Sage) on Jan 17, 2007 at 20:05 UTC | |
|
Re: Equivalence classes from equivalence relation and fingerprints
by kyle (Abbot) on Jan 17, 2007 at 20:29 UTC | |
by ambrus (Abbot) on Jan 19, 2007 at 14:15 UTC | |
by kyle (Abbot) on Jan 19, 2007 at 16:52 UTC | |
|
Re: Equivalence classes from equivalence relation and fingerprints
by OfficeLinebacker (Chaplain) on Jan 18, 2007 at 02:51 UTC | |
|
Re: Equivalence classes from equivalence relation and fingerprints
by Moron (Curate) on Jan 18, 2007 at 13:10 UTC |