Pairing function

Function uniquely mapping two numbers into a single number


title: "Pairing function" type: doc version: 1 created: 2026-02-28 author: "Wikipedia contributors" status: active scope: public tags: ["set-theory", "georg-cantor", "functions-and-mappings"] description: "Function uniquely mapping two numbers into a single number" topic_path: "science/mathematics" source: "https://en.wikipedia.org/wiki/Pairing_function" license: "CC BY-SA 4.0" wikipedia_page_id: 0 wikipedia_revision_id: 0

::summary Function uniquely mapping two numbers into a single number ::

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}.

Generalization

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 distinct pairs of elements of A are associated with distinct elements of A, or a bijection from A^2 to A.

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 \mathbb{N}.

Cantor pairing function

::figure[src="https://upload.wikimedia.org/wikipedia/commons/c/c3/Cantor's_Pairing_Function.svg" caption="The Cantor pairing function assigns one natural number to each pair of natural numbers" alt="A plot of the Cantor pairing function"] ::

::figure[src="https://upload.wikimedia.org/wikipedia/commons/6/61/Cantor's_Pairing_Function_Plot.svg" caption="Graph of the Cantor pairing function" alt="A graph of the 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=\binom{k_1+k_2+1}{2}+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 with respect to each argument, that is, for all k_1, k_1', k_2, k_2' \in \mathbb{N}, if k_1 , then \pi(k_1, k_2) ; similarly, if k_2 , then \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 k1 and k2 we often denote the resulting number as ⟨k1, k2⟩.

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).

Another generalization of the Cantor pairing function to a bijection \pi^{(n)}\colon\mathbb{N}^n \to \mathbb{N} is provided by the combinatorial number system: :\pi^{(n)}(x_1,\dots,x_n)=\binom{x_1+\dots+x_n+n-1}{n}+\binom{x_1+\dots+x_{n-1}+n-2}{n-1}+\dots+\binom{x_1+x_2+1}{2}+\binom{x_1}1.

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

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

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 .

To find x and y such that : :, :, :, :, :, :, so ; :, :, :, so ; :, so ; :, so ; thus .

Derivation

::figure[src="https://upload.wikimedia.org/wikipedia/commons/8/85/Diagonal_argument.svg" caption="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. 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 :\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: .

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 and: :bk^2+ek+1 = a(k+1)^2+d(k+1),

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: :\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 a and c, 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.

Shifted Cantor pairing function

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 }. is the same as the Cantor pairing function, but 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)). It was used in the popular computer textbook of Hopcroft and Ullman (1979).

For ordinal numbers

There exists a "canonical" pairing function for ordinal numbers which is simultaneously a pairing function for every aleph number (i.e., the initial ordinal of every infinite well-orderable cardinal number). It is induced by the following well-ordering of pairs of ordinal numbers: :(\alpha,\beta)\preccurlyeq(\gamma,\delta) \text{ if either } \begin{cases} (\alpha,\beta) = (\gamma,\delta),\[4pt] \max(\alpha,\beta) \max(\alpha,\beta) = \max(\gamma,\delta)\ \text{and}\ \alpha \max(\alpha,\beta) = \max(\gamma,\delta)\ \text{and}\ \alpha=\gamma\ \text{and}\ \beta \end{cases}

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 {{tmath|1=\lambda \times \lambda = \bigcup_{\alpha

  • If , then by continuity since is a natural number for every natural number .
  • If is an initial ordinal, then by continuity since {{tmath|1=\vert \gamma(\delta) \vert = \vert \delta \times \delta \vert = \vert \delta \vert^2 = \vert \delta \vert

An important implication of this pairing function is that for all well-orderable infinite cardinal number . In particular, in ZFC every cardinal number is well-orderable, so holds for all infinite cardinal . Conversely, the statement " holds for all infinite cardinal " implies the axiom of choice; this result is known as Tarski's theorem about choice.

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: :\operatorname{ElegantPair}[x, y] := \begin{cases} y^2 + x&\text{if}\ x 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 \ \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.)

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 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. 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:

:\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.

Citations

Notes

Footnotes

References

  • {{cite book | last1 = Hopcroft | first1 = John E. |author1link = John E. Hopcroft |author2link = Jeffrey D. Ullman | last2 = Ullman | first2 = Jeffrey D. | title = Introduction to Automata Theory, Languages, and Computation | publisher = Addison-Wesley | edition = 1st | year = 1979 | isbn = 0-201-02988-X

References

  1. {{Harvnb. Pigeon:
    "Pairing functions arise naturally in the demonstration that the cardinalities of the rationals \mathbb{Q} and the nonnegative integers \mathbb{Z}{\geq 0} are the same, i.e., . \mathbb{Q}. \mathbb{Z}{\geq 0}
  2. {{harvtxt. Stein. 1999. Pigeon.
  3. {{harvtxt. Hopcroft. Ullman. 1979. Pigeon

::callout[type=info title="Wikipedia Source"] This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page. ::

set-theorygeorg-cantorfunctions-and-mappings