I think I can use a bijection between $\mathbb{Z}$ and $\mathbb{N}$ to transform $\mathbb{Z}\times\mathbb{Z}$ into $\mathbb{N}\times\mathbb{N}$ and then use the Cantor pairing function, but is there an explicit bijection with fewer computational steps?
-
3You could walk around in a square "spiral" on the $\mathbb{Z} \times \mathbb{Z}$ grid, however this really is only easier to visualize. – ZirconCode Jan 01 '17 at 21:16
-
Related: Construct a bijection between $\mathbb{Z}^+\times \mathbb{Z}^+$ and $\mathbb{Z}^+$ – Winther Jan 01 '17 at 21:20
-
By the way, am I correct that the simplest function from integers to naturals is $|x|\times2+(sign(x)-|sign(x)|)\div2$? – Alexey Jan 01 '17 at 21:37
-
Your question is not very clear. What do you mean by "explicit" and "computational steps"? Are you interested in computational complexity in the technical sense or just in having a simple formula? – Rob Arthan Jan 01 '17 at 23:37
2 Answers
The function $f=\left( -1\right)^n\lfloor \frac{n}{2}\rfloor$ bijects $\mathbb{N}$ with $\mathbb{Z}$ if we define $0\notin\mathbb{N}$. (Whatever bijects with this definition of $\mathbb{N}$ can trivially be modified otherwise with the shift $n\mapsto n-1$.) The function $g\left(2^k r\right)=\left(k+1, \frac{r+1}{2}\right)$ with $r$ odd bijects $\mathbb{N}$ with $\mathbb{N}^2$. Thus the function $h\left(2^k r\right)=\left(f\left( k+1\right), f\left(\frac{r+1}{2}\right)\right)$ bijects $\mathbb{N}$ with $\mathbb{Z}^2$.

- 115,835
Originating at $(0,0) \in \mathbb{Z} \times \mathbb{Z}$, imagine walking a square spiral where each step would add one to our counter and biject to the appropriate number in $\mathbb{N}$. For example, first you would walk up 1 step, left 1 step, down 2, right 2, up 3, and so on...
It is easy to see that this is a bijection and would cover the entire space of $\mathbb{Z} \times \mathbb{Z}$. It is perhaps very easy to visualize, however writing the bijection explicitly may be tougher.

- 904
-
1
-
In that case I believe that there is no easier function to write explicitly than with cantor pairing. – ZirconCode Jan 01 '17 at 21:36