Pairing function
Template:Short description Template:More citations needed
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.<ref>Template:Harvnb:
"Pairing functions arise naturally in the demonstration that the cardinalities of the rationals <math>\mathbb{Q}</math> and the nonnegative integers <math>\mathbb{Z}_{\geq 0}</math> are the same, i.e., <math> | \mathbb{Q} | = | \mathbb{Z}_{\geq 0} | = \aleph_0 </math>, originally due to Cantor."
</ref>
DefinitionEdit
A pairing function is a bijection
- <math>\pi:\mathbb{N} \times \mathbb{N} \to \mathbb{N}.</math>Template:SfnTemplate:SfnTemplate:Sfn
GeneralizationEdit
More generally, a pairing function on a set <math>A</math> is a function that maps each pair of elements from <math>A</math> into an element of <math>A</math>, such that any two pairs of elements of <math>A</math> are associated with different elements of <math>A</math>,Template:SfnTemplate:Efn or a bijection from <math>A^2</math> to <math>A</math>.Template:Sfn
Instead of abstracting from the domain, the arity of the pairing function can also be generalized: there exists an n-ary generalized Cantor pairing function on <math>\mathbb{N}</math>.Template:Sfn
Cantor pairing functionEdit
The Cantor pairing function is a primitive recursive pairing function
- <math>\pi:\mathbb{N} \times \mathbb{N} \to \mathbb{N}</math>
defined by
- <math>\pi(k_1,k_2) := \frac{1}{2}(k_1 + k_2)(k_1 + k_2 + 1)+k_2</math>
where <math>k_1, k_2\in\{0, 1, 2, 3, \dots\}</math>.Template:SfnTemplate:Bsn
It can also be expressed as <math>\pi(x, y) := \frac{x^2 + x + 2xy + 3y + y^2}{2}</math>.Template:Sfn
It is also strictly monotonic w.r.t. each argument, that is, for all <math>k_1, k_1', k_2, k_2' \in \mathbb{N}</math>, if <math>k_1 < k_{1}'</math>, then <math>\pi(k_1, k_2) < \pi(k_1', k_2)</math>; similarly, if <math>k_2 < k_{2}'</math>, then <math>\pi(k_1, k_2) < \pi(k_1, k_2')</math>.Template:Citation needed
The statement that this is the only quadratic pairing function is known as the Fueter–Pólya theorem.<ref>Template:Harvtxt cited in Template:Harvtxt.</ref> Whether this is the only polynomial pairing function is still an open question. When we apply the pairing function to Template:Math and Template:Math we often denote the resulting number as Template:Math.Template:Citation needed
This definition can be inductively generalized to the Template:Citation needed span
- <math>\pi^{(n)}:\mathbb{N}^n \to \mathbb{N}</math>
for <math>n > 2</math> as
- <math>\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>\pi^{(2)}(k_1,k_2) := \pi(k_1,k_2).</math>Template:Sfn
Inverting the Cantor pairing functionEdit
Let <math>z \in \mathbb{N}</math> be an arbitrary natural number. We will show that there exist unique values <math>x, y \in \mathbb{N}</math> such that
- <math> z = \pi(x, y) = \frac{(x + y + 1)(x + y)}{2} + y </math>
and hence that the function Template:Math is invertible. It is helpful to define some intermediate values in the calculation:
- <math> w = x + y \!</math>
- <math> t = \frac{1}{2}w(w + 1) = \frac{w^2 + w}{2} </math>
- <math> z = t + y \!</math>
where Template:Math is the triangle number of Template:Math. If we solve the quadratic equation
- <math> w^2 + w - 2t = 0 \!</math>
for Template:Math as a function of Template:Math, we get
- <math> w = \frac{\sqrt{8t + 1} - 1}{2} </math>
which is a strictly increasing and continuous function when Template:Math is non-negative real. Since
- <math> t \leq z = t + y < t + (w + 1) = \frac{(w + 1)^2 + (w + 1)}{2} </math>
we get that
- <math> w \leq \frac{\sqrt{8z + 1} - 1}{2} < w + 1 </math>
and thus
- <math> w = \left\lfloor \frac{\sqrt{8z + 1} - 1}{2} \right\rfloor. </math>
where Template:Math is the floor function. So to calculate Template:Math and Template:Math from Template:Math, we do:
- <math> w = \left\lfloor \frac{\sqrt{8z + 1} - 1}{2} \right\rfloor </math>
- <math> t = \frac{w^2 + w}{2} </math>
- <math> y = z - t \!</math>
- <math> x = w - y. \!</math>
Since the Cantor pairing function is invertible, it must be one-to-one and onto.Template:SfnTemplate:Additional citation needed
ExamplesEdit
To calculate Template:Math:
so Template:Math.
To find Template:Math and Template:Math such that Template:Math:
so Template:Math;
so Template:Math;
so Template:Math;
so Template:Math; thus Template:Math.Template:Citation needed
DerivationEdit
The graphical shape of Cantor's pairing function, a diagonal progression, is a standard trick in working with infinite sequences and countability.Template:Efn 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 Template:Mathth pair, what is the Template:Mathth pair? The way Cantor's function progresses diagonally across the plane can be expressed as
- <math>\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>\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: Template:Math.
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>\pi(x,y) = ax^2+by^2+cxy+dx+ey+f</math>.
Plug in our initial and boundary conditions to get Template:Math and:
- <math>bk^2+ek+1 = a(k+1)^2+d(k+1)</math>,
so we can match our Template:Math terms to get
So every parameter can be written in terms of Template:Math except for Template:Math, and we have a final equation, our diagonal step, that will relate them:
- <math>\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 Template:Math and Template:Math, and thus all parameters:
Therefore
- <math>\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.Template:Citation needed
Shifted Cantor pairing functionEdit
The following pairing function: <math>\langle i, j\rangle := \frac{1}{2}(i+j-2)(i+j-1) + i</math>, where <math>i, j\in\{1, 2, 3, \dots \}</math>.<ref>Template:Harvtxt cited in Template:Harv.</ref> is the same as the Cantor pairing function, but shifted to exclude 0 (i.e., <math>i=k_2+1</math>, <math>j=k_1+1</math>, and <math>\langle i, j\rangle - 1 = \pi(k_2,k_1)</math>).Template:Sfn It was used in the popular computer textbook of Hopcroft and Ullman (1979).
Other pairing functionsEdit
The function <math>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 if 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.Template:Clarify 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.Template:SfnTemplate:Clarify
In 2001, Pigeon proposed a pairing function based on bit-interleaving, defined recursively as:
- <math>\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>i_0</math> and <math>j_0</math> are the least significant bits of i and j respectively.Template:SfnTemplate:Bsn
In 2006, Szudzik proposed a "more elegant" pairing function defined by the expression:
- <math>\operatorname{ElegantPair}[x, y] := \begin{cases}
y^2 + x&\text{if}\ x < y,\\ x^2 + x + y&\text{if}\ x \ge y.\\ \end{cases}</math> Which can be unpaired using the expression:
- <math>\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}</math> (Qualitatively, it assigns consecutive numbers to pairs along the edges of squares.) This pairing function orders SK combinator calculus expressions by depth.Template:SfnTemplate:Clarify This method is the mere application to <math>\N</math> of the idea, found in most textbooks on Set Theory,<ref>See for instance Template:Harvtxt.</ref> used to establish <math>\kappa^2=\kappa</math> for any infinite cardinal <math>\kappa</math> in ZFC. Define on <math>\kappa\times\kappa</math> the binary relation
- <math>(\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}</math> <math>\preccurlyeq</math> is then shown to be a well-ordering such that every element has <math>{}<\kappa</math> predecessors, which implies that <math>\kappa^2=\kappa</math>. It follows that <math>(\N\times\N,\preccurlyeq)</math> is isomorphic to <math>(\N,\leqslant)</math> and the pairing function above is nothing more than the enumeration of integer couples in increasing order.Template:Efn
CitationsEdit
NotesEdit
FootnotesEdit
ReferencesEdit
- {{#invoke:Template wrapper|{{#if:|list|wrap}}|_template=cite web
|_exclude=urlname, _debug, id |url = https://mathworld.wolfram.com/{{#if:PairingFunction%7CPairingFunction.html}} |title = Pairing Function |author = Steven Pigeon |website = MathWorld |access-date = |ref = Template:SfnRef }}Template:Sfn whitelist
- Template:Cite journal
- Template:Cite journal
- {{#invoke:citation/CS1|citation
|CitationClass=web }}