One 'efficient' algorithm I can think of -

Presteps:
Step 1 - build a hash that holds the combination pairs of 1..N (math notation N C 2) as search keys, values initialized to 0.
Step 2 - while iterating through the input lines/arrays, for each combination of the elements on the current line, increment value of $hash{$comb_idx} by 1.

The aim of the prestep is to reduce the data set you have to work with (N=20, number of pair-combinations is just 190). The result hash will contain a count of the combinations in the data set. The hash key can be, simply, $n1 . '-' . $n2. Eg., '1-6', '3-18', etc.

Poststeps:
Step 1. For the given element, build a new hash by grepping for the given element in the keys of the original hash, and assigning the key=>count in the hash.
Step 2. Sort this hash to find the hash key for the top N counts, and decode the hash keys to get the other numbers in the pair.

The beauty of this solution is that once the hash of pair-combination-count is built (in the initialization phase), looking for 'maximum pairs' would be lightening fast.

I can implement the above algorithm in Perl later, but I think it should be piece of cake for you, besides, you are only looking for an algorithm afterall.

:-)


In reply to Re: Best Pairs by Roger
in thread Best Pairs by artist

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.