Ok so here is a combinatorial problem that I thought of.

Suppose N is in $\mathbb N$ such that $N>1$, then there is a way to count (set an index) to all pairs $(i,j) \in \{1,\dots,\mathbb N\}\times\{1,\dots,\mathbb N\}$ such that $i<j$. This problem is the same as the problem of setting a unique index to the edges of a complete undirected graph with $N$ vertices. In total there are $N(N-1)/2$ such pairs and the counting function can be given by $$f:S\rightarrow \{1,\dots,N(N-1)/2\} \qquad f((i,j)):= \sum_{k=2}^i (n-k+1) + (j-i)$$ where $S := \{(i,j) : 1\leq i,j\leq N$ and $i<j\}$ and those under the summation notation is ignored if $i=1$. This function is a bijection and you do get for a pair $(i,j)$ a unique number $f(i,j)$ less or equal to $N(N-1)/2$. The question is how to find the inverse of this function, i.e. if $x$ is a number in the image of $f$ then what is the $(i,j)$ associated to $x$. In other words, given the index of the edge can I easily find the pairs that present the vertices of the complete graph. You will end up with a quadratic equation with respect to $i$ but the condition $i<j$ will also give us a $j$. But what is the formula for the inverse of $f$?


First of all, for this kind of monotonic functions you can usually effectively use binary search, which would calculate the solution in logarithmic time with respect to the space size (assuming you can calculate where "half of the subspace" roughly is). As you can calculate your sum (after some simplifications) in constant time (or $O(\log N)$ for arbitrarily big numbers), that algorithm would work in $O(\log N)$ time (or respectively in $O(\log^2 N)$).

Second, you can derive a more direct formula. More precisely, observe that (using a formula for the area of a trapezoid):

$$f\big((i,j)\big) = \frac{1}{2}(N-1 + N-i+1)\cdot(i-1) + (j-i)$$

Here we will use the Newton's method to calculate $i$ out of $v$ using $g_v(x) = f(x,x)-v$:

$$x_{n+1} = x_n - \frac{g(x_n)}{g'(x_n)} = x_n - \frac{\frac{1}{2}(2N-x_n)\cdot(x_n-1)-v}{N-x_n+\frac{1}{2}}$$ After we recover the solution $x = g_{v-1}^{-1}(0)$ with a reasonable accuracy, you can calculate $j$ by using $j = v-f(\lfloor x\rfloor,\lfloor x\rfloor)$. The $-1$ in $v-1$ in $g_{v-1}$ was added because $j > i$. I didn't do any convergence analysis, but my guess is only a few steps are enough for any reasonable $N$ (for square roots 6 are enough for $N<2^{64}$, for arbitrarily big numbers $O(\log N)$).

Third, you can find a bijection which is more inverse-friendly, for example see my answer here.

I hope this helps $\ddot\smile$

  • $\begingroup$ Thank you. I liked your argument. I actually wanted an algebraic answer because f(i,j) itself is an algebraic function (quadratic in terms of i). So an algebraic equation for i and j in terms of f(i,j) should be possible. The second possibility you showed allows for this. But for that you don't need to use any approximation method like Newton's method. An immediate answer using quadratic equation is possible. I will post the answer shortly inspired from your idea. $\endgroup$ – quantum Jul 27 '16 at 7:21
  • $\begingroup$ Btw. I should also admit that I have cheated a little bit. I will still use the floor symbol. But the answer will be a one-liner formula (algebraic in terms of f(i,j) and the floor of a quadratic equation). $\endgroup$ – quantum Jul 27 '16 at 7:27
  • $\begingroup$ Perhaps you mean something like $i = \left\lfloor\frac{1}{2}\Big(1+2N\pm\sqrt{1-4 N+4 N^2-8(v-1)}\Big)\right\rfloor$? I thought you asked about something else, because such a transformation seemed too easy (it's just a normal quadratic equation). $\endgroup$ – dtldarek Jul 27 '16 at 7:29
  • $\begingroup$ Yes that's what I meant. And then just as you wrote substitute $i$ in the equation for $f$ and solve for $j$. $\endgroup$ – quantum Jul 27 '16 at 12:16
  • $\begingroup$ I'm gonna mark this as answered. Since you also put in the equation for $i$ in the comment, I don't think I need to write this extra detail in a new post here (If you want, you could include the quadratic formula in the original answer as an edit as well). Thanks for the help btw :) $\endgroup$ – quantum Jul 27 '16 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.