2

Given a set of $n$ items (i.e., $\{X_1, X_2, X_3 ... X_n\}$), for all $\binom{n}{2}$ combinations, is there a function I can use to calculate unique labels in the range of $[1, \frac{n (n-1)}{2}]$, given indexes $i, j \in [1, n]$ and $i \neq j$?

E.g., If $n = 5$, I might have the following diagonal table of combinations:

        5   1
      4   D   2
    3   C   G   3
  2   B   F   I   4
    A   E   H   J
  • $A$ would be a real number between 1 and 10 that can be calculated by either $f(1, 2)$ or $f(2, 1)$.
  • $D$ would be a real number between 1 and 10 that can be calculated by either $f(1, 5)$ or $f(5, 1)$
  • $H$ would be a real number between 1 and 10 that can be calculated by either $f(3, 4)$ or $f(4, 3)$

...and each letter's value would not be repeated for another letter. E.g., if $D = 4$ then none of the other letters can be $4$.

If it helps to have some context, I'm writing some computer code and want to index into an array to store the results of the somewhat computationally-expensive combination procedure. The pairs will be received in random order and some may be repeated (in which case I want to lookup the previously computed value).

steamer25
  • 121
  • 3
  • I think if I have to sort the arguments (i and j), I may just use a ragged array. I'm still curious if there's a commutative/numerical solution but I guess it's not overly important. I'll try and add +1s to the answers that I can understand well enough to convince myself of their correctness in spite of the sorting. – steamer25 Aug 31 '13 at 16:22

3 Answers3

1

If I understand the question correctly, you are simply trying to store a lower-triangular matrix in contiguous storage. I.e., you have a collection of values $A_{i,j}$ where $1 \le i \le j$, and you want to map $(i,j)$ to a one-dimensional array index. If so, just map $(i,j)$ to $i + (j-1)j/2$. If the maximum value of $j$ is $N$, you will need an array of size $\frac{1}{2} N (N+1)$.

awkward
  • 14,736
  • +1 That works and is simpler than the formula I put in a comment on another answer. I'll add a comment to the question for more clarification though I'm not sure there's a solution to the unnecessary particulars of my problem. – steamer25 Aug 31 '13 at 16:20
  • 1
    Also, I should note that this seems to require zero-based indexes. – steamer25 Aug 31 '13 at 16:29
  • @streamer25, I don't think zero-based indexing is required, since (1,1) is mapped to 1. – awkward Aug 31 '13 at 19:35
1

The type of question has been asked, in various guises, for the $\binom nk$ combinations of $k$ among $n$ elements, for general values of $k$ instead of just $k=2$. See A positional number system for enumerating fixed-size subsets?, and Determining the position of a binary value with $k$ one bits and $n-k$ zeros in an enumeration of $C_k^n$ bit strings, and Generate all k-weight n-bit numbers in pseudo-random sequence.. In this generality, there is an elegant solution called the combinatorial number system.

For $k=2$ this comes down to locating the pair $(a,b)$ with $0\leq a<b<n$ at position $a+\binom b2<\binom n2$ (if you were indexing from$~1$ instead of from$~0$, make sure to correct for that). Should you need to find to pair mapping to a given index$~i$ find the maximal $b$ with $\binom b2\leq i$ (which can be done about as fast and easily as computing integer square roots) and put $a=i-\binom b2$.

  • I was curious about the general case as well. Thanks for the links--I'll take a look when I have more time. I don't know if I'll accept any of the answers based on ordering the arguments (see my comment above) but I intend to come back with at least a +1 :) . – steamer25 Aug 31 '13 at 16:32
  • Actually, having not yet read the links, the $a+\binom{b}{2}$ for $0 ≤ a < b < n$ is the same as awkward's answer which I've already plussed. +1 here too :) . – steamer25 Aug 31 '13 at 16:46
0

It is easier to sort the pairs you receive, then find the index from a sorted pair. If storage is not an issue take $(a,b)$ with $a \ge b$ to $a^2+b$ This wastes about half your indices but is easy. To go the other way, if $n=a^2+b, a=\lfloor \sqrt n\rfloor, b=n-a^2$ If a factor $2$ matters (it shouldn't-do you know the demand that well?) you can use a pairing function

Ross Millikan
  • 374,822
  • It does seem like I may need to sort the arguments. If that's the case, it might end up being: $an-\frac{a(a-1)}{2}+b-a$ where $a < b$. – steamer25 Aug 31 '13 at 05:41
  • That is: $(a-1)n-\frac{a(a-1)}{2}+b-a$ – steamer25 Aug 31 '13 at 05:56
  • Longer response: Yeah, I'm trying to avoid wasting half of the indexes and the [Cantor] pairing function linked seems to encode differing permutations of the same combination differently which also seems like the indexes would be non-contiguous if I pre-sort the combos. – steamer25 Aug 31 '13 at 16:26