My apologies, I probably should have included an example. The important thing to note is that since we're storing information on unordered pairs, so that (0,1) is the same as (1,0). Hence the requirement that X > Y to find the index allows us to cover all possible pairs:

for my $x (0..5) { for my $y (0..$x-1) { print "Info for pair ($x, $y) is stored at: ",$x*($x-1 +)/2+$y,"\n"; } }

Which provides the output:

Info for pair (1, 0) is stored at: 0
Info for pair (2, 0) is stored at: 1
Info for pair (2, 1) is stored at: 2
Info for pair (3, 0) is stored at: 3
Info for pair (3, 1) is stored at: 4
Info for pair (3, 2) is stored at: 5
Info for pair (4, 0) is stored at: 6
Info for pair (4, 1) is stored at: 7
Info for pair (4, 2) is stored at: 8
Info for pair (4, 3) is stored at: 9
Info for pair (5, 0) is stored at: 10
Info for pair (5, 1) is stored at: 11
Info for pair (5, 2) is stored at: 12
Info for pair (5, 3) is stored at: 13
Info for pair (5, 4) is stored at: 14

The important thing to note here is that as the range expands we're able to encode the additional pairs created by the addition of extra elements into our potential array.

As a simple example, I'm using this to store information about undirected graphs, where every vertex may contain a link to any other vertex, and the graph can grow over time. By using the algorithm described above we can encode the entire graph as a bitstring, with each position indicating the presence (true) or absence (false) of an edge between two nodes.

The whole thing works just fine, but I'm certain it's not an original algorithm, hence my seeking of wisdom before anything goes up on the CPAN.


In reply to Re^2: Extendable pairwise indexes - prior work? by pjf
in thread Extendable pairwise indexes - prior work? by pjf

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.