Pairing function; function <math>v

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. In theoretical computer science they are used to encode a function defined on a vector of natural numbers f:NkN into a new function g:NN.

Every pairing function is primitive recursive.

Contents


Definition

A pairing function is a bijective function

<math>\pi:\mathbb{N} \times \mathbb{N} \to \mathbb{N}.</math>


Cantor pairing function

The Cantor pairing function is a pairing function

<math>\pi:\mathbb{N} \times \mathbb{N} \to \mathbb{N}</math>

defined by

<math>\pi(k_1,k_2) := \frac{1}{2}(k_1 + k_2)(k_1 + k_2 + 1)+k_2.</math>

When we apply the pairing function to <math>k_1</math> and <math>k_2</math> we often denote the resulting number as <math>\langle k_1, k_2 \rangle</math>

This definition can be inductively generalized to the Cantor tuple function

<math>\pi^{(n)}:\mathbb{N}^n \to \mathbb{N}</math>

as

<math>\pi^{(n)}(k_1, \ldots, k_{n-1}, k_n) := \pi ( \pi^{(n-1)}(k_1, \ldots, k_{n-1}) , k_n)</math>


Inverting the Cantor pairing function

Suppose we are given z with

<math> z = \langle x, y \rangle = \frac{(x + y)(x + y + 1)}{2} + y </math>

and we want to find x and y. It is helpful to define some intermediate values in the calculation:

<math> w = x + y \!</math>
<math> t = \frac{w(w + 1)}{2} = \frac{w^2 + w}{2} </math>
<math> z = t + y \!</math>

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

<math> w^2 + w - 2t = 0 \!</math>

for w as a function of t, we get

<math> w = \frac{\sqrt{8t + 1} - 1}{2} </math>

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

<math> t \leq z = t + y < t + (w + 1) = \frac{(w + 1)^2 + (w + 1)}{2} </math>

we get that

<math> w \leq \frac{\sqrt{8z + 1} - 1}{2} < w + 1 </math>

and thus

<math> w = \left\lfloor \frac{\sqrt{8z + 1} - 1}{2} \right\rfloor </math>.

So to calculate x and y from z, we do:

<math> w = \left\lfloor \frac{\sqrt{8z + 1} - 1}{2} \right\rfloor </math>
<math> t = \frac{w^2 + w}{2} </math>
<math> y = z - t \!</math>
<math> x = w - y \!</math>.

Since the Cantor pairing function is invertible, it must be one-to-one and onto.


References

Leave a Reply

You must be logged in to post a comment.