in reply to Better algorithm or data structure?

I'm not sure whether this would work as I haven't thought how to get the remaining "sets" back after culling but here goes. How about using the garbage collection built into Perl by turning your @bools into %bools keyed by the range 1 .. $N and the values being anonymous arrays containing references to any "sets" that contain the number in the key. By delete'ing any key in %bools that is in @someOrder you are left with only references to "sets" that have at least one number reamaining. The "sets" are array references that are now out of scope so once all references are gone they are garbage collected without the need to do a splice. You should then be able to recover the remaining "sets" by combing through %bools for unique array refs.

This idea may be cock-eyed because I don't have time to fully test it now but your code took about 25 seconds on my elderly laptop whereas this code

use strict; use warnings; use List::Util qw{ shuffle }; use Time::HiRes qw{ time }; our $N //= 1e3; our $S //= 1e4; our $Z //= 5; my %bools = map { $_ => [] } 1 .. $N; for my $set ( 1 .. $S ) { my $conts = []; push @{ $conts }, int rand $N for 0 .. int rand $Z; push @{ $bools{ $_ } }, $conts for @{ $conts }; } my @someOrder = shuffle 1 .. $N; my $start = time(); delete @bools{ @someOrder }; printf "Took %.6f\n", time() - $start;

takes about 15ms, although more time would be taken recovering the sets.

I hope I'm not just having a rush of blood and that this is actually helpful.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: Better algorithm or data structure?
by BrowserUk (Patriarch) on Aug 30, 2010 at 00:37 UTC

    I like your lateral thinking.

    Unfortunately, I need to find all (any) sets completed by the state change of each boolean, at the time I receive that boolean. @someOrder was intended to represent the program receiving the notification of the booleans changing state over time, in some unspecifiable order.

    That said, I think combining @sets into a hash keyed by the booleans is a very smart move. I think. I'm having trouble wrapping my head around how the sets, which will be referenced from multiple kash keys will get removed, but maybe tomorrow...


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      How about making each "set" an object rather than just an anonymous array and store references to the objects in the %bools hash. You could then iterate over @someOrder in a loop rather than hash slicing and use the object destructor to take some action when the final reference to a "set" is deleteed from %bools.

      Just a thought that occurred to me while trying to get to sleep :-/

      Cheers,

      JohnGG