String operations

From Wikipedia, the free encyclopedia

In computer science, in the area of formal language theory, frequent use is made of a variety of string functions; however, the notation used is different from that used on computer programming, and some commonly used functions in the theoretical realm are rarely used when programming. This article defines some of these basic terms.

Strings and languages

A string is a finite sequence of characters. The empty string is denoted by \varepsilon . The concatenation of two string s and t is denoted by s\cdot t, or shorter by st. Concatenating with the empty string makes no difference: s\cdot \varepsilon =s=\varepsilon \cdot s. Concatenation of strings is associative: s\cdot (t\cdot u)=(s\cdot t)\cdot u.

For example, (\langle b\rangle \cdot \langle l\rangle )\cdot (\varepsilon \cdot \langle ah\rangle )=\langle bl\rangle \cdot \langle ah\rangle =\langle blah\rangle .

A language is a finite or infinite set of strings. Besides the usual set operations like union, intersection etc., concatenation can be applied to languages: if both S and T are languages, their concatenation S\cdot T is defined as the set of concatenations of any string from S and any string from T, formally S\cdot T=\{s\cdot t\mid s\in S\land t\in T\}. Again, the concatenation dot \cdot is often omitted for shortness.

The language \{\varepsilon \} consisting of just the empty string is to be distinguished from the empty language \{\}. Concatenating any language with the former doesn't make any change: S\cdot \{\varepsilon \}=S=\{\varepsilon \}\cdot S, while concatenating with the latter always yields the empty language: S\cdot \{\}=\{\}=\{\}\cdot S. Concatenation of languages is associtive: S\cdot (T\cdot U)=(S\cdot T)\cdot U.

For example, abbreviating D=\{\langle 0\rangle ,\langle 1\rangle ,\langle 2\rangle ,\langle 3\rangle ,\langle 4\rangle ,\langle 5\rangle ,\langle 6\rangle ,\langle 7\rangle ,\langle 8\rangle ,\langle 9\rangle \}, the set of all three-digit decimal numbers is obtained as D\cdot D\cdot D. The set of all decimal numbers of arbitrary length is an example for an infinite language.

Alphabet of a string

The alphabet of a string is the set of all of the characters that occur in a particular string. If s is a string, its alphabet is denoted by

\operatorname {Alph}(s)

The alphabet of a language S is the set of all characters that occur in any string of S, formally: \operatorname {Alph}(S)=\bigcup _{{s\in S}}\operatorname {Alph}(s).

For example, the set \{\langle a\rangle ,\langle c\rangle ,\langle o\rangle \} is the alphabet of the string \langle cacao\rangle , and the above D is the alphabet of the above language D\cdot D\cdot D as well as of the language of all decimal numbers.

String substitution

Let L be a language, and let \Sigma be its alphabet. A string substitution or simply a substitution is a mapping f that maps letters in \Sigma to languages (possibly in a different alphabet). Thus, for example, given a letter a\in \Sigma , one has f(a)=L_{a} where L_{a}\subseteq \Delta ^{*} is some language whose alphabet is \Delta . This mapping may be extended to strings as

f(\varepsilon )=\varepsilon

for the empty string \varepsilon , and

f(sa)=f(s)f(a)

for string s\in L. String substitution may be extended to the entire language as

f(L)=\bigcup _{{s\in L}}f(s)

Regular languages are closed under string substitution. That is, if each letter of a regular language is substituted by another regular language, the result is still a regular language.

A simple example is the conversion f_{{uc}}(\cdot ) to upper case, which may be defined e.g. as follows:

letter mapped to language remark
x f_{{uc}}(x)
\langle a\rangle \{\langle A\rangle \} map lower-case char to corresponding upper-case char
\langle A\rangle \{\langle A\rangle \} map upper-case char to itself
\langle {\text{ß}}\rangle \{\langle SS\rangle \} no upper-case char available, map to two-char string
\langle 0\rangle \{\varepsilon \} map digit to empty string
\langle !\rangle \{\} forbid punctuation, map to empty language
\ldots similar for other chars

For the extension of f_{{uc}} to strings, we have e.g.

  • f_{{uc}}(\langle {\text{Straße}}\rangle )=\{\langle S\rangle \}\cdot \{\langle T\rangle \}\cdot \{\langle R\rangle \}\cdot \{\langle A\rangle \}\cdot \{\langle SS\rangle \}\cdot \{\langle E\rangle \}=\{\langle STRASSE\rangle \},
  • f_{{uc}}(\langle u2\rangle )=\{\langle U\rangle \}\cdot \{\varepsilon \}=\{\langle U\rangle \}, and
  • f_{{uc}}(\langle Go!\rangle )=\{\langle G\rangle \}\cdot \{\langle O\rangle \}\cdot \{\}=\{\}.

For the extension of f_{{uc}} to languages, we have e.g.

  • f_{{uc}}(\{\langle {\text{Straße}}\rangle ,\langle u2\rangle ,\langle Go!\rangle \})=\{\langle STRASSE\rangle \}\cup \{\langle U\rangle \}\cup \{\}=\{\langle STRASSE\rangle ,\langle U\rangle \}.


Another example is the conversion of an EBCDIC-encoded string to ASCII.

String homomorphism

A string homomorphism (often referred to simply as a homomorphism in formal language theory) is a string substitution such that each letter is replaced by a single string. That is, f(a)=s, where s is a string, for each letter a.

String homomorphisms are monoid morphisms on the free monoid, preserving the binary operation of string concatenation. Given a language L, the set f(L) is called the homomorphic image of L. The inverse homomorphic image of a string s is defined as

f^{{-1}}(s)=\{w\vert f(w)=s\}

while the inverse homomorphic image of a language L is defined as

f^{{-1}}(L)=\{s\vert f(s)\in L\}

Note that, in general, f(f^{{-1}}(L))\neq L, while one does have

f(f^{{-1}}(L))\subseteq L

and

L\subseteq f^{{-1}}(f(L))

for any language L.

A string homomorphism is said to be \varepsilon -free (or e-free) if f(a)\neq \varepsilon for all a in the alphabet \Sigma . Simple single-letter substitution ciphers are examples of (\varepsilon -free) string homomorphisms.

An example string homomorphism g_{{uc}} can also be obtained by defining similar to the above substitution: g_{{uc}}(\langle a\rangle )=\langle A\rangle , ..., g_{{uc}}(\langle 0\rangle )=\varepsilon , but letting g_{{uc}} undefined on punctuation chars. Besides this restriction of its input domain, g_{{uc}} differs from f_{{uc}} by returning strings, while the latter returned singleton sets of strings. Examples for inverse homomorphic images are

  • g_{{uc}}^{{-1}}(\{\langle SSS\rangle \})=\{\langle sss\rangle ,\langle {\text{sß}}\rangle ,\langle {\text{ßs}}\rangle \}, since g_{{uc}}(\langle sss\rangle )=g_{{uc}}(\langle {\text{sß}}\rangle )=g_{{uc}}(\langle {\text{ßs}}\rangle )=\langle SSS\rangle , and
  • g_{{uc}}^{{-1}}(\{\langle A\rangle ,\langle bb\rangle \})=\{\langle a\rangle \}, since g_{{uc}}(\langle a\rangle )=\langle A\rangle , while \langle bb\rangle cannot be reached by g_{{uc}}.

For the latter language, g_{{uc}}(g_{{uc}}^{{-1}}(\{\langle A\rangle ,\langle bb\rangle \}))=g_{{uc}}(\{\langle a\rangle \})=\{\langle A\rangle \}\neq \{\langle A\rangle ,\langle bb\rangle \}. The homomorphism g_{{uc}} is not \varepsilon -free, since it maps e.g. \langle 0\rangle to \varepsilon .

String projection

If s is a string, and \Sigma is an alphabet, the string projection of s is the string that results by removing all letters which are not in \Sigma . It is written as \pi _{\Sigma }(s)\,. It is formally defined by removal of letters from the right hand side:

\pi _{\Sigma }(s)={\begin{cases}\varepsilon &{\mbox{if }}s=\varepsilon {\mbox{ the empty string}}\\\pi _{\Sigma }(t)&{\mbox{if }}s=ta{\mbox{ and }}a\notin \Sigma \\\pi _{\Sigma }(t)a&{\mbox{if }}s=ta{\mbox{ and }}a\in \Sigma \end{cases}}

Here \varepsilon denotes the empty string. The projection of a string is essentially the same as a projection in relational algebra.

String projection may be promoted to the projection of a language. Given a formal language L, its projection is given by

\pi _{\Sigma }(L)=\{\pi _{\Sigma }(s)\vert s\in L\}

Right quotient

The right quotient of a letter a from a string s is the truncation of the letter a in the string s, from the right hand side. It is denoted as s/a. If the string does not have a on the right hand side, the result is the empty string. Thus:

(sa)/b={\begin{cases}s&{\mbox{if }}a=b\\\varepsilon &{\mbox{if }}a\neq b\end{cases}}

The quotient of the empty string may be taken:

\varepsilon /a=\varepsilon

Similarly, given a subset S\subset M of a monoid M, one may define the quotient subset as

S/a=\{s\in M\vert sa\in S\}

Left quotients may be defined similarly, with operations taking place on the left of a string.

Syntactic relation

The right quotient of a subset S\subset M of a monoid M defines an equivalence relation, called the right syntactic relation of S. It is given by

\sim _{S}\;\,=\,\{(s,t)\in M\times M\vert S/s=S/t\}

The relation is clearly of finite index (has a finite number of equivalence classes) if and only if the family right quotients is finite; that is, if

\{S/m\vert m\in M\}

is finite. In this case, S is a recognizable language, that is, a language that can be recognized by a finite state automaton. This is discussed in greater detail in the article on syntactic monoids.

Right cancellation

The right cancellation of a letter a from a string s is the removal of the first occurrence of the letter a in the string s, starting from the right hand side. It is denoted as s\div a and is recursively defined as

(sa)\div b={\begin{cases}s&{\mbox{if }}a=b\\(s\div b)a&{\mbox{if }}a\neq b\end{cases}}

The empty string is always cancellable:

\varepsilon \div a=\varepsilon

Clearly, right cancellation and projection commute:

\pi _{\Sigma }(s)\div a=\pi _{\Sigma }(s\div a)

Prefixes

The prefixes of a string is the set of all prefixes to a string, with respect to a given language:

\operatorname {Pref}_{L}(s)=\{t\vert s=tu{\mbox{ for }}t,u\in \operatorname {Alph}(L)^{*}\}

here s\in L.

The prefix closure of a language is

\operatorname {Pref}(L)=\bigcup _{{s\in L}}\operatorname {Pref}_{L}(s)=\left\{t\vert s=tu;s\in L;t,u\in \operatorname {Alph}(L)^{*}\right\}

Example:
L=\left\{abc\right\}{\mbox{ then }}\operatorname {Pref}(L)=\left\{\varepsilon ,a,ab,abc\right\}

A language is called prefix closed if \operatorname {Pref}(L)=L.

The prefix closure operator is idempotent:

\operatorname {Pref}(\operatorname {Pref}(L))=\operatorname {Pref}(L)

The prefix relation is a binary relation \sqsubseteq such that s\sqsubseteq t if and only if s\in \operatorname {Pref}_{L}(t). This relation is a particular example of a prefix order.

See also

References

    • Hopcroft, John E.; Ullman, Jeffrey D. (1979). Introduction to Automata Theory, Languages and Computation. Reading, Massachusetts: Addison-Wesley Publishing. ISBN 0-201-02988-X. Zbl 0426.68001.  (See chapter 3.)
    This article is issued from Wikipedia. The text is available under the Creative Commons Attribution/Share Alike; additional terms may apply for the media files.