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

Dear monks,
A holiday ponderance.

Given a hash consisting of key integers > value integers, remove the fewest number of key/value pairs such that all remaining pairs are "nested" (loosely used!! see examples). Integers may only appear once in the hash. Note that there are equally optimal solutions in some (many?) cases, and not all need be returned. A solution which loops through left to right and removes non-nested pairs as they are encountered (using some look-ahead to detect this) is simple, but AFAIKT not guaranteed to remove the fewest number of pairs? I may be wrong on this, in which case this question is pointless. Of course this is not Perl specific- but Perl is what I'm using, and perhaps there some nice hash-foo which will work!

Some trivial examples with letters, to show possible input patterns:

1. a b B c A C <- bad 2. a b B A c C <- ok 3. a b c C B A <- ok 4. a b A B c C <- bad 5. a b B c C A d e E D <- ok 6. a A b B c C <- ok
'solved'
1. a b B A (removed cC) or- b B c C (removed aA)
4. remove aA or bB

Some play data.

%k = ( 0 => 8, 3 => 5, 7 => 9, 10 => 100, 11 => 99, 15 => 25, 16 => 26, 17 => 27, 35 => 40, 36 => 39, 37 => 38, 60 => 80, 65 => 75, 66 => 76, 67 => 74);

Happy qw/Holidays Christmas Festivus Kwanzaa yet-another-day-of-meditation-with-no-otherwise-special-meaning/

P.S. Over-caffeinated monks- this is honestly not homework, or a final exam question, or even class related, so don't bother pointing out the fact its sounds like such. :) Thank you.

Replies are listed 'Best First'.
Re: (non) nested pairs
by ikegami (Patriarch) on Dec 25, 2005 at 01:31 UTC

    I'm sure I'm not the only one who doesn't see your definition of nested. You try to explain by example, but all I see is a flat list while you talk of hashes (which have keys and value groupings).

    Maybe you could demonstrate by giving us the algorithm you mentioned. Even though it doesn't give the optimal solution, it'll help us understand what you want.

      ikegami,

      Below is some "detector" code that works along the lines as I mentioned above. It determines whether or not I should remove a pair. Its crude because I've ripped it from another function I've written that does a little something different, and the data structures may look a little odd because of that. Hmm... now that I test this further I wonder if I actually had the answer all along, I'll try and come up with an example that doesn't work with this code and report back.

      sub pseudoKnotPairs { my $self = shift; my %p = @_; my @r; print "\n\n"; my $hel; my $h = 0; my @keys = sort {$a <=> $b} keys %p; map { $hel->{$h} = [$_, $p{$_}]; $h++} (@keys); # set some flags my $reset = 0; foreach my $k (sort {$a <=> $b} keys %{$hel}) { print $k, " ", @{$hel->{$k}}[0], " ", @{$hel->{$k}}[1], ": "; + my $nested = 1; my $cl = @{$hel->{$k}}[1]; for (my $c = $k+1; $c < $#keys + 1; $c++) { print " $cl"; if (@{$hel->{$c}}[1] > @{$hel->{$k}}[1]) { if (@{$hel->{$k}}[1] > @{$hel->{$c}}[0] ) { $nested =0 + } last; } if ( $cl < @{$hel->{$c}}[1]) { $nested = 0; } $cl = @{$hel->{$c}}[1]; } # some @r value set below to be returned if ($nested == 1) { print "\tok!"; } else { print "\tremove me!"; } print "\n"; } print "\n\n"; return 1; } &pseudoKnotPairs(qw/0 9 3 6 4 5 7 10 8 11/);
      cheers,
      M

        To further muddy the water. With other test data I find that the result with the above code is not quite as desired, as I initially expected (thus the plea for help). While it returns a legal result, its far from optimum. For instance:

        &pseudoKnotPairs(qw/0 100 1 99 10 88 11 99/);

        Will remove the first 3 pairs. All that would be necessary is to remove one of 10/88 or 11/99 and all three remain pairs would pass the mystical "nested" test.