encoding words

encoding words

Let

Σ

be an

alphabet

, and

Σ

*

the set of all words over

Σ

. An

encoding

of words over

Σ

is, loosely speaking, an assignment of words to numbers such that the numbers uniquely identify the words.

Definition

. An encoding for a

language

L

⊆

Σ

*

is a one-to-one function

E

:

L

→

ℕ

.

If

L

is finite, then it is easy to find an encoding for

L

. We are interested mainly in encoding

infinite sets

. By the definition above,

L

can not be encoded if it is

uncountable

. We can therefore limit the discussion to

Σ

that is at most

countably infinite

by listing some common methods of encoding

L

.

Among the methods liststed,

Σ

is an enumerated set

{

a

1

,

a

2

,

…

}

. In the first method,

Σ

is assumed to be finite, and countably infinite in the last three. In

addition

,

L

is assumed to be

Σ

*

in the first three methods.

Method 1

. First, set

E

⁢

(

a

i

)

:=

i

. In addition, for the

empty word

λ

, we set

E

⁢

(

λ

)

:=

0

. Next, inductively define

E

⁢

(

w

)

on the length of

w

. Suppose now that

E

⁢

(

w

)

has been defiend. Set

E

⁢

(

w

⁢

a

)

:=

n

⁢

E

⁢

(

w

)

+

E

⁢

(

a

)

,

where

⁢

a

∈

Σ

.

It is easy to see that if any non-empty word

w

∈

A

, with

w

=

b

1

⁢

⋯

⁢

b

m

, where

b

i

∈

Σ

. Then

E

⁢

(

w

)

=

E

⁢

(

b

1

)

⁢

n

m

-

1

+

⋯

+

E

⁢

(

b

m

)

.

Then

E

is an encoding of

L

. This is really just the base-

n

digital representation of integers

, where each

a

i

can be thought of as digits used by the

representation

. The only

difference

here is that

0

is not used as a “digit” (every letter gets mapped to a

positive integer

), except when the word is empty.

For example, let

Σ

=

{

0

,

1

,

…

,

9

}

. Then the words

01

,

001

, and

10

have code numbers

12

,

112

, and

21

.

It is easy to see that the encoding is

one-to-one

(see proof here (

http://planetmath.org/UniquenessOfDigitalRepresentation

)).

Method 2

. Pick three positive number

p

,

q

,

r

such that

p

,

q

are

coprime

, with

p

>

1

. Set

E

⁢

(

a

i

)

:=

p

i

and

E

⁢

(

λ

)

:=

1

. Inductively define

E

⁢

(

w

)

on length of

w

. Suppose now that

E

⁢

(

w

)

has been defined. Then

E

⁢

(

w

⁢

a

)

:=

(

r

⁢

E

⁢

(

w

)

+

q

)

⁢

E

⁢

(

a

)

,

where

⁢

a

∈

Σ

.

For example,

E

⁢

(

a

2

⁢

a

5

⁢

a

3

)

=

(

r

⁢

(

r

⁢

p

2

+

q

)

⁢

p

5

+

q

)

⁢

p

3

=

r

2

⁢

p

10

+

r

⁢

q

⁢

p

8

+

q

⁢

p

3

.

To see that

E

is injective, we make the following series of observations:

1.

E

is injective on

Σ

. In addition, either

E

(

a

)

|

E

(

b

)

or

E

(

b

)

|

E

(

a

)

for any

a

,

b

∈

Σ

.

2.

p

|

E

(

w

)

iff

w

≠

λ

.

3.

If

E

⁢

(

w

)

=

E

⁢

(

a

)

for some

a

∈

Σ

, then

w

=

a

. First, note that

w

≠

1

, and if

w

∈

Σ

, then

w

=

a

. So suppose

w

=

v

⁢

b

, with

b

∈

Σ

and

v

≠

λ

. Then

(

r

⁢

E

⁢

(

v

)

+

q

)

⁢

E

⁢

(

b

)

=

E

⁢

(

a

)

. If

E

(

b

)

|

E

(

a

)

, then

r

⁢

E

⁢

(

v

)

+

q

=

p

i

. Since

E

⁢

(

v

)

>

1

,

i

≠

0

. But if

i

>

0

,

p

and

q

would not be coprime as

p

|

E

(

v

)

. On the other hand, if

E

(

a

)

|

E

(

b

)

, then

(

E

⁢

(

v

)

+

q

)

⁢

p

j

=

1

, again impossible. So

w

must be a letter, and therefore is

a

.

4.

Now, suppose

E

⁢

(

w

)

=

E

⁢

(

v

)

, and

E

(

a

)

|

E

(

b

)

, where

a

,

b

are the rightmost letters of

w

,

v

respectively. By the same

argument

as previously,

a

=

b

, so we may cancel the letters, leaving us with the

equation

E

⁢

(

w

′

)

=

E

⁢

(

v

′

)

, where

w

=

w

′

⁢

a

and

v

=

v

′

⁢

b

. Continue the process of canceling the last letters in pairs, we end up with

E

⁢

(

u

)

=

E

⁢

(

c

)

for some letter

c

∈

Σ

. So

u

=

c

. This shows that

w

=

v

.

A variation of this method is to set

E

⁢

(

a

⁢

w

)

:=

(

r

⁢

E

⁢

(

w

)

+

q

)

⁢

E

⁢

(

a

)

.

If we set

p

=

2

and

r

=

q

=

1

, then the range of

E

is the set of all positive integers.

Method 3

. The third method utilizes the uniqueness of prime decomposition of

integers

. First, define

f

:

Σ

→

ℕ

by

f

⁢

(

a

i

)

=

i

. Then, for any

w

=

b

1

⁢

⋯

⁢

b

m

, with

b

i

∈

Σ

, define

E

⁢

(

w

)

:=

p

1

f

⁢

(

b

1

)

⁢

⋯

⁢

p

m

f

⁢

(

b

m

)

=

∏

i

=

1

m

p

i

f

⁢

(

b

i

)

,

where

p

i

is the

i

-th

prime number

(for example,

p

1

=

2

). We again set

E

⁢

(

λ

)

:=

1

. By the

fundamental theorem of arithmetic

, and the fact that

f

is a

bijection

,

E

is injective (and maps onto the set of positive integers). This method is known as the

multiplicative encoding

of Gödel.

Method 4

. Once an encoding

E

is found for

Σ

*

, an encoding for

L

⊆

Σ

*

can be obtained by restricting the domain of

E

to

L

. Depending on how

L

is defined, other methods of encoding

L

via

E

are possible. We illustrate one example.

Let

L

=

L

1

∪

L

2

⁢

Σ

*

, where

L

1

,

L

2

are

disjoint

non-empty

finite sets

not containing the empty word. Encode

L

as follows: suppose

L

1

=

{

v

1

,

…

,

v

m

}

and

L

2

=

{

w

1

,

…

,

w

n

}

. Define

E

′

:

L

→

ℕ

such that:

1.

E

′

⁢

(

v

i

)

:=

10

i

-

1

and

E

′

⁢

(

w

j

)

:=

10

m

+

j

-

1

, where

i

∈

{

1

,

…

,

m

}

and

j

∈

{

1

,

…

,

n

}

;

2.

E

′

⁢

(

w

)

:=

E

′

⁢

(

w

j

)

⁢

E

⁢

(

u

)

⁢

10

m

+

n

-

1

, where

w

=

w

j

⁢

u

, and

λ

≠

u

∈

Σ

*

.

Essentially, the first

m

+

n

digits are reserved for

encoding words

v

i

and

w

j

.

E

′

is easily seen to be injective.

Method 5

. Let

L

2

be the language consisting of all words of length

2

. Define

E

2

:

L

2

→

ℕ

by

E

2

⁢

(

a

i

⁢

a

j

)

:=

J

⁢

(

i

,

j

)

, where

J

is a

pairing function

that codes pairs of positive integers. Since

J

is an injection (actually maps onto the set of positive integers), so is

E

2

. Using

J

, one can encode the language

L

3

of all length

3

words. Define

E

3

:

L

2

→

ℕ

by

E

3

⁢

(

a

i

⁢

a

j

⁢

a

k

)

:=

J

⁢

(

i

,

J

⁢

(

j

,

k

)

)

. Again,

E

3

is an injection. By

induction

, one can encode the language

L

n

of all words of length

n

, for any positive integer

n

.

Method 6

. Let

L

⁢

(

n

)

be the language consisting of all words of length at most

n

. We can utilize Method 5 to code

L

. First, let

Σ

1

:=

Σ

∪

{

a

0

}

, where

a

0

is a letter not in

Σ

. Define

ϕ

:

L

⁢

(

n

)

→

Σ

1

*

by

ϕ

⁢

(

w

)

:=

a

0

n

-

|

w

|

⁢

w

, where

|

w

|

is the length of

w

. Then

ϕ

⁢

(

L

)

⊆

L

n

, the language of all length

n

words over

Σ

1

*

. It is easy to see that

ϕ

is one-to-one. Then

E

⁢

(

n

)

:=

E

n

∘

ϕ

is an encoding for

L

, where

E

n

is defined in Method 5 that encodes

L

n

, via the modified version of the pairing function

J

′

⁢

(

i

,

j

)

:=

J

⁢

(

i

+

1

,

j

+

1

)

, where

i

,

j

≥

0

.

Method 7

. Can Method 5 be used to encode

Σ

*

? The answer is yes. However, a direct

extension

of

E

n

does not work. By this we mean that

E

:

Σ

*

→

ℕ

, given by

E

⁢

(

w

)

=

E

n

⁢

(

w

)

where

|

w

|

=

n

, though a

function

, is not injective. For any positive integer

m

, there is a word

w

n

of length

n

for every

n

>

0

, such that

E

n

⁢

(

w

n

)

=

m

. Instead, define

E

so that

E

⁢

(

w

)

:=

E

2

⁢

(

|

w

|

,

E

|

w

|

⁢

(

w

)

)

if

w

≠

λ

, and

E

⁢

(

λ

)

:=

0

. It is easy to see that

E

is injective, since both

E

2

and

E

n

are (in fact,

E

is a bijection).

Remark

. An encoding

E

for

L

can be thought of as a

partial function

from

Σ

*

to

ℕ

, whose domain is

L

⊆

Σ

*

.

E

is said to be

effective

if

E

⁢

(

L

)

is a

recursive set

. Equivalently, the partial function

E

*

on

Σ

*

given by

E

*

⁢

(

w

)

=

{

a

1

E

⁢

(

w

)

if

⁢

w

∈

L

,

undefined

otherwise

.

is

Turing-computable

. An enumeration of

Σ

can be thought of as an encoding for

Σ

. If

Σ

is finite, any enumeration of

Σ

is effective. Assume that

Σ

is effectively enumerated, whether or not

Σ

is finite (so that

a

1

in the definition of

E

*

can be effectively chosen). Then it is not hard to see that all of the encodings described above are effective. In fact, all of the sets

E

⁢

(

L

)

described are

primitive recursive

.

Title

encoding words

Canonical

name

EncodingWords

Date of creation

2013-03-22 19:06:09

Last modified on

2013-03-22 19:06:09

Owner

CWoo (3771)

Last modified by

CWoo (3771)

Numerical id

20

Author

CWoo (3771)

Entry type

Feature

Classification

msc 94A60

Classification

msc 68Q05

Classification

msc 68Q45

Classification

msc 03D20

Generated on Sat Feb 10 13:42:37 2018 by

LaTeXML