Pairing function

From testwiki
Jump to navigation Jump to search

Template:Short description Template:More citations needed Template:Use dmy dates

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]

Definition

A pairing function is a bijection

π:ℕ×ℕ→ℕ.Template:SfnTemplate:SfnTemplate:Sfn

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,Template:SfnTemplate:Efn or a bijection from A2 to A.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 ℕ.Template:Sfn

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

π:ℕ×ℕ→ℕ

defined by

π(k1,k2):=12(k1+k2)(k1+k2+1)+k2=(k1+k2+12)+k2

where k1,k2∈{0,1,2,3,…}.Template:SfnTemplate:Bsn

It can also be expressed as π(x,y):=x2+x+2xy+3y+y22.Template:Sfn

It is also strictly monotonic with respect to each argument, that is, for all k1,k1′,k2,k2′∈ℕ, if k1<k1′, then π(k1,k2)<π(k1′,k2); similarly, if k2<k2′, then π(k1,k2)<π(k1,k2′).Template:Citation needed

The statement that this is the only quadratic pairing function is known as the Fueter–Pólya theorem.[2] 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:Sfn

This definition can be inductively generalized to the Template:Citation needed span

π(n):ℕn→ℕ

for n>2 as

π(n)(k1,…,kn−1,kn):=π(π(n−1)(k1,…,kn−1),kn)

with the base case defined above for a pair: π(2)(k1,k2):=π(k1,k2).Template:Sfn

Another generalization of the Cantor pairing function to a bijection π(n):ℕn→ℕ is provided by the combinatorial number system:

π(n)(x1,…,xn)=(x1+…+xn+n−1n)+(x1+…+xn−1+n−2n−1)+…+(x1+x2+12)+(x11).

Inverting the Cantor pairing function

Let z∈ℕ be an arbitrary natural number. We will show that there exist unique values x,y∈ℕ such that

z=π(x,y)=(x+y+1)(x+y)2+y

and hence that the function Template:Math is invertible. It is helpful to define some intermediate values in the calculation:

w=x+y
t=12w(w+1)=w2+w2
z=t+y

where Template:Math is the triangle number of Template:Math. If we solve the quadratic equation

w2+w−2t=0

for Template:Math as a function of Template:Math, we get

w=8t+1−12

which is a strictly increasing and continuous function when Template:Math is non-negative real. Since

t≤z=t+y<t+(w+1)=(w+1)2+(w+1)2

we get that

w≤8z+1−12<w+1

and thus

w=⌊8z+1−12⌋.

where Template:Math is the floor function. So to calculate Template:Math and Template:Math from Template:Math, we do:

w=⌊8z+1−12⌋
t=w2+w2
y=z−t
x=w−y.

Since the Cantor pairing function is invertible, it must be one-to-one and onto.Template:SfnTemplate:Additional citation needed

Examples

To calculate Template:Math:

Template:Math,
Template:Math,
Template:Math,
Template:Math,
Template:Math,

so Template:Math.

To find Template:Math and Template:Math such that Template:Math:

Template:Math,
Template:Math,
Template:Math,
Template:Math,
Template:Math,
Template:Math,

so Template:Math;

Template:Math,
Template:Math,
Template:Math,

so Template:Math;

Template:Math,

so Template:Math;

Template:Math,

so Template:Math; thus Template:Math.

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

π(x,y)+1=π(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:

π(0,k)+1=π(k+1,0).

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

π(x,y)=ax2+by2+cxy+dx+ey+f.

Plug in our initial and boundary conditions to get Template:Math and:

bk2+ek+1=a(k+1)2+d(k+1),

so we can match our Template:Math terms to get

Template:Math
Template:Math
Template:Math.

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:

π(x,y)+1=a(x2+y2)+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).

Expand and match terms again to get fixed values for Template:Math and Template:Math, and thus all parameters:

Template:Math
Template:Math
Template:Math
Template:Math.

Therefore

π(x,y)=12(x2+y2)+xy+12x+32y=12(x+y)(x+y+1)+y,

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 function

The following pairing function: ⟨i,j⟩:=12(i+j−2)(i+j−1)+i, where i,j∈{1,2,3,…}.[3] is the same as the Cantor pairing function, but shifted to exclude 0 (i.e., i=k2+1, j=k1+1, and ⟨i,j⟩−1=π(k2,k1)).Template:Sfn 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:Template:Sfn

(α,β)≼(γ,δ) if either {(α,β)=(γ,δ),max⁡(α,β)<max⁡(γ,δ),max⁡(α,β)=max⁡(γ,δ) and α<γ, ormax⁡(α,β)=max⁡(γ,δ) and α=γ and β<δ.

The basic idea is that Template:Tmath is used as the primary sort key. Therefore, for every ordinal Template:Tmath, all pairs with both entries less than Template:Tmath comes before all other pairs; in other words, the Cartesian product Template:Tmath is mapped to an initial segment of this new ordering, with the order type of the initial segment denoted by Template:Tmath.

Since Template:Tmath is a strictly increasing ordinal sequence, Template:Tmath. It is also continuous, since for limit ordinal Template:Tmath we have Template:Tmath. Now for all aleph number Template:Tmath, Template:Tmath can be proved by transfinite induction:Template:Sfn

An important implication of this pairing function is that Template:Tmath for all well-orderable infinite cardinal number Template:Tmath. In particular, in ZFC every cardinal number is well-orderable, so Template:Tmath holds for all infinite cardinal Template:Tmath. Conversely, the statement "Template:Tmath holds for all infinite cardinal Template:Tmath" 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 Template:Tmath yields a pairing function different from the Cantor pairing function, which was considered "more elegant" by Szudzik.Template:Sfn The explicit expression defining this pairing function is:

ElegantPair⁡[x,y]:={y2+xif x<y,x2+x+yif x≥y.

Which can be unpaired using the expression:

ElegantUnpair⁡[z]:={{z−⌊z⌋2,⌊z⌋}if z−⌊z⌋2<⌊z⌋,{⌊z⌋,z−⌊z⌋2−⌊z⌋}if z−⌊z⌋2≥⌊z⌋.

(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 Template:Tmath natural numbers represent distinct types of leaves, and Template:Tmath represents a binary tree with left and right subtrees represented by Template:Tmath and Template:Tmath 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.Template:Sfn

Other pairing functions

The function P2(x,y):=2x(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.Template:Sfn

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

⟨i,j⟩P={⊥if i=j=0;⟨⌊i/2⌋,⌊j/2⌋⟩P:i0:j0otherwise,

where i0 and j0 are the least significant bits of i and j respectively.Template:SfnTemplate:Bsn

Citations

Notes

Template:Notelist

Footnotes

Template:Reflist

References

Template:Refbegin

Template:Refend