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 bijectionGeneralization
More generally, a pairing function on a set is a function that maps each pair of elements from into an element of, such that distinct pairs of elements of are associated with distinct elements of, or a bijection from to.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.
Cantor pairing function
The Cantor pairing function is a primitive recursive pairing functiondefined by
where.
It can also be expressed as.
It is also strictly monotonic with respect to each argument, that is, for all, if, then ; similarly, if, then.
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 and we often denote the resulting number as.
This definition can be inductively generalized to the
for as
with the base case defined above for a pair:
Another generalization of the Cantor pairing function to a bijection is provided by the combinatorial number system:
Inverting the Cantor pairing function
Let be an arbitrary natural number. We will show that there exist unique values such thatand hence that the function is invertible. It is helpful to define some intermediate values in the calculation:
where is the triangle number of. If we solve the quadratic equation
for as a function of, we get
which is a strictly increasing and continuous function when is non-negative real. Since
we get that
and thus
where is the floor function.
So to calculate and from, we do:
Since the Cantor pairing function is invertible, it must be one-to-one and onto.
Examples
To calculate :so.
To find and such that :
so ;
so ;
so ;
so ; thus.
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 th pair, what is the 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:.
Assume that there is a quadratic 2-dimensional polynomial that can fit these conditions. The general form is then
Plug in our initial and boundary conditions to get and:
so we can match our terms to get
So every parameter can be written in terms of except for, and we have a final equation, our diagonal step, that will relate them:
Expand and match terms again to get fixed values for and, 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.
Shifted Cantor pairing function
The following pairing function:, where. is the same as the Cantor pairing function, but shifted to exclude 0. It was used in the popular computer textbook of Hopcroft and Ullman.For ordinal numbers
There exists a "canonical" pairing function for ordinal numbers which is simultaneously a pairing function for every aleph number. It is induced by the following well-ordering of pairs of ordinal numbers:The basic idea is that is used as the primary sort key. Therefore, for every ordinal, all pairs with both entries less than comes before all other pairs; in other words, the Cartesian product is mapped to an initial segment of this new ordering, with the order type of the initial segment denoted by.
Since is a strictly increasing ordinal sequence,. It is also continuous, since for limit ordinal we have. Now for all aleph number, can be proved by transfinite induction:
- If, then by continuity since is a natural number for every natural number.
- If is an initial ordinal, then by continuity since for all infinite, where can be shown by applying the inductive hypothesis to the initial ordinal of.
Restriction to natural numbers
Restriction of the "canonical" pairing function for ordinal numbers to the set of natural numbers yields a pairing function different from the Cantor pairing function, which was considered "more elegant" by Szudzik. The explicit expression defining this pairing function is:Which can be unpaired using the expression:
One advantage of this pairing function manifests when using a pair function to represent a binary tree-like structure, where the first natural numbers represent distinct types of leaves, and represents a binary tree with left and right subtrees represented by and respectively. This pairing function guarantees that all binary trees are ordered by depth. A concrete example of such a binary tree-like structure is an SK combinator calculus expression.
Other pairing functions
The function is a pairing function.In 1990, Regan proposed the first known pairing function that is computable in linear time and with constant space. In fact, both this pairing function and its inverse can be computed with finite-state transducers. 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 constant space.
In 2001, Pigeon proposed a pairing function based on bit-interleaving, defined recursively as:
where and are the least significant bits of i and j respectively.