Pairing function

In mathematics, a pairing function is a process to uniquely encode two natural numbers into a single natural number.

Any pairing function can be used in set theory to prove that integers and rational numbers have the same cardinality as natural numbers.

Definition
A pairing function is a bijection
 * $$\pi:\mathbb{N} \times \mathbb{N} \to \mathbb{N}.$$

More generally, a pairing function on a set A is a function that maps each pair of elements from A into an element of A, such that any two pairs of elements of A are associated with different elements of A, or a bijection from $$A^2$$ to A.

Hopcroft and Ullman pairing function
Hopcroft and Ullman (1979) define the following pairing function: $$\langle i, j\rangle := \frac{1}{2}(i+j-2)(i+j-1) + i$$, where $$i, j\in\{1, 2, 3, \dots \}$$. This is the same as the Cantor pairing function below, shifted to exclude 0 (i.e., $$i=k_2+1$$, $$j=k_1+1$$, and $$\langle i, j\rangle - 1 = \pi(k_2,k_1)$$).

Cantor pairing function
The Cantor pairing function is a primitive recursive pairing function
 * $$\pi:\mathbb{N} \times \mathbb{N} \to \mathbb{N}$$

defined by
 * $$\pi(k_1,k_2) := \frac{1}{2}(k_1 + k_2)(k_1 + k_2 + 1)+k_2$$

where $$k_1, k_2\in\{0, 1, 2, 3, \dots\}$$.

It can also be expressed as $$\pi(x, y) := \frac{x^2 + x + 2xy + 3y + y^2}{2}$$.

It is also strictly monotonic w.r.t. each argument, that is, for all $$k_1, k_1', k_2, k_2' \in \mathbb{N}$$, if $$k_1 < k_{1}'$$, then $$\pi(k_1, k_2) < \pi(k_1', k_2)$$; similarly, if $$k_2 < k_{2}'$$, then $$\pi(k_1, k_2) < \pi(k_1, k_2')$$.

The statement that this is the only quadratic pairing function is known as the Fueter–Pólya theorem. Whether this is the only polynomial pairing function is still an open question. When we apply the pairing function to $k_{1}$ and $k_{2}$ we often denote the resulting number as $⟨k_{1}, k_{2}⟩$.

This definition can be inductively generalized to the
 * $$\pi^{(n)}:\mathbb{N}^n \to \mathbb{N}$$

for $$n > 2$$ as
 * $$\pi^{(n)}(k_1, \ldots, k_{n-1}, k_n) := \pi ( \pi^{(n-1)}(k_1, \ldots, k_{n-1}), k_n)$$

with the base case defined above for a pair: $$\pi^{(2)}(k_1,k_2) := \pi(k_1,k_2).$$

Inverting the Cantor pairing function
Let $$z \in \mathbb{N}$$ be an arbitrary natural number. We will show that there exist unique values $$x, y \in \mathbb{N}$$ such that


 * $$ z = \pi(x, y) = \frac{(x + y + 1)(x + y)}{2} + y $$

and hence that the function $π(x, y)$ is invertible. It is helpful to define some intermediate values in the calculation:
 * $$ w = x + y \!$$
 * $$ t = \frac{1}{2}w(w + 1) = \frac{w^2 + w}{2} $$
 * $$ z = t + y \!$$

where $t$ is the triangle number of $w$. If we solve the quadratic equation
 * $$ w^2 + w - 2t = 0 \!$$

for $w$ as a function of $t$, we get
 * $$ w = \frac{\sqrt{8t + 1} - 1}{2} $$

which is a strictly increasing and continuous function when $t$ is non-negative real. Since
 * $$ t \leq z = t + y < t + (w + 1) = \frac{(w + 1)^2 + (w + 1)}{2} $$

we get that
 * $$ w \leq \frac{\sqrt{8z + 1} - 1}{2} < w + 1 $$

and thus
 * $$ w = \left\lfloor \frac{\sqrt{8z + 1} - 1}{2} \right\rfloor. $$

where $⌊ ⌋$ is the floor function. So to calculate $x$ and $y$ from $z$, we do:
 * $$ w = \left\lfloor \frac{\sqrt{8z + 1} - 1}{2} \right\rfloor $$
 * $$ t = \frac{w^2 + w}{2} $$
 * $$ y = z - t \!$$
 * $$ x = w - y. \!$$

Since the Cantor pairing function is invertible, it must be one-to-one and onto.

Examples
To calculate $π(47, 32)$:

so $47 + 32 = 79$.

To find $79 + 1 = 80$ and $79 × 80 = 6320$ such that $6320 ÷ 2 = 3160$:

so $3160 + 32 = 3192$;

so $π(47, 32) = 3192$;

so $x$;

so $y$; thus $π(x, y) = 1432$.

Derivation


The graphical shape of Cantor's pairing function, a diagonal progression, is a standard trick in working with infinite sequences and countability. The algebraic rules of this diagonal-shaped function can verify its validity for a range of polynomials, of which a quadratic will turn out to be the simplest, using the method of induction. Indeed, this same technique can also be followed to try and derive any number of other functions for any variety of schemes for enumerating the plane.

A pairing function can usually be defined inductively – that is, given the $8 × 1432 = 11456$th pair, what is the $11456 + 1 = 11457$th pair? The way Cantor's function progresses diagonally across the plane can be expressed as
 * $$\pi(x,y)+1 = \pi(x-1,y+1)$$.

The function must also define what to do when it hits the boundaries of the 1st quadrant – Cantor's pairing function resets back to the x-axis to resume its diagonal progression one step further out, or algebraically:
 * $$\pi(0,k)+1 = \pi(k+1,0)$$.

Also we need to define the starting point, what will be the initial step in our induction method: $\sqrt{11457} = 107.037$.

Assume that there is a quadratic 2-dimensional polynomial that can fit these conditions (if there were not, one could just repeat by trying a higher-degree polynomial). The general form is then
 * $$\pi(x,y) = ax^2+by^2+cxy+dx+ey+f$$.

Plug in our initial and boundary conditions to get $107.037 − 1 = 106.037$ and:
 * $$bk^2+ek+1 = a(k+1)^2+d(k+1)$$,

so we can match our $106.037 ÷ 2 = 53.019$ terms to get

So every parameter can be written in terms of $⌊53.019⌋ = 53$ except for $w = 53$, and we have a final equation, our diagonal step, that will relate them:
 * $$\begin{align}

\pi(x,y)+1 &= a(x^2+y^2) + cxy + (1-a)x + (1+a)y + 1 \\ &= a((x-1)^2+(y+1)^2) + c(x-1)(y+1) + (1-a)(x-1) + (1+a)(y+1). \end{align}$$

Expand and match terms again to get fixed values for $53 + 1 = 54$ and $53 × 54 = 2862$, and thus all parameters:

Therefore
 * $$\begin{align}

\pi(x,y) &= \frac{1}{2}(x^2+y^2) + xy + \frac{1}{2}x + \frac{3}{2}y \\ &= \frac{1}{2}(x+y)(x+y+1) + y, \end{align}$$

is the Cantor pairing function, and we also demonstrated through the derivation that this satisfies all the conditions of induction.

Other pairing functions
The function $$P_2(x, y):= 2^x(2y + 1) - 1$$ is a pairing function.

In 1990, Regan proposed the first known pairing function that is computable in linear time and with constant space (as the previously known examples can only be computed in linear time iff multiplication can be too, which is doubtful). In fact, both this pairing function and its inverse can be computed with finite-state transducers that run in real time. In the same paper, the author proposed two more monotone pairing functions that can be computed online in linear time and with logarithmic space; the first can also be computed offline with zero space.

In 2001, Pigeon proposed a pairing function based on bit-interleaving, defined recursively as:


 * $$\langle i,j\rangle_{P}=\begin{cases}

T & \text{if}\ i=j=0;\\ \langle\lfloor i/2\rfloor,\lfloor j/2\rfloor\rangle_{P}:i_0:j_0&\text{otherwise,} \end{cases}$$

where $$i_0$$ and $$j_0$$ are the least significant bits of i and j respectively.

In 2006, Szudzik proposed a "more elegant" pairing function defined by the expression:
 * $$\operatorname{ElegantPair}[x, y] := \begin{cases}

y^2 + x&\text{if}\ x < y,\\ x^2 + x + y&\text{if}\ x \ge y.\\ \end{cases}$$ Which can be unpaired using the expression:
 * $$\operatorname{ElegantUnpair}[z] := \begin{cases}

\left\{ z - \lfloor\sqrt{z}\rfloor^2, \lfloor\sqrt{z}\rfloor \right\} & \text{if }z - \lfloor\sqrt{z}\rfloor^2 < \lfloor\sqrt{z}\rfloor, \\ \left\{ \lfloor\sqrt{z}\rfloor, z - \lfloor\sqrt{z}\rfloor^2 - \lfloor\sqrt{z}\rfloor \right\} & \text{if }z - \lfloor\sqrt{z}\rfloor^2\geq\lfloor\sqrt{z}\rfloor. \end{cases}$$ (Qualitatively, it assigns consecutive numbers to pairs along the edges of squares.) This pairing function orders SK combinator calculus expressions by depth. This method is the mere application to $$\N$$ of the idea, found in most textbooks on Set Theory, used to establish $$\kappa^2=\kappa$$ for any infinite cardinal $$\kappa$$ in ZFC. Define on $$\kappa\times\kappa$$ the binary relation
 * $$(\alpha,\beta)\preccurlyeq(\gamma,\delta) \text{ if either } \begin{cases}

(\alpha,\beta) = (\gamma,\delta),\\[4pt] \max(\alpha,\beta) < \max(\gamma,\delta),\\[4pt] \max(\alpha,\beta) = \max(\gamma,\delta)\ \text{and}\ \alpha<\gamma,\text{ or}\\[4pt] \max(\alpha,\beta) = \max(\gamma,\delta)\ \text{and}\ \alpha=\gamma\ \text{and}\ \beta<\delta. \end{cases}$$ $$\preccurlyeq$$ is then shown to be a well-ordering such that every element has $${}<\kappa$$ predecessors, which implies that $$\kappa^2=\kappa$$. It follows that $$(\N\times\N,\preccurlyeq)$$ is isomorphic to $$(\N,\leqslant)$$ and the pairing function above is nothing more than the enumeration of integer couples in increasing order. (See also Talk:Tarski's theorem about choice.)