Puneet Varma (Editor)

Pairing function

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit
Pairing function

In mathematics a pairing function is a process to uniquely encode two natural numbers into a single natural number.

Contents

Any pairing function can be used in set theory to prove that integers and rational numbers have the same cardinality as natural numbers. In theoretical computer science they are used to encode a function defined on a vector of natural numbers f : N k N into a new function g : N N .

Definition

A pairing function is a primitive recursive bijection

π : N × N N .

Cantor pairing function

The Cantor pairing function is a pairing function

π : N × N N

defined by

π ( k 1 , k 2 ) := 1 2 ( k 1 + k 2 ) ( k 1 + k 2 + 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 Cantor tuple function

π ( n ) : N n N

as

π ( n ) ( k 1 , , k n 1 , k n ) := π ( π ( n 1 ) ( k 1 , , k n 1 ) , k n ) .

Inverting the Cantor pairing function

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

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

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

w = x + y t = 1 2 w ( w + 1 ) = w 2 + w 2 z = t + y

where t is the triangle number of w. If we solve the quadratic equation

w 2 + w 2 t = 0

for w as a function of t, we get

w = 8 t + 1 1 2

which is a strictly increasing and continuous function when t is non-negative real. Since

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

we get that

w 8 z + 1 1 2 < w + 1

and thus

w = 8 z + 1 1 2 .

where ⌊ ⌋ is the floor function. So to calculate x and y from z, we do:

w = 8 z + 1 1 2 t = 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):

47 + 32 = 79, 79 + 1 = 80, 79 × 80 = 6320, 6320 ÷ 2 = 3160, 3160 + 32 = 3192,

so π(47, 32) = 3192.

To find x and y such that π(x, y) = 1432:

8 × 1432 = 11456, 11456 + 1 = 11457, √11457 = 107.037, 107.037 − 1 = 106.037, 106.037 ÷ 2 = 53.019, ⌊53.019⌋ = 53,

so w = 53;

53 + 1 = 54, 53 × 54 = 2862, 2862 ÷ 2 = 1431,

so t = 1431;

1432 − 1431 = 1,

so y = 1;

53 − 1 = 52,

so x = 52; thus π(52, 1) = 1432.

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 nth pair, what is the (n+1)th 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: π(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

π ( x , y ) = a x 2 + b y 2 + c x y + d x + e y + f .

Plug in our initial and boundary conditions to get f = 0 and:

b k 2 + e k + 1 = a ( k + 1 ) 2 + d ( k + 1 ) ,

so we can match our k terms to get

b = a d = 1-a e = 1+a.

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:

π ( x , y ) + 1 = a ( x 2 + y 2 ) + c x y + ( 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 a and c, and thus all parameters:

a = 1/2 = b = d c = 1 e = 3/2 f = 0.

Therefore

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

References

Pairing function Wikipedia