Pairing function

From HandWiki

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

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

Definition

A pairing function is a bijection[verification needed]

[math]\displaystyle{ \pi:\mathbb{N} \times \mathbb{N} \to \mathbb{N}. }[/math][1]

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,[2] or a bijection from [math]\displaystyle{ A^2 }[/math] to A.[3]

Hopcroft and Ullman pairing function

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

Cantor pairing function

A plot of the Cantor pairing function
The Cantor pairing function assigns one natural number to each pair of natural numbers
A graph of the Cantor pairing function
Graph of the Cantor pairing function

The Cantor pairing function is a primitive recursive pairing function

[math]\displaystyle{ \pi:\mathbb{N} \times \mathbb{N} \to \mathbb{N} }[/math]

defined by

[math]\displaystyle{ \pi(k_1,k_2) := \frac{1}{2}(k_1 + k_2)(k_1 + k_2 + 1)+k_2 }[/math][1][verification needed]

where [math]\displaystyle{ k_1, k_2\in\{0, 1, 2, 3, \dots\} }[/math].[1]

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

It is also strictly monotonic w.r.t. each argument, that is, for all [math]\displaystyle{ k_1, k_1', k_2, k_2' \in \mathbb{N} }[/math], if [math]\displaystyle{ k_1 \lt k_{1}' }[/math], then [math]\displaystyle{ \pi(k_1, k_2) \lt \pi(k_1', k_2) }[/math]; similarly, if [math]\displaystyle{ k_2 \lt k_{2}' }[/math], then [math]\displaystyle{ \pi(k_1, k_2) \lt \pi(k_1, k_2') }[/math].[citation needed]

The statement that this is the only quadratic pairing function is known as the Fueter–Pólya theorem.[1][verification needed] Whether this is the only polynomial pairing function is still an open question. When we apply the pairing function to k1 and k2 we often denote the resulting number as k1, k2.[citation needed]

This definition can be inductively generalized to the Cantor tuple function[citation needed]

[math]\displaystyle{ \pi^{(n)}:\mathbb{N}^n \to \mathbb{N} }[/math]

for [math]\displaystyle{ n \gt 2 }[/math] as

[math]\displaystyle{ \pi^{(n)}(k_1, \ldots, k_{n-1}, k_n) := \pi ( \pi^{(n-1)}(k_1, \ldots, k_{n-1}) , k_n) }[/math]

with the base case defined above for a pair: [math]\displaystyle{ \pi^{(2)}(k_1,k_2) := \pi(k_1,k_2). }[/math][1]

Inverting the Cantor pairing function

Let [math]\displaystyle{ z \in \mathbb{N} }[/math] be an arbitrary natural number. We will show that there exist unique values [math]\displaystyle{ x, y \in \mathbb{N} }[/math] such that

[math]\displaystyle{ z = \pi(x, y) = \frac{(x + y + 1)(x + y)}{2} + y }[/math]

and hence that the function π(x, y) is invertible. It is helpful to define some intermediate values in the calculation:

[math]\displaystyle{ w = x + y \! }[/math]
[math]\displaystyle{ t = \frac{1}{2}w(w + 1) = \frac{w^2 + w}{2} }[/math]
[math]\displaystyle{ z = t + y \! }[/math]

where t is the triangle number of w. If we solve the quadratic equation

[math]\displaystyle{ w^2 + w - 2t = 0 \! }[/math]

for w as a function of t, we get

[math]\displaystyle{ w = \frac{\sqrt{8t + 1} - 1}{2} }[/math]

which is a strictly increasing and continuous function when t is non-negative real. Since

[math]\displaystyle{ t \leq z = t + y \lt t + (w + 1) = \frac{(w + 1)^2 + (w + 1)}{2} }[/math]

we get that

[math]\displaystyle{ w \leq \frac{\sqrt{8z + 1} - 1}{2} \lt w + 1 }[/math]

and thus

[math]\displaystyle{ w = \left\lfloor \frac{\sqrt{8z + 1} - 1}{2} \right\rfloor. }[/math]

where ⌊ ⌋ is the floor function. So to calculate x and y from z, we do:

[math]\displaystyle{ w = \left\lfloor \frac{\sqrt{8z + 1} - 1}{2} \right\rfloor }[/math]
[math]\displaystyle{ t = \frac{w^2 + w}{2} }[/math]
[math]\displaystyle{ y = z - t \! }[/math]
[math]\displaystyle{ x = w - y. \! }[/math]

Since the Cantor pairing function is invertible, it must be one-to-one and onto.[2][additional citation(s) needed]

Examples

To calculate π(47, 32):

47 + 32 = 79,
79 + 1 = 80,
79 × 80 = 6320,
6320 ÷ 2 = 3160,
3160 + 32 = 3192,

so π(47, 32) = 3192.

To find x and y such that π(x, y) = 1432:

8 × 1432 = 11456,
11456 + 1 = 11457,
11457 = 107.037,
107.037 − 1 = 106.037,
106.037 ÷ 2 = 53.019,
⌊53.019⌋ = 53,

so w = 53;

53 + 1 = 54,
53 × 54 = 2862,
2862 ÷ 2 = 1431,

so t = 1431;

1432 − 1431 = 1,

so y = 1;

53 − 1 = 52,

Derivation

A diagonally incrementing "snaking" function, from same principles as Cantor's pairing function, is often used to demonstrate the countability of the rational numbers.

The graphical shape of Cantor's pairing function, a diagonal progression, is a standard trick in working with infinite sequences and countability.[lower-alpha 1] 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 nth pair, what is the (n+1)th pair? The way Cantor's function progresses diagonally across the plane can be expressed as

[math]\displaystyle{ \pi(x,y)+1 = \pi(x-1,y+1) }[/math].

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:

[math]\displaystyle{ \pi(0,k)+1 = \pi(k+1,0) }[/math].

Also we need to define the starting point, what will be the initial step in our induction method: π(0, 0) = 0.

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

[math]\displaystyle{ \pi(x,y) = ax^2+by^2+cxy+dx+ey+f }[/math].

Plug in our initial and boundary conditions to get f = 0 and:

[math]\displaystyle{ bk^2+ek+1 = a(k+1)^2+d(k+1) }[/math],

so we can match our k terms to get

b = a
d = 1-a
e = 1+a.

So every parameter can be written in terms of a except for c, and we have a final equation, our diagonal step, that will relate them:

[math]\displaystyle{ \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} }[/math]

Expand and match terms again to get fixed values for a and c, and thus all parameters:

a = 1/2 = b = d
c = 1
e = 3/2
f = 0.

Therefore

[math]\displaystyle{ \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} }[/math]

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

Other pairing functions

The function [math]\displaystyle{ P_2(x, y):= 2^x(2y + 1) - 1 }[/math] 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).[4] In fact, both this pairing function and its inverse can be computed with finite-state transducers that run in real time.[4][clarification needed] 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.[4][clarification needed]

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

[math]\displaystyle{ \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} }[/math]

where [math]\displaystyle{ i_0 }[/math] and [math]\displaystyle{ j_0 }[/math] are the least significant bits of i and j respectively.[1][verification needed]

In 2006, Szudzik proposed a "more elegant" pairing function defined by the expression:

[math]\displaystyle{ \operatorname{ElegantPair}[x, y] := \begin{cases} y^2 + x&\text{if}\ x \lt y,\\ x^2 + x + y&\text{if}\ x \ge y.\\ \end{cases} }[/math]

Which can be unpaired using the expression:

[math]\displaystyle{ \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 \lt \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} }[/math]

(Qualitatively, it assigns consecutive numbers to pairs along the edges of squares.) This pairing function orders SK combinator calculus expressions by depth.[2][clarification needed] This method is the mere application to [math]\displaystyle{ \N }[/math] of the idea, found in most textbooks on Set Theory,[5] used to establish [math]\displaystyle{ \kappa^2=\kappa }[/math] for any infinite cardinal [math]\displaystyle{ \kappa }[/math] in ZFC. Define on [math]\displaystyle{ \kappa\times\kappa }[/math] the binary relation

[math]\displaystyle{ (\alpha,\beta)\preccurlyeq(\gamma,\delta) \text{ if either } \begin{cases} (\alpha,\beta) = (\gamma,\delta),\\[4pt] \max(\alpha,\beta) \lt \max(\gamma,\delta),\\[4pt] \max(\alpha,\beta) = \max(\gamma,\delta)\ \text{and}\ \alpha\lt \gamma,\text{ or}\\[4pt] \max(\alpha,\beta) = \max(\gamma,\delta)\ \text{and}\ \alpha=\gamma\ \text{and}\ \beta\lt \delta. \end{cases} }[/math]

[math]\displaystyle{ \preccurlyeq }[/math] is then shown to be a well-ordering such that every element has [math]\displaystyle{ {}\lt \kappa }[/math] predecessors, which implies that [math]\displaystyle{ \kappa^2=\kappa }[/math]. It follows that [math]\displaystyle{ (\N\times\N,\preccurlyeq) }[/math] is isomorphic to [math]\displaystyle{ (\N,\leqslant) }[/math] 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#Proof of the converse.)

Notes

  1. The term "diagonal argument" is sometimes used to refer to this type of enumeration, but it is not directly related to Cantor's diagonal argument.[citation needed]

References

  1. 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 Steven Pigeon. "Pairing function". http://mathworld.wolfram.com/PairingFunction.html. 
  2. 2.0 2.1 2.2 2.3 Szudzik, Matthew (2006). "An Elegant Pairing Function". http://szudzik.com/ElegantPairing.pdf. 
  3. Szudzik, Matthew P. (2017-06-01). "The Rosenberg-Strong Pairing Function". arXiv:1706.04129 [cs.DM].
  4. 4.0 4.1 4.2 Regan, Kenneth W. (1992-12-01). "Minimum-complexity pairing functions" (in en). Journal of Computer and System Sciences 45 (3): 285–295. doi:10.1016/0022-0000(92)90027-G. ISSN 0022-0000. 
  5. See for instance Thomas, Jech (2006). Set theory: the third millennium edition. Springer Monographs in Mathematics. Springer-Verlag. p. 30. doi:10.1007/3-540-44761-X. ISBN 3-540-44085-2.