in reply to Best Pairs
The following is relatively simple and efficient. The result is the top K pairs, with preference given to higher numbers in the event of a tie. Integer data from 1 to N allow us to use an array in place of a hash (the indices of the array form the required "set"):
# preconditions: my $elem = 1; my $k = 3; my $n_arrays = [map [split], <DATA>]; # algorithm: my @pairs = (0) x (@$n_arrays + 1); for (@$n_arrays) { next unless grep $_==$elem, @$_[0..$elem]; $_ != $elem and $pairs[$_]++ for @$_; } my @top_k = (sort{$pairs[$a]<=>$pairs[$b]} 1..$#pairs)[reverse -$k..-1 +]; print "@top_k\n"; __END__ 2 4 5 7 8 10 1 2 5 6 7 9 2 6 7 8 9 10 1 3 5 10 1 3 4 5 6 8 9 1 2 4 6 1 2 4 5 7 10 1 3 4 6 7 8 9
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Best Pairs
by Roger (Parson) on Nov 08, 2003 at 02:11 UTC | |
by Anonymous Monk on Nov 08, 2003 at 02:19 UTC |