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.[1]
A pairing function is a bijection
More generally, a pairing function on a set A {\displaystyle A} is a function that maps each pair of elements from A {\displaystyle A} into an element of A {\displaystyle A} , such that any two pairs of elements of A {\displaystyle A} are associated with different elements of A {\displaystyle A} ,[5][a] or a bijection from A 2 {\displaystyle A^{2}} to A {\displaystyle A} .[6]
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 N {\displaystyle \mathbb {N} } .[3]
The Cantor pairing function is a primitive recursive pairing function
defined by
where k 1 , k 2 ∈ { 0 , 1 , 2 , 3 , … } {\displaystyle k_{1},k_{2}\in \{0,1,2,3,\dots \}} .[7][better source needed]
It can also be expressed as π ( x , y ) := x 2 + x + 2 x y + 3 y + y 2 2 {\displaystyle \pi (x,y):={\frac {x^{2}+x+2xy+3y+y^{2}}{2}}} .[5]
It is also strictly monotonic w.r.t. each argument, that is, for all k 1 , k 1 ′ , k 2 , k 2 ′ ∈ N {\displaystyle k_{1},k_{1}',k_{2},k_{2}'\in \mathbb {N} } , if k 1 < k 1 ′ {\displaystyle k_{1}<k_{1}'} , then π ( k 1 , k 2 ) < π ( k 1 ′ , k 2 ) {\displaystyle \pi (k_{1},k_{2})<\pi (k_{1}',k_{2})} ; similarly, if k 2 < k 2 ′ {\displaystyle k_{2}<k_{2}'} , then π ( k 1 , k 2 ) < π ( k 1 , k 2 ′ ) {\displaystyle \pi (k_{1},k_{2})<\pi (k_{1},k_{2}')} .[citation needed]
The statement that this is the only quadratic pairing function is known as the Fueter–Pólya theorem.[8] 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]
for n > 2 {\displaystyle n>2} as
with the base case defined above for a pair: π ( 2 ) ( k 1 , k 2 ) := π ( k 1 , k 2 ) . {\displaystyle \pi ^{(2)}(k_{1},k_{2}):=\pi (k_{1},k_{2}).} [9]
Let z ∈ N {\displaystyle z\in \mathbb {N} } be an arbitrary natural number. We will show that there exist unique values x , y ∈ N {\displaystyle x,y\in \mathbb {N} } such that
and hence that the function π(x, y) is invertible. It is helpful to define some intermediate values in the calculation:
where t is the triangle number of w. If we solve the quadratic equation
for w as a function of t, we get
which is a strictly increasing and continuous function when t is non-negative real. Since
we get that
and thus
where ⌊ ⌋ is the floor function. So to calculate x and y from z, we do:
Since the Cantor pairing function is invertible, it must be one-to-one and onto.[5][additional citation(s) needed]
To calculate π(47, 32):
so π(47, 32) = 3192.
To find x and y such that π(x, y) = 1432:
so w = 53;
so t = 1431;
so y = 1;
so x = 52; thus π(52, 1) = 1432.[citation needed]
The graphical shape of Cantor's pairing function, a diagonal progression, is a standard trick in working with infinite sequences and countability.[b] 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
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:
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
Plug in our initial and boundary conditions to get f = 0 and:
so we can match our k terms to get
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:
Expand and match terms again to get fixed values for a and c, and thus all parameters:
Therefore
is the Cantor pairing function, and we also demonstrated through the derivation that this satisfies all the conditions of induction.[citation needed]
The following pairing function: ⟨ i , j ⟩ := 1 2 ( i + j − 2 ) ( i + j − 1 ) + i {\displaystyle \langle i,j\rangle :={\frac {1}{2}}(i+j-2)(i+j-1)+i} , where i , j ∈ { 1 , 2 , 3 , … } {\displaystyle i,j\in \{1,2,3,\dots \}} .[10] is the same as the Cantor pairing function, but shifted to exclude 0 (i.e., i = k 2 + 1 {\displaystyle i=k_{2}+1} , j = k 1 + 1 {\displaystyle j=k_{1}+1} , and ⟨ i , j ⟩ − 1 = π ( k 2 , k 1 ) {\displaystyle \langle i,j\rangle -1=\pi (k_{2},k_{1})} ).[7] It was used in the popular computer textbook of Hopcroft and Ullman (1979).
The function P 2 ( x , y ) := 2 x ( 2 y + 1 ) − 1 {\displaystyle 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 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.[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:
where i 0 {\displaystyle i_{0}} and j 0 {\displaystyle j_{0}} are the least significant bits of i and j respectively.[11][better source needed]
In 2006, Szudzik proposed a "more elegant" pairing function defined by the expression:
Which can be unpaired using the expression:
(Qualitatively, it assigns consecutive numbers to pairs along the edges of squares.) This pairing function orders SK combinator calculus expressions by depth.[5][clarification needed] This method is the mere application to N {\displaystyle \mathbb {N} } of the idea, found in most textbooks on Set Theory,[12] used to establish κ 2 = κ {\displaystyle \kappa ^{2}=\kappa } for any infinite cardinal κ {\displaystyle \kappa } in ZFC. Define on κ × κ {\displaystyle \kappa \times \kappa } the binary relation
≼ {\displaystyle \preccurlyeq } is then shown to be a well-ordering such that every element has < κ {\displaystyle {}<\kappa } predecessors, which implies that κ 2 = κ {\displaystyle \kappa ^{2}=\kappa } . It follows that ( N × N , ≼ ) {\displaystyle (\mathbb {N} \times \mathbb {N} ,\preccurlyeq )} is isomorphic to ( N , ⩽ ) {\displaystyle (\mathbb {N} ,\leqslant )} and the pairing function above is nothing more than the enumeration of integer couples in increasing order.[c]
"Pairing functions arise naturally in the demonstration that the cardinalities of the rationals Q {\displaystyle \mathbb {Q} } and the nonnegative integers Z ≥ 0 {\displaystyle \mathbb {Z} _{\geq 0}} are the same, i.e., | Q | = | Z ≥ 0 | = ℵ 0 {\displaystyle |\mathbb {Q} |=|\mathbb {Z} _{\geq 0}|=\aleph _{0}} , originally due to Cantor."