Is there a function for mapping 1 to 1, 2 to 2...n to n, 1,2 to n+1, 1,3 to n+2...1,n-1 to 2n-1 for n variables?

464 Views Asked by At

So I'm programming something and I need to create this mapping. This is my rough idea of how it will work.

I have a list of variables, $x_1$ to $x_n$.

$x_1$ maps to $1$, $x_1$ maps to $2$, so on until $x_n$ which maps to $n$. This has $n$ terms.

For 2 variables together, I need to map $x_1x_2$ to $n+1$, all the way to $x_1x_n$ which will map to $2n-1$. This has $n-1$ terms. Note that I'm not considering $x_1x_1$.

As I go along to $x_2x_3$ to $x_2x_n$, I will be mapping it to the additional $n-2$ terms. For 2 variables, I should have a total of $\frac{(n)(n+1)}{2}-n$ terms mapped to.

For 3 variables and beyond, I know the number of terms that it needs to get mapped to. It is $n$ choose 3, and so on for 4 variables. But is there a function which perhaps takes in the indicies of $x$ and spits out where it should be along this long chain? I can't seem to find it.

Thanks.

1

There are 1 best solutions below

0
On BEST ANSWER

The following bijection takes a set of $k$ distinct indices $a_1<a_2<\dots<a_k$ and returns a number between $0$ and $\binom{n}k-1$, so that every set gets a different number.

$$ S=\{a_1<a_2<\dots<a_k\}\implies f(S)=\binom{n-a_1}k+\binom{n-a_2}{k-1}+\dots+\binom{n-a_k}1 $$ However, this is exactly the opposite of the order you want, so you should instead use $\binom{n}k-f(S)$.

Therefore, given an arbitrary set $S$ of $k$ indices, your desired indexing operation is $$ g(S) = \binom{n}1+\dots+\binom{n}{k-1}+\binom{n}k-f(S) $$ For example, when $n=10$ and your set of indices is $x_2x_5x_7$, the place in the list is $$ g(\{2,5,7\})=\binom{10}1+\binom{10}2+\binom{10}3-\binom{10-2}3-\binom{10-5}2-\binom{10-7}1= $$ As a sanity check, the place of $x_1x_n$ in the list is $\binom{n}1+\binom{n}2-\binom{n-1}2-\binom{n-n}1=n+(n-1)+0=2n-1$, which agrees with your example. The position of $x_1x_2$ is $\binom{n}1+\binom{n}2-\binom{n-1}2-\binom{n-2}1=n+1$.